From a0f351c9fa731fc738ab2eeaa0bb23b20980f29f Mon Sep 17 00:00:00 2001 From: Andrew Chambers Date: Tue, 5 May 2020 16:03:13 +1200 Subject: [PATCH] Fix (mostly nonsensible) race condition in multi threaded processes using os/execute with os/setenv. --- src/core/os.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/core/os.c b/src/core/os.c index 70cd6288..9c5a80a9 100644 --- a/src/core/os.c +++ b/src/core/os.c @@ -389,15 +389,26 @@ static Janet os_execute(int32_t argc, Janet *argv) { char *const *cargv = (char *const *)child_argv; /* Use posix_spawn to spawn new process */ + + int use_environ = !janet_flag_at(flags, 0); + + if (use_environ) { + janet_lock_environ(); + } + pid_t pid; if (janet_flag_at(flags, 1)) { status = posix_spawnp(&pid, child_argv[0], NULL, NULL, cargv, - janet_flag_at(flags, 0) ? envp : environ); + use_environ ? environ : envp); } else { status = posix_spawn(&pid, child_argv[0], NULL, NULL, cargv, - janet_flag_at(flags, 0) ? envp : environ); + use_environ ? environ : envp); + } + + if (use_environ) { + janet_unlock_environ(); } /* Wait for child */