Skip to content

Commit 32685e9

Browse files
committed
fix(sandbox): resolve wait on disconnect
Complete live command and PTY wait promises immediately when disconnecting so socket event timing cannot leave Node 14 tests hanging.
1 parent e670855 commit 32685e9

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

qiniu/sandbox/commands.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ function CommandHandle (commands, pid, result, opts, request, waitPromise) {
108108
this.opts = opts || {};
109109
this._request = request;
110110
this._waitPromise = waitPromise;
111+
this._resolveWait = null;
111112
this.stdout = result && result.stdout ? result.stdout : '';
112113
this.stderr = result && result.stderr ? result.stderr : '';
113114
}
@@ -135,11 +136,26 @@ CommandHandle.prototype.kill = function () {
135136
};
136137

137138
CommandHandle.prototype.disconnect = function () {
139+
this._disconnected = true;
138140
if (this._request) {
139-
this._disconnected = true;
140141
this._request.destroy();
141142
this._request = null;
142143
}
144+
if (this._resolveWait) {
145+
const result = this.result || {
146+
pid: this.pid,
147+
exitCode: 0,
148+
stdout: this.stdout,
149+
stderr: this.stderr,
150+
error: ''
151+
};
152+
if (result.exitCode === -1) {
153+
result.exitCode = 0;
154+
}
155+
this.result = result;
156+
this._resolveWait(result);
157+
this._resolveWait = null;
158+
}
143159
return Promise.resolve();
144160
};
145161

@@ -212,6 +228,7 @@ function connectLiveCommand (commands, procedure, body, opts, fallbackPid) {
212228
cleanupStartTimer();
213229
result.pid = pid || result.pid;
214230
handle = new CommandHandle(commands, result.pid, null, opts, req, waitPromise);
231+
handle._resolveWait = resolveWait;
215232
settled = true;
216233
resolve(handle);
217234
}
@@ -248,6 +265,7 @@ function connectLiveCommand (commands, procedure, body, opts, fallbackPid) {
248265
result.error = end && end.error ? end.error : '';
249266
if (handle) {
250267
handle.result = result;
268+
handle._resolveWait = null;
251269
}
252270
resolveWait(result);
253271
}

qiniu/sandbox/pty.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function LivePtyHandle (pty, pid, request, waitPromise, opts) {
3333
this.pid = pid;
3434
this._request = request;
3535
this._waitPromise = waitPromise;
36+
this._resolveWait = null;
3637
this.opts = opts || {};
3738
this.stdout = '';
3839
this.stderr = '';
@@ -52,11 +53,20 @@ LivePtyHandle.prototype.kill = function () {
5253
};
5354

5455
LivePtyHandle.prototype.disconnect = function () {
56+
this._disconnected = true;
5557
if (this._request) {
56-
this._disconnected = true;
5758
this._request.destroy();
5859
this._request = null;
5960
}
61+
if (this._resolveWait) {
62+
const result = {
63+
exitCode: 0,
64+
stdout: this.stdout,
65+
stderr: this.stderr
66+
};
67+
this._resolveWait(result);
68+
this._resolveWait = null;
69+
}
6070
return Promise.resolve();
6171
};
6272

@@ -124,6 +134,7 @@ function connectLivePty (sandbox, procedure, body, opts, pty) {
124134
if (event.start && !handle) {
125135
cleanupStartTimer();
126136
handle = new LivePtyHandle(pty, event.start.pid, req, waitPromise, opts);
137+
handle._resolveWait = resolveWait;
127138
settled = true;
128139
resolve(handle);
129140
}
@@ -151,6 +162,9 @@ function connectLivePty (sandbox, procedure, body, opts, pty) {
151162
exitCode: event.end.exitCode === undefined ? 0 : event.end.exitCode,
152163
error: event.end.error || ''
153164
});
165+
if (handle) {
166+
handle._resolveWait = null;
167+
}
154168
resolveWait(result);
155169
}
156170
}

0 commit comments

Comments
 (0)