fix(process): avoid potential data race on exit (#27769)

On exit, pty_process_close() may be called after pty_process_finish1()
but before start_wait_eof_timer(), in which case the timer shouldn't be
started because pty_process_close() has already closed it.
This commit is contained in:
zeertzjq 2024-03-08 09:18:03 +08:00 committed by GitHub
parent dc2379b89b
commit d0b3c87219
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -32,9 +32,10 @@ static void start_wait_eof_timer(void **argv)
FUNC_ATTR_NONNULL_ALL
{
PtyProcess *ptyproc = (PtyProcess *)argv[0];
Process *proc = (Process *)ptyproc;
uv_timer_start(&ptyproc->wait_eof_timer, wait_eof_timer_cb, 200, 200);
if (ptyproc->finish_wait != NULL) {
uv_timer_start(&ptyproc->wait_eof_timer, wait_eof_timer_cb, 200, 200);
}
}
/// @returns zero on success, or negative error code.
@ -214,6 +215,7 @@ static void wait_eof_timer_cb(uv_timer_t *wait_eof_timer)
PtyProcess *ptyproc = wait_eof_timer->data;
Process *proc = (Process *)ptyproc;
assert(ptyproc->finish_wait != NULL);
if (proc->out.closed || proc->out.did_eof || !uv_is_readable(proc->out.uvstream)) {
uv_timer_stop(&ptyproc->wait_eof_timer);
pty_process_finish2(ptyproc);