Skip to content

Close all remaining children on exit#652

Merged
subdavis merged 5 commits into
mainfrom
desktop/fix-597
Mar 19, 2021
Merged

Close all remaining children on exit#652
subdavis merged 5 commits into
mainfrom
desktop/fix-597

Conversation

@subdavis
Copy link
Copy Markdown
Contributor

fixes #597

@subdavis subdavis requested a review from BryonLewis March 18, 2021 21:45
Comment on lines +28 to +40
function close(child: ChildProcess): Promise<void> {
const onclose = new Promise<void>((resolve) => {
child.on('exit', (code, signal) => {
console.warn(`pid=${child.pid} exited with code=${code} and signal=${signal}`);
resolve();
});
});

child.kill('SIGTERM');
child.kill('SIGINT');
child.kill('SIGKILL');
return onclose;
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please tell me your thoughts on sending this barrage of signals

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not as a familiar with this but would we not want to chain these calls without some check in case the PID is reassigned?

https://nodejs.org/api/child_process.html#child_process_subprocess_kill_signal

The ChildProcess object may emit an 'error' event if the signal cannot be delivered. Sending a signal to a child process that has already exited is not an error but may have unforeseen consequences. Specifically, if the process identifier (PID) has been reassigned to another process, the signal will be delivered to that process instead which can have unexpected results.

I don't know if there is a good solution to this. At most you can check .kill() return value to make sure it received the signal. But that I still think isn't an indication that the process was actually killed, just that it received the signal. It is at least a secondary check to prevent getting to the weird reassignment PID state.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit slow, I didn't realize that the exit event had the signal if the process terminated using a signal. You could change this so it uses some sort of cascading timeout to elevate the kill signal and reduce the chance calling SIGKILL on a PID reassignment. So instead of blasting the signals it elevates and gives it time to clean up if needed. Just give some time between SIGTERM/SIGINT and SIGKILL for the process to hopefully properly terminate and then finally kill it if it goes beyond some timeout (I don't know what that should be, 30000ms?). No clue if any of this makes any sense or if there is a better way to do this.

Copy link
Copy Markdown
Contributor Author

@subdavis subdavis Mar 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot, unfortunately. Any use of the event scheduler (such as with callbacks or async api) will be dropped. This does mean that the exit event will often not be caught and you just have to trust that the process exited.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in e3a3aa2

Copy link
Copy Markdown
Collaborator

@BryonLewis BryonLewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a run through on Windows as well. It was confirmed to be killing the outstanding processes on a force exit while pipelines were running.

@subdavis subdavis merged commit b036c8f into main Mar 19, 2021
@subdavis subdavis deleted the desktop/fix-597 branch March 19, 2021 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] When DIVE desktop exits, force exit all running pipelines

2 participants