Repository navigation
Route script output per thread instead of swapping System.out for the JVM - #218
slachiewicz wants to merge 7 commits into
Conversation
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Redirect lifetime and target handling can misroute output, while Groovy binding can reject immutable variable maps.
Review effort: Balanced
Findings: 3
Open (5)
Immutable global variables map breaks output-enabled evaluation · New Child threads retain closed output redirect contexts · New Re-registering dispatcher as target causes recursive output dispatch · New Unsynchronized bystander makes concurrent output test flaky · New Test omits second script stderr and println output assertions · New
What changed in this PR
Introduces thread-aware routing for script output, enabling concurrent script execution without JVM-wide serialization.
Changes:
- Adds per-thread stdout/stderr dispatching.
- Removes the global
ScriptRunnerlock. - Adds concurrent output-isolation tests.
| File | Description |
|---|---|
ScriptRunner.java |
Removes serialized evaluation. |
ScriptOutputRedirect.java |
Implements thread-aware stream dispatch. |
GroovyScriptInterpreter.java |
Uses redirect and binds Groovy output. |
BeanShellScriptInterpreter.java |
Uses thread-aware redirect. |
ScriptOutputRedirectTest.java |
Tests concurrency and restoration. |
concurrency/verify.groovy |
Supplies concurrent test output. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| Binding binding = new Binding(globalVariables); | ||
| if (scriptOutput != null) { | ||
| // println/print/printf in a Groovy script go to the "out" variable when one is bound | ||
| binding.setVariable("out", scriptOutput); | ||
| } |
|
|
||
| private static final Object LOCK = new Object(); | ||
|
|
||
| private static final InheritableThreadLocal<PrintStream> CURRENT = new InheritableThreadLocal<>(); |
| } | ||
| } | ||
| previous = CURRENT.get(); | ||
| CURRENT.set(target); |
| Future<?> bystander = pool.submit(() -> { | ||
| System.out.println("bystander line"); | ||
| return null; | ||
| }); |
| CURRENT.remove(); | ||
| } | ||
| synchronized (LOCK) { | ||
| if (--active == 0) { |
There was a problem hiding this comment.
relying on prefix vs postfix -- is very bug-prone. consider splitting the increment from the comparison such that this doesn't matter.
| } | ||
|
|
||
| @Override | ||
| public void write(int b) { |
There was a problem hiding this comment.
do we really need all these overrides? I would have expected most of them to work with just overriding one or two write methods.
… JVM Both interpreters replaced the process-wide standard streams for the duration of an evaluation, so ScriptRunner serialised every run behind a static lock and anything another thread printed meanwhile landed in the script's log. The streams are now swapped once for a dispatching stream that forwards each call to the redirect of the calling thread, or to the original stream when there is none, and put back when the last redirect closes. Groovy also gets the log bound as "out", where its println goes. Closes #205.
new Binding(Map) keeps the caller's map, so binding the output stream as "out" (and any variable the script assigns) wrote into it and failed for an immutable map.
A caller handing the current System.out to a script made the dispatcher its own target and printing ended in a StackOverflowError.
A thread started by a script inherited the script's stream and kept writing to the closed log after the script ended. Each redirect is now a context that is marked closed on close; dispatch skips closed contexts and ends at the real stream.
PrintStream encodes print, println, printf, format and append into its write methods, so the dispatching stream overrides just the underlying sink instead of every print variant. A test drives each variant on System.out and System.err and compares the script log with a plain PrintStream.
The bystander now prints only after both scripts have printed and the scripts stay alive until it is done, instead of relying on a 50 ms sleep. The second script's System.err line and final println are asserted too.
8848c0d to
80bd69c
Compare


Fixes #205. Both interpreters did
System.setOut/setErr(scriptOutput)around an evaluation, which is whyScriptRunnerserialised every run in the JVM behind a static lock and why a concurrent thread's output ended up in the script'sbuild.log.Now
ScriptOutputRedirectswaps the process streams once, while at least one script runs, for a stream that routes each write to the redirect of the calling thread, or to the original stream when there is none; the last redirect to close puts the originals back. A redirect is a context with a parent and a closed flag, inherited by the threads a script spawns: a thread that outlives its script falls back to the first open enclosing redirect, then to the real stream, so it never writes to a closed log. A redirect given the currentSystem.outas its target resolves it to the real stream first, so it cannot dispatch to itself. The dispatching stream overrides only the write methods, which everyprint,println,printf,formatandappendvariant funnels through; the output is encoded as UTF-8, asFileLoggeris. Groovy gets the log bound asout, which is where a script'sprintlngoes, in a fresh binding so the caller's variable map is left alone. The static lock is gone.Verified: 42 tests green, including two scripts running concurrently while a third thread prints, with the three coordinated by latches rather than sleeps: each log holds only its own script's
println/System.out/System.errlines, the bystander's line reaches the real stream, andSystem.outis the original object again afterwards. Every print variant on both streams produces a log byte-identical to a plainPrintStream.