From: Timo Sirainen Date: Tue, 24 Aug 2021 20:58:59 +0000 (+0200) Subject: lib-test: Fix race when subprocess immediately receives signal X-Git-Tag: 2.3.17~170 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8a7fa002d2775d83ef43ac9bd5fd79f60b8b3db1;p=thirdparty%2Fdovecot%2Fcore.git lib-test: Fix race when subprocess immediately receives signal Signal could be received before test_subprocess_is_child=1 is set, causing the subprocess's signal handler to also attempt to cleanup other subprocesses. This was causing http-test-client-errors unit tests to fail somewhat randomly, especially when running them only with 1 CPU. --- diff --git a/src/lib-test/test-subprocess.c b/src/lib-test/test-subprocess.c index df0c8359d6..bef12b1c45 100644 --- a/src/lib-test/test-subprocess.c +++ b/src/lib-test/test-subprocess.c @@ -89,15 +89,18 @@ void test_subprocess_fork(int (*func)(void *context), void *context, lib_signals_ioloop_detach(); + /* avoid races: fork the child process with test_subprocess_is_child + set to 1 in case it immediately receives a signal. */ + test_subprocess_is_child = 1; if ((subprocess->pid = fork()) == (pid_t)-1) i_fatal("test: sub-process: fork() failed: %m"); if (subprocess->pid == 0) { - test_subprocess_is_child = 1; test_subprocess_free_all(); test_subprocess_child(func, context, continue_test); i_unreached(); } + test_subprocess_is_child = 0; array_push_back(&test_subprocesses, &subprocess); lib_signals_ioloop_attach();