Skip to content

Commit dd3ce6e

Browse files
committed
fix: allocate OpenCLI output capture on first write
1 parent 1847740 commit dd3ce6e

4 files changed

Lines changed: 28 additions & 4 deletions

File tree

‎openspec/changes/harden-opencli-process-lifecycle/tasks.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,11 @@ JDK 21 使用仓库 Maven Wrapper 执行 `-B -ntp -o clean verify`,2,115 项
6666
新增真实继承管道回归。根进程启动继承 stdout、stderr 的独立 JVM,后者通过文件汇报心跳。观察到后代进程启动后取消调用。清理宽限配置为 250 ms,测试允许 1,500 ms 等待窗口,包含线程调度。SDK 仍返回 `CANCELLED`,流未排空时必须报告 `UNCONFIRMED`,并且不声称后代已退出。断言取消后后代心跳仍更新,最后由测试自己的释放文件终止它。
6767

6868
JDK 21 使用仓库 Maven Wrapper 执行 `-B -ntp -o clean verify`,2,116 项通过,0 失败、0 错误、0 跳过。该场景验证未追踪后代的安全边界,后代所有权和回收能力仍由 2.3、3.2 跟踪。
69+
70+
## 2026-11-18 OC-PROC-003 输出缓冲区按需分配
71+
72+
修复前,构造捕获对象即分配每个输出流的初始数组,默认两个共 16 KiB,包括仍排队或在启动前取消的调用。JDK 17 的两项回归分别观测到预取消路径的 16 字节测试配置和普通空写路径的 8,192 字节数组容量,预期均为零。
73+
74+
修复后,第一次非空写入才分配对应捕获数组,仍受流的配置上限约束。预取消路径的 stdout、stderr 容量均为零,读取线程的工作缓冲和 JVM 总堆内存属独立指标。红灯 2 项失败,修复后 JDK 17 定向 33 项通过。
75+
76+
JDK 21 使用仓库 Maven Wrapper 执行 `-B -ntp -o clean verify`,2,117 项通过,0 失败、0 错误、0 跳过。

‎src/main/java/io/github/easy4j/opencli/core/support/SubprocessExecutionSupport.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ interface WatchdogFactory {
4444
public static final long WAIT_GRACE_MILLIS = 5_000L;
4545
public static final int DEFAULT_STDOUT_LIMIT = 8 * 1024 * 1024;
4646
public static final int DEFAULT_STDERR_LIMIT = 2 * 1024 * 1024;
47+
private static final int READ_BUFFER_BYTES = 8192;
4748
private static final long POLL_NANOS = TimeUnit.MILLISECONDS.toNanos(10L);
4849
private static final int DEFAULT_MAX_CONCURRENT = Math.max(2, java.lang.Runtime.getRuntime().availableProcessors());
4950
private static final Object LEGACY_LOCK = new Object();
@@ -369,7 +370,7 @@ static final class BoundedCapture extends ByteArrayOutputStream {
369370
private boolean frozen;
370371

371372
BoundedCapture(int limit) {
372-
super(Math.min(8192, limit));
373+
super(0);
373374
this.limit = limit;
374375
}
375376

@@ -380,8 +381,8 @@ public synchronized void write(byte[] bytes, int offset, int length) {
380381
int retained = Math.min(length, limit - count);
381382
int required = count + retained;
382383
if (required > buf.length) {
383-
int doubled = (int) Math.min(2L * buf.length, limit);
384-
buf = Arrays.copyOf(buf, Math.max(required, doubled));
384+
int growth = (int) Math.min(limit, Math.max(READ_BUFFER_BYTES, 2L * buf.length));
385+
buf = Arrays.copyOf(buf, Math.max(required, growth));
385386
}
386387
super.write(bytes, offset, retained);
387388
truncated |= retained < length;
@@ -410,7 +411,7 @@ private static final class Reader extends Thread {
410411
@Override
411412
public void run() {
412413
try (InputStream stream = input) {
413-
byte[] buffer = new byte[8192];
414+
byte[] buffer = new byte[READ_BUFFER_BYTES];
414415
int size;
415416
while ((size = stream.read(buffer)) != -1) {
416417
capture.write(buffer, 0, size);

‎src/test/java/io/github/easy4j/opencli/core/support/SubprocessCleanupFaultTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ void cancelledSubmissionDoesNotInitializeWatchdogOrSpawn() throws Exception {
164164
assertEquals(CleanupState.NOT_STARTED, session.getExecutionDetails().getCleanupState());
165165
assertNull(session.getObservedExitCode());
166166
assertNotNull(session.getWatchdog());
167+
assertEquals(0, ((SubprocessExecutionSupport.BoundedCapture) session.getStdout()).bufferCapacity());
168+
assertEquals(0, ((SubprocessExecutionSupport.BoundedCapture) session.getStderr()).bufferCapacity());
167169
}
168170

169171
private static void assertCleanupSurvives(boolean failStop, boolean failDestroy) throws Exception {

‎src/test/java/io/github/easy4j/opencli/core/support/SubprocessExecutionSupportTest.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55

66
class SubprocessExecutionSupportTest {
77

8+
@Test
9+
void shouldAllocateOutputStorageOnlyOnFirstNonemptyWrite() {
10+
SubprocessExecutionSupport.BoundedCapture capture = new SubprocessExecutionSupport.BoundedCapture(12_000);
11+
byte[] empty = new byte[0];
12+
capture.write(empty, 0, empty.length);
13+
14+
assertEquals(0, capture.bufferCapacity());
15+
capture.write(99);
16+
assertEquals(1, capture.size());
17+
assertEquals(99, capture.toByteArray()[0]);
18+
assertEquals(8_192, capture.bufferCapacity());
19+
}
20+
821
@Test
922
void shouldBoundBackingBufferGrowthAtTheConfiguredLimit() {
1023
int limit = 8_193;

0 commit comments

Comments
 (0)