Skip to content

Commit 7106c4d

Browse files
server: prevent server from auto-terminating
- IMPORTANT: We need the setInterval for detached to work, so that the server does not auto-terminate when the parent does. See the code comment. - Additional: As a safety measure we allow a detached LS without a client/parent to live for at most a week. This is an arbitrary amount. Signed-off-by: nkomonen-amazon <nkomonen@amazon.com>
1 parent dd87d6c commit 7106c4d

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

server/src/node/main.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,33 @@ function isDetached() {
4646
}
4747

4848
if(process.argv.includes('--detached')) {
49-
setDetached();
50-
} else {
51-
_isDetached = false;
52-
}
53-
return _isDetached;
54-
55-
function setDetached() {
5649
_isDetached = true;
5750

5851
// Ignores the default behavior when then parent/child communication
5952
// channel (e.g. IPC) disconnects, the child terminates.
6053
process.on('disconnect', () => {});
54+
55+
// Keeps the server alive since node may terminate this process if
56+
// the parent terminates + this process has nothing in the event loop.
57+
const oneWeekMilliseconds = 7 * 24 * 60 * 60 * 1000;
58+
const oneHourMilliseconds = 60 * 60 * 1000;
59+
setInterval(() => {
60+
// Allow detached servers to live at most 1 week after disconnecting from the client
61+
if (_protocolConnectionEndTime && Date.now() - _protocolConnectionEndTime >= oneWeekMilliseconds) {
62+
process.exit(7);
63+
}
64+
}, oneHourMilliseconds);
65+
} else {
66+
_isDetached = false;
6167
}
68+
return _isDetached;
6269
}
6370

6471
let _protocolConnection: ProtocolConnection | undefined;
72+
let _protocolConnectionEndTime: number | undefined = undefined;
6573
function endProtocolConnection(): void {
74+
_protocolConnectionEndTime = Date.now();
75+
6676
if (_protocolConnection === undefined) {
6777
return;
6878
}

0 commit comments

Comments
 (0)