Skip to content

Commit 6ab6fcc

Browse files
committed
fix: cap OpenCLI capture buffer allocation
1 parent 6985cc3 commit 6ab6fcc

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,11 @@ SDK 通过 `OpenCliBrowserSession.network` 设置 `follow(true)` 和 `all(true)`
4242
| 21 | `1ed41f928b4c906cb0499955687d33270c70c2ca` | 33 / 5,866 / 4,518 ms | 11 / 2,048 / 2,844 / 2,208 ms |
4343

4444
6 个场景通过,0 失败、0 跳过。SDK 分别返回 `EXECUTION_TIMEOUT` 和 `OUTPUT_LIMIT`,均附带部分输出和 `ROOT_EXIT_CONFIRMED`。每组验证确认共享 daemon 的 PID 和隔离浏览器进程在两次调用后仍存活,后续标签页查询仍成功。当前结果验证 macOS 上的同步有界采集。受控 sink、后代进程所有权和其他操作系统上的真实浏览器验收仍待验证。
45+
46+
## 2026-11-18 OC-PROC-003 捕获数组扩容
47+
48+
JDK 17 红灯测试以 8,193 字节作为捕获上限,分两次写满,观察到旧实现把 backing array 扩到 16,384 字节。修复对单次分配容量应用捕获上限。回归验证 8,193 字节数组、原样保留的前缀、准确的观测字节数及截断标记。
49+
50+
JDK 21 使用仓库 Maven Wrapper 执行 `-B -ntp -o clean verify`,2,114 项通过,0 失败、0 错误、0 跳过。真实 OpenCLI 1.8.6 两项复验通过。4,500 ms 时限返回 `EXECUTION_TIMEOUT` 并保留 34 条完整事件。2 KiB 上限返回 `OUTPUT_LIMIT` 并保留 2,048 字节和 11 条完整事件。两次均确认 SDK 启动的 CLI 子进程已退出,后续 daemon 查询可用。
51+
52+
本次测量单个捕获数组的容量。整体堆内存峰值还包含扩容时的数组复制、输出快照和并发调用,任务 1.2 继续跟踪。

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ private static long nanos(long millis, String field) {
362362
return millis * 1_000_000L;
363363
}
364364

365-
private static final class BoundedCapture extends ByteArrayOutputStream {
365+
static final class BoundedCapture extends ByteArrayOutputStream {
366366
private final int limit;
367367
private long observed;
368368
private boolean truncated;
@@ -378,12 +378,18 @@ public synchronized void write(byte[] bytes, int offset, int length) {
378378
if (frozen) { return; }
379379
observed = observed > Long.MAX_VALUE - length ? Long.MAX_VALUE : observed + length;
380380
int retained = Math.min(length, limit - count);
381+
int required = count + retained;
382+
if (required > buf.length) {
383+
int doubled = (int) Math.min(2L * buf.length, limit);
384+
buf = Arrays.copyOf(buf, Math.max(required, doubled));
385+
}
381386
super.write(bytes, offset, retained);
382387
truncated |= retained < length;
383388
}
384389

385390
@Override
386391
public synchronized void write(int value) { write(new byte[]{(byte) value}, 0, 1); }
392+
synchronized int bufferCapacity() { return buf.length; }
387393
synchronized boolean isTruncated() { return truncated; }
388394
synchronized long observed() { return observed; }
389395
synchronized void freeze() { frozen = true; }

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

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

66
class SubprocessExecutionSupportTest {
77

8+
@Test
9+
void shouldBoundBackingBufferGrowthAtTheConfiguredLimit() {
10+
int limit = 8_193;
11+
SubprocessExecutionSupport.BoundedCapture capture = new SubprocessExecutionSupport.BoundedCapture(limit);
12+
byte[] chunk = new byte[8_192];
13+
capture.write(chunk, 0, chunk.length);
14+
capture.write(99);
15+
capture.write(100);
16+
17+
assertEquals(limit, capture.bufferCapacity());
18+
assertEquals(limit, capture.size());
19+
assertEquals(99, capture.toByteArray()[limit - 1]);
20+
assertEquals(limit + 1L, capture.observed());
21+
assertTrue(capture.isTruncated());
22+
}
23+
824
@Test
925
void shouldReturnDefaultMaxConcurrent() {
1026
int defaultVal = SubprocessExecutionSupport.defaultMaxConcurrentExecutions();

0 commit comments

Comments
 (0)