Repository navigation
fix(harness): preserve Docker exec commands on Windows - #2925
guslegend0510 wants to merge 30 commits into
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
oss-maintainer
left a comment
There was a problem hiding this comment.
Summary
Re-review of PR #2925 at its current head (automated review of the changes in this diff). 2 item(s) raised below; nothing here blocks the approach itself — details inline.
Findings
- [Warning]
agentscope-harness/src/main/java/io/agentscope/harness/agent/sandbox/impl/docker/DockerSandbox.java:185— The stdin writer thread performs blocking I/O on a realOutputStreamwhich is not interruptible byThread.interrupt(). If the pipe buffer fills (unlikely for typical sandbox commands, but possible for very large generated scripts) and the process stalls,stdinFuture.cancel(true)in the timeout path will not unblock the writer. Consider adding a deadline tostdinFuture.get()in the success path (e.g.stdinFuture.get(5, TimeUnit.SECONDS)) to avoid an indefinite hang if the writer is stuck, or use a socket-based stream that supports async close. - [Info]
agentscope-harness/src/main/java/io/agentscope/harness/agent/sandbox/impl/docker/DockerSandbox.java:228— The{...} </dev/nullwrapper intentionally severs stdin for inner commands to prevent them from consuming the script text fed tosh -s. This is a behavioral change from the oldsh -capproach: commands like barecatorreadthat previously would hang waiting for stdin will now silently receive EOF from/dev/null. Consider documenting this constraint in the class-level Javadoc or thewrapCommandForStdinJavadoc so callers are aware.
Notes
Inline comments are anchored to the diff; if a line anchor was out of range it is listed above instead.
Automated review by github-manager-bot
| ioExecutor.submit( | ||
| () -> { | ||
| try (OutputStream stdin = process.getOutputStream()) { | ||
| stdin.write( |
There was a problem hiding this comment.
The stdin writer thread performs blocking I/O on a real OutputStream which is not interruptible by Thread.interrupt(). If the pipe buffer fills (unlikely for typical sandbox commands, but possible for very large generated scripts) and the process stalls, stdinFuture.cancel(true) in the timeout path will not unblock the writer. Consider adding a deadline to stdinFuture.get() in the success path (e.g. stdinFuture.get(5, TimeUnit.SECONDS)) to avoid an indefinite hang if the writer is stuck, or use a socket-based stream that supports async close.
| * | ||
| * <p>The shell parses the complete compound command before applying the {@code /dev/null} | ||
| * redirection. Commands such as {@code read} therefore cannot consume the remaining script, | ||
| * while heredocs and explicit stdin redirections continue to work. |
There was a problem hiding this comment.
The {...} </dev/null wrapper intentionally severs stdin for inner commands to prevent them from consuming the script text fed to sh -s. This is a behavioral change from the old sh -c approach: commands like bare cat or read that previously would hang waiting for stdin will now silently receive EOF from /dev/null. Consider documenting this constraint in the class-level Javadoc or the wrapCommandForStdin Javadoc so callers are aware.
oss-maintainer
left a comment
There was a problem hiding this comment.
Summary
Streaming the script through sh -s instead of sh -c <argv> is the right fix for Windows argv mangling, and wrapping it in { : ...; } </dev/null is a neat way to stop an inner read from swallowing the rest of the script while heredocs and explicit redirects keep working. Tests are thorough (nested quotes/unicode/heredoc byte-fidelity/exit-code/timeout/truncation).
Findings
- [Warning]
stdinFuture.get()on the success path can convert a successful exec into a thrown exception on EPIPE. - [Info] the group redirect also denies host stdin to inner commands — document the invariant.
- [Info] no test for "process exits while the script is still being written".
Verdict
Nothing here is architecturally wrong; the [Warning] is a small robustness fix. Not approving in this round because the PR still has a merge conflict with main — rebase and ping for a re-review.
Automated review by github-manager-bot
| awaitWriterIgnoringFailure(stdinFuture); | ||
| throw new SandboxException.ExecException(exitCode, stdout, stderr); | ||
| } | ||
| stdinFuture.get(); |
There was a problem hiding this comment.
This unconditional get() turns a writer failure into an exec failure. If the child exits before draining the script (a large command, or one that ends early), the write fails with EPIPE, the Future completes exceptionally, and a command that exited 0 is reported as an exception instead of an ExecResult. The non-zero path already calls awaitWriterIgnoringFailure(stdinFuture) for exactly this reason — consider the same treatment here, since the exit code is the authoritative signal.
| */ | ||
| static String wrapCommandForStdin(String command) { | ||
| String trailingNewline = command.endsWith("\n") ? "" : "\n"; | ||
| return "{\n:\n" + command + trailingNewline + "} </dev/null\n"; |
There was a problem hiding this comment.
Worth a note in the javadoc that the wrapper also means the inner commands never see host stdin (it is /dev/null for the whole group). Today nothing pipes data in from the host so this is a feature, but it is the kind of invariant that a future execWithStdin caller would trip over.
| } | ||
|
|
||
| @Test | ||
| void destroysProcessAndClosesStdinOnTimeout() { |
There was a problem hiding this comment.
Nice seam with startExecProcess. One gap: no case where the process exits while the writer still has bytes pending (the EPIPE scenario above). A FakeProcess whose stdin sink rejects writes after exitValue() becomes available would pin down whatever behaviour you decide on here.
AgentScope-Java Version
2.0.3-SNAPSHOT
Description
Fixes #2924
DockerSandbox.doExec()previously passed the complete shell command as the finaldocker.exeargument (sh -c <command>). On Windows, nested double quotes can be altered while the Win32 command line is serialized and parsed, breaking filesystem commands such aswrite_fileandedit_file.This PR:
docker exec -i ... sh -sand streams the command through UTF-8 stdin instead of placing it in argv;Testing
mvn -pl agentscope-harness -am -Dtest=DockerSandboxExecTest -Dsurefire.failIfNoSpecifiedTests=false testmvn -pl agentscope-harness -am testmvn spotless:checkgit diff --checkChecklist
mvn spotless:applymvn test)