]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-test: Fix race when subprocess immediately receives signal
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Tue, 24 Aug 2021 20:58:59 +0000 (22:58 +0200)
committerTimo Sirainen <timo.sirainen@open-xchange.com>
Tue, 24 Aug 2021 21:00:49 +0000 (23:00 +0200)
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.

src/lib-test/test-subprocess.c

index df0c8359d6b30421d05d72aa91696b6e02aa5894..bef12b1c453d8a8e258b6f63e1aa72fe52594660 100644 (file)
@@ -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();