The best way to control Claude Code, Codex and more from any computer or phone
perf(server): Windows terminals report exit 0.9 s sooner (PowerShell 1,034 → 138 ms) (#30880)
Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Pyseph committed
Nov 15, 2026 at 02:47 UTC
6a8f022b451f0d678c8da2ecee0042987e80d2d9
3 files changed
+166
-4
apps/server/src/terminal/nodePtyFlushDataAndCleanUp.test.ts
new
+131
@@ -0,0 +1,131 @@
1
+import * as NodeModule from "node:module";
2
+
3
+import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
4
+
5
+const requireCjs = NodeModule.createRequire(import.meta.url);
6
+const {
7
+ WindowsPtyAgent,
8
+}: {
9
+ WindowsPtyAgent: {
10
+ prototype: {
11
+ _flushDataAndCleanUp(this: FakeAgent): void;
12
+ _cleanUpProcess(this: FakeAgent): void;
13
+ };
14
+ };
15
+} = requireCjs("node-pty/lib/windowsPtyAgent.js");
16
+
17
+interface FakeAgent {
18
+ _useConptyDll: boolean;
19
+ _closeTimeout?: unknown;
20
+ _inSocket: { readable: boolean };
21
+ _outSocket: { readable: boolean; destroy: () => void };
22
+ _cleanUpProcess: () => void;
23
+}
24
+
25
+// The sockets are stand-ins; the timer logic and _cleanUpProcess are node-pty's own (as patched by T3).
26
+const makeAgent = (): FakeAgent => {
27
+ const agent: FakeAgent = {
28
+ _useConptyDll: false,
29
+ _inSocket: { readable: true },
30
+ _outSocket: { readable: true, destroy: vi.fn() },
31
+ _cleanUpProcess: () => WindowsPtyAgent.prototype._cleanUpProcess.call(agent),
32
+ };
33
+ return agent;
34
+};
35
+
36
+const flush = (agent: FakeAgent) => WindowsPtyAgent.prototype._flushDataAndCleanUp.call(agent);
37
+
38
+// A minimal event loop that mirrors Node's phases: expired timers run first, then immediates (the check phase).
39
+// node-pty calls the global timer functions at call time, so replacing them lets the test place a "poll phase"
40
+// (delivery of already-buffered pipe data) exactly between the two.
41
+interface Timer {
42
+ readonly id: number;
43
+ readonly dueAt: number;
44
+ readonly run: () => void;
45
+}
46
+let now = 0;
47
+let nextId = 0;
48
+let timers: Timer[] = [];
49
+let immediates: Array<() => void> = [];
50
+
51
+const runExpiredTimers = () => {
52
+ for (const timer of timers.filter((t) => t.dueAt <= now).toSorted((a, b) => a.dueAt - b.dueAt)) {
53
+ timers = timers.filter((t) => t.id !== timer.id);
54
+ timer.run();
55
+ }
56
+};
57
+const runImmediates = () => {
58
+ const batch = immediates;
59
+ immediates = [];
60
+ for (const run of batch) run();
61
+};
62
+/** One loop iteration after `ms` of wall time; `poll` runs between the timers and check phases. */
63
+const loopIteration = (ms: number, poll?: () => void) => {
64
+ now += ms;
65
+ runExpiredTimers();
66
+ poll?.();
67
+ runImmediates();
68
+};
69
+
70
+// ConPTY never closes the output pipe after the shell exits, so node-pty ends the session on a timer.
71
+// T3 shortens it (patches/node-pty@1.2.0-beta.15.patch) so the terminal reports "exited" about 0.9 s sooner.
72
+describe("node-pty Windows exit flush", () => {
73
+ beforeEach(() => {
74
+ now = 0;
75
+ nextId = 0;
76
+ timers = [];
77
+ immediates = [];
78
+ vi.stubGlobal("setTimeout", (run: () => void, ms: number) => {
79
+ const id = ++nextId;
80
+ timers.push({ id, dueAt: now + ms, run });
81
+ return id;
82
+ });
83
+ vi.stubGlobal("clearTimeout", (id: number) => {
84
+ timers = timers.filter((t) => t.id !== id);
85
+ });
86
+ vi.stubGlobal("setImmediate", (run: () => void) => {
87
+ immediates.push(run);
88
+ return ++nextId;
89
+ });
90
+ });
91
+ afterEach(() => {
92
+ vi.unstubAllGlobals();
93
+ });
94
+
95
+ it("closes the output socket 100 ms after the exit, not a second later", () => {
96
+ const agent = makeAgent();
97
+ flush(agent);
98
+
99
+ loopIteration(99);
100
+ expect(agent._outSocket.destroy).not.toHaveBeenCalled();
101
+
102
+ loopIteration(1);
103
+ expect(agent._outSocket.destroy).toHaveBeenCalledTimes(1);
104
+ expect(agent._outSocket.readable).toBe(false);
105
+ expect(agent._inSocket.readable).toBe(false);
106
+ });
107
+
108
+ // Timers run before the poll phase. After a main-thread stall longer than the window, output that is already
109
+ // buffered in the pipe is read only after the timer has expired, so the cleanup has to wait one loop turn for it.
110
+ it("keeps the socket open when buffered output is read right after a stalled timer expires", () => {
111
+ const agent = makeAgent();
112
+ flush(agent);
113
+
114
+ loopIteration(150, () => flush(agent));
115
+ expect(agent._outSocket.destroy).not.toHaveBeenCalled();
116
+
117
+ loopIteration(99);
118
+ expect(agent._outSocket.destroy).not.toHaveBeenCalled();
119
+
120
+ loopIteration(1);
121
+ expect(agent._outSocket.destroy).toHaveBeenCalledTimes(1);
122
+ });
123
+
124
+ it("closes after a stall when no output was buffered", () => {
125
+ const agent = makeAgent();
126
+ flush(agent);
127
+
128
+ loopIteration(150);
129
+ expect(agent._outSocket.destroy).toHaveBeenCalledTimes(1);
130
+ });
131
+});
patches/node-pty@1.2.0-beta.15.patch
+32
-1
@@ -17,7 +17,18 @@ diff --git a/lib/windowsConoutConnection.js b/lib/windowsConoutConnection.js
17
diff --git a/lib/windowsPtyAgent.js b/lib/windowsPtyAgent.js
18
--- a/lib/windowsPtyAgent.js
19
+++ b/lib/windowsPtyAgent.js
20
-@@ -218,13 +218,31 @@
20
+@@ -20,7 +20,9 @@ var conptyNative;
21
+ * shutting down the socket. The timer will be reset if a new data event comes in after the timer
22
+ * has started.
23
+ */
24
+-var FLUSH_DATA_INTERVAL = 1000;
25
++// ConPTY never closes the output pipe on its own after the shell exits, so wait this long for the last
26
++// screen update. Trailing output follows the exit within about 20 ms; 1000 ms made every exit report 1 s late.
27
++var FLUSH_DATA_INTERVAL = 100;
28
+ var CONNECTION_TIMEOUT = 5000;
29
+ var defaultWindowsPtyAgentOptions = {
30
+ connectionTimeout: CONNECTION_TIMEOUT,
31
+@@ -218,13 +220,31 @@
32
return Promise.resolve([]);
33
}
34
return new Promise(function (resolve) {
@@ -53,3 +64,23 @@ diff --git a/lib/windowsPtyAgent.js b/lib/windowsPtyAgent.js
64
- resolve([_this._innerPid]);
65
+ finish([_this._innerPid]);
66
}, 5000);
67
+@@ -259,7 +279,18 @@ var WindowsPtyAgent = /** @class */ (function () {
68
+ if (this._closeTimeout) {
69
+ clearTimeout(this._closeTimeout);
70
+ }
71
+- this._closeTimeout = setTimeout(function () { return _this._cleanUpProcess(); }, FLUSH_DATA_INTERVAL);
72
++ // Timers run before the poll phase. After a main-thread stall longer than the interval, output that is already
73
++ // buffered in the pipe would be dropped by the cleanup, so give the poll phase one turn to read it first; every
74
++ // chunk it reads re-arms this timer.
75
++ this._flushDataSeq = (this._flushDataSeq || 0) + 1;
76
++ this._closeTimeout = setTimeout(function () {
77
++ var seq = _this._flushDataSeq;
78
++ setImmediate(function () {
79
++ if (_this._flushDataSeq === seq) {
80
++ _this._cleanUpProcess();
81
++ }
82
++ });
83
++ }, FLUSH_DATA_INTERVAL);
84
+ };
85
+ WindowsPtyAgent.prototype._cleanUpProcess = function () {
86
+ if (this._useConptyDll) {
pnpm-lock.yaml
+3
-3
@@ -391,7 +391,7 @@ patchedDependencies:
391
micromark-extension-gfm-table@2.1.1: 9ad23a530e45d9f83cba1da8654fe16d5af8b59d5c73d5c7a7c91fd25b9be1d8
392
micromark-extension-llm-math@3.1.0: 6c191d17ea797a4ac6cc88b901041c5e6c5d4c4e508102a033e3f14e95bd2add
393
micromark@4.0.2: cb79edde7eca83bbcaaafb6ceb7b044b81b987314cde4910823b15257ecada67
394
- node-pty@1.2.0-beta.15: f1e680afec85f650699ea756f91f469844ec4c976f4f168492a45c6b3f67d651
394
+ node-pty@1.2.0-beta.15: ff8117509663fe1d5a07ef2648c324e80a9349b7d90401a00a591702615b8b30
395
react-native-gesture-handler@3.5.0: 171379dd378f8f11e9187876dec2116e5cbf5f21cef6576cdde6956518324f98
396
react-native-image-viewing@0.2.2: 4aa038d541c7ddcc8f1437eb3e7b4f769f61de78e7f91b53f9805dfca608474d
397
react-native-keyboard-controller@1.23.0: 1c7c80317ef050e08d4aabe034a0813d03b497dff2dc8b37fb5f4a6e3b20fddb
@@ -940,7 +940,7 @@ importers:
940
version: 1.3.2
941
node-pty:
942
specifier: 1.2.0-beta.15
943
- version: 1.2.0-beta.15(patch_hash=f1e680afec85f650699ea756f91f469844ec4c976f4f168492a45c6b3f67d651)
943
+ version: 1.2.0-beta.15(patch_hash=ff8117509663fe1d5a07ef2648c324e80a9349b7d90401a00a591702615b8b30)
944
playwright-core:
945
specifier: 1.64.1
946
version: 1.64.1
@@ -21627,7 +21627,7 @@ snapshots:
21627
21628
node-mock-http@1.0.5: {}
21629
21630
- node-pty@1.2.0-beta.15(patch_hash=f1e680afec85f650699ea756f91f469844ec4c976f4f168492a45c6b3f67d651):
21630
+ node-pty@1.2.0-beta.15(patch_hash=ff8117509663fe1d5a07ef2648c324e80a9349b7d90401a00a591702615b8b30):
21631
dependencies:
21632
node-addon-api: 7.1.1
21633