Skip to content

Commit 61ea55d

Browse files
committed
fix: allocate OpenCLI output capture on first write
1 parent 77569f8 commit 61ea55d

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 8 使用系统 Maven 执行 `-B -ntp -o clean verify`,2,115 项通过,0
6666
新增真实继承管道回归。根进程启动继承 stdout、stderr 的独立 JVM,后者通过文件汇报心跳。观察到后代进程启动后取消调用。清理宽限配置为 250 ms,测试允许 1,500 ms 等待窗口,包含线程调度。SDK 仍返回 `CANCELLED`,流未排空时必须报告 `UNCONFIRMED`,并且不声称后代已退出。断言取消后后代心跳仍更新,最后由测试自己的释放文件终止它。
6767

6868
JDK 8 使用系统 Maven 执行 `-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 8 使用系统 Maven 执行 `-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();
@@ -376,7 +377,7 @@ static final class BoundedCapture extends ByteArrayOutputStream {
376377
private boolean frozen;
377378

378379
BoundedCapture(int limit) {
379-
super(Math.min(8192, limit));
380+
super(0);
380381
this.limit = limit;
381382
}
382383

@@ -387,8 +388,8 @@ public synchronized void write(byte[] bytes, int offset, int length) {
387388
int retained = Math.min(length, limit - count);
388389
int required = count + retained;
389390
if (required > buf.length) {
390-
int doubled = (int) Math.min(2L * buf.length, limit);
391-
buf = Arrays.copyOf(buf, Math.max(required, doubled));
391+
int growth = (int) Math.min(limit, Math.max(READ_BUFFER_BYTES, 2L * buf.length));
392+
buf = Arrays.copyOf(buf, Math.max(required, growth));
392393
}
393394
super.write(bytes, offset, retained);
394395
truncated |= retained < length;
@@ -417,7 +418,7 @@ private static final class Reader extends Thread {
417418
@Override
418419
public void run() {
419420
try (InputStream stream = input) {
420-
byte[] buffer = new byte[8192];
421+
byte[] buffer = new byte[READ_BUFFER_BYTES];
421422
int size;
422423
while ((size = stream.read(buffer)) != -1) {
423424
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)