Skip to content

Commit 3afad15

Browse files
committed
Exits after everything written to stdout has been drained.
1 parent ad9786d commit 3afad15

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

lib/CLI.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,7 +2043,7 @@ function speedList(code) {
20432043
return setTimeout(speedList, 1400);
20442044
}
20452045
console.error('Error retrieving process list: %s.\nA process seems to be on infinite loop, retry in 5 seconds',err);
2046-
exitCli(cst.ERROR_EXIT);
2046+
return exitCli(cst.ERROR_EXIT);
20472047
}
20482048
if (commander.miniList && !commander.silent)
20492049
UX.miniDisplay(list);
@@ -2061,7 +2061,7 @@ function speedList(code) {
20612061
return CLI.streamLogs(null, 1);
20622062
}
20632063
else {
2064-
//return exitCli(code ? code : cst.SUCCESS_EXIT);
2064+
return exitCli(code ? code : cst.SUCCESS_EXIT);
20652065
}
20662066
});
20672067
}

lib/Common.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,33 @@ Common.formatCLU = function(csp) {
187187
Common.exitCli = function(code) {
188188
InteractorDaemonizer.disconnectRPC(function() {
189189
Satan.disconnectRPC(function() {
190-
process.exit(code || 0);
190+
code = code || 0;
191+
// Safe exits process after all streams are drained.
192+
// file descriptor flag.
193+
var fds = 0;
194+
// exits process when stdout (1) and sdterr(2) are both drained.
195+
function tryToExit() {
196+
if ((fds & 1) && (fds & 2)) {
197+
process.exit(code);
198+
}
199+
}
200+
201+
[process.stdout, process.stderr].forEach(function(std) {
202+
var fd = std.fd;
203+
if (!std.bufferSize) {
204+
// bufferSize equals 0 means current stream is drained.
205+
fds = fds | fd;
206+
} else {
207+
// Appends nothing to the std queue, but will trigger `tryToExit` event on `drain`.
208+
std.write && std.write('', function() {
209+
fds = fds | fd;
210+
tryToExit();
211+
});
212+
}
213+
// Does not write anything more.
214+
delete std.write;
215+
});
216+
tryToExit();
191217
});
192218
});
193219
};

0 commit comments

Comments
 (0)