@@ -187,19 +187,34 @@ class ReplDriver(settings: Array[String],
187187 // TODO: i5069
188188 final def bind (name : String , value : Any )(using state : State ): State = state
189189
190+ /**
191+ * Controls whether the `System.out` and `System.err` streams are set to the provided constructor parameter instance
192+ * of [[java.io.PrintStream ]] during the execution of the repl. On by default.
193+ *
194+ * Disabling this can be beneficial when executing a repl instance inside a concurrent environment, for example a
195+ * thread pool (such as the Scala compile server in the Scala Plugin for IntelliJ IDEA).
196+ *
197+ * In such environments, indepently executing `System.setOut` and `System.setErr` without any synchronization can
198+ * lead to unpredictable results when restoring the original streams (dependent on the order of execution), leaving
199+ * the Java process in an inconsistent state.
200+ */
201+ protected def redirectOutput : Boolean = true
202+
190203 // redirecting the output allows us to test `println` in scripted tests
191204 private def withRedirectedOutput (op : => State ): State = {
192- val savedOut = System .out
193- val savedErr = System .err
194- try {
195- System .setOut(out)
196- System .setErr(out)
197- op
198- }
199- finally {
200- System .setOut(savedOut)
201- System .setErr(savedErr)
202- }
205+ if redirectOutput then
206+ val savedOut = System .out
207+ val savedErr = System .err
208+ try {
209+ System .setOut(out)
210+ System .setErr(out)
211+ op
212+ }
213+ finally {
214+ System .setOut(savedOut)
215+ System .setErr(savedErr)
216+ }
217+ else op
203218 }
204219
205220 private def newRun (state : State , reporter : StoreReporter = newStoreReporter) = {
0 commit comments