Skip to content

Commit 57a36b2

Browse files
committed
fix(exec): 同步 1.0.x 的 stdin 管道竞态修复——执行器总是即时关闭子进程 stdin;替身脚本排空 stdin;commons-exec 1.4.0→1.6.0(对齐 easy4j 依赖矩阵)
1 parent 0649d09 commit 57a36b2

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4848
<!-- Dependency versions -->
4949
<additionalparam>-Xdoclint:none</additionalparam>
50-
<commons-exec.version>1.4.0</commons-exec.version>
50+
<commons-exec.version>1.6.0</commons-exec.version>
5151
<jackson-bom.version>2.22.1</jackson-bom.version>
5252
<junit.version>5.11.4</junit.version>
5353
<junit-jupiter.version>6.1.0</junit-jupiter.version>

src/main/java/io/github/easy4j/codex/cli/CodexCliExecutor.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ private CodexCliResult runProcess(String stdin, String... args) {
120120
DefaultExecutor executor = new DefaultExecutor();
121121
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
122122
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
123-
if (stdin != null && !stdin.isEmpty()) {
124-
executor.setStreamHandler(new org.apache.commons.exec.PumpStreamHandler(stdout, stderr,
125-
new ByteArrayInputStream(stdin.getBytes(StandardCharsets.UTF_8))));
126-
} else {
127-
executor.setStreamHandler(new org.apache.commons.exec.PumpStreamHandler(stdout, stderr));
128-
}
123+
// Always hand the child a (possibly empty) stdin pipe that closes
124+
// right after the payload: consumers like `codex login --with-api-key`
125+
// read to EOF, and a closed pipe cannot race the input pump.
126+
byte[] stdinBytes = stdin == null ? new byte[0] : stdin.getBytes(StandardCharsets.UTF_8);
127+
executor.setStreamHandler(new org.apache.commons.exec.PumpStreamHandler(stdout, stderr,
128+
new ByteArrayInputStream(stdinBytes)));
129129

130130
long timeoutMs = config.getLocalTimeoutSeconds() * 1000L;
131131
ExecuteWatchdog watchdog = new ExecuteWatchdog(timeoutMs);

src/test/resources/echo-args.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
# coreutils echo (Linux) interprets --version/--help as flags while BSD echo
77
# (macOS) prints them literally.
88
#
9+
# Drain piped stdin first: real `codex login --with-api-key` consumes its
10+
# stdin payload; reading to EOF also keeps the executor's input pump race-free
11+
# (an immediately-closed pipe yields instant EOF here).
12+
cat > /dev/null 2>/dev/null
913
printf '%s\n' "$*"

0 commit comments

Comments
 (0)