Skip to content

Route script output per thread instead of swapping System.out for the JVM - #218

Draft
slachiewicz wants to merge 7 commits into
masterfrom
agent/thread-local-output
Draft

slachiewicz wants to merge 7 commits into
masterfrom
agent/thread-local-output

Conversation

@slachiewicz

@slachiewicz slachiewicz commented Sep 20, 2026 •

Copy link
Copy Markdown
Member

Fixes #205. Both interpreters did System.setOut/setErr(scriptOutput) around an evaluation, which is why ScriptRunner serialised every run in the JVM behind a static lock and why a concurrent thread's output ended up in the script's build.log.

Now ScriptOutputRedirect swaps 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 current System.out as its target resolves it to the real stream first, so it cannot dispatch to itself. The dispatching stream overrides only the write methods, which every print, println, printf, format and append variant funnels through; the output is encoded as UTF-8, as FileLogger is. Groovy gets the log bound as out, which is where a script's println goes, 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.err lines, the bystander's line reaches the real stream, and System.out is the original object again afterwards. Every print variant on both streams produces a log byte-identical to a plain PrintStream.

@slachiewicz slachiewicz added the bug Something isn't working label Sep 20, 2026
@slachiewicz
slachiewicz marked this pull request as ready for review September 20, 2026 18:28
@slachiewicz slachiewicz added this to the 1.10 milestone Sep 21, 2026
@elharo
elharo requested a balanced review from Copilot September 29, 2026 10:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Medium severity · 2 Low severity

Open (5)
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 ScriptRunner lock.
  • 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.

Comment on lines +149 to +153
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);
Comment on lines +62 to +65
Future<?> bystander = pool.submit(() -> {
System.out.println("bystander line");
return null;
});
CURRENT.remove();
}
synchronized (LOCK) {
if (--active == 0) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need all these overrides? I would have expected most of them to work with just overriding one or two write methods.

Base automatically changed from agent/default-groovy to master September 30, 2026 15:32
@slachiewicz
slachiewicz marked this pull request as draft October 20, 2026 23:07
… 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Global System.out/System.err redirection and static lock serialize all script runs JVM-wide

3 participants