Bug #22540
closedProcess.spawn leaves the env, umask, and redirections applied in the parent on Windows when chdir: fails
Description
On Windows, when Process.spawn is given a chdir: directory that is missing or can't be entered, it raises as expected, but the environment, umask, and redirections intended for the child are left applied in the parent process.
begin
Process.spawn({ 'LEAK' => '1' }, 'cmd', '/c', 'exit', chdir: 'no-such-dir', out: File::NULL)
rescue SystemCallError
nil
end
p ENV['LEAK'] # => "1" on Windows; nil on Linux and macOS
$stdout.puts 'lost' # written to NUL, not the console
These related cases also leave the child's settings applied in the parent:
- A
chdir:directory the process can't enter raisesErrno::EACCES, where a missing one raisesErrno::ENOENT. Either way, the env, umask, and redirections are left applied in the parent. out: 99, where fd 99 isn't open, raisesErrno::EBADF. The env and umask are left applied in the parent. The redirections are not, because Ruby fails before it changes the standard streams.- Adding
unsetenv_others: trueto any of these leaves the parent'sENVholding only the variables the call passed. - Adding
umask: 0o222to the example above, or to theout: 99case, leaves the parent's umask at 0200, up from 0000, and a file the parent creates afterward is read-only. A spawn with the sameumask:that fails on a missing program leaves the umask unchanged. The umask cases were run on 3.1.7, 3.4.10, and 4.0.6.
Each failure also leaves open the copies save_redirect_fd made of the redirected descriptors. A loop of spawns that fail on chdir: with out: and err: redirected stops with Errno::EMFILE after about 4,090 iterations. The same loop with a missing program runs 6,000 times without error.
Reproduced on the x64-mingw-ucrt builds of 3.1.7, 3.4.2, 3.4.10, and 4.0.6. The code path is unchanged on master at 99fb833, and the same early return is in 2.0.0.
Without fork, rb_spawn_process calls rb_execarg_run_options(eargp, &sarg, ...), which applies env, umask, and fd redirections to the parent and records the old values in sarg, and then calls chdir. When a step fails, rb_execarg_run_options returns -1, and rb_spawn_process returns without calling rb_execarg_run_options(&sarg, NULL, ...) to restore them. The early return is the return -1 after the first rb_execarg_run_options call in rb_spawn_process, in process.c. A missing program fails after that point, so the restore runs and nothing leaks.
A possible fix is to restore from sarg before returning -1, as the success path does. sarg is zeroed at the start of rb_execarg_run_options and holds only what was applied before the failure, so restoring it undoes exactly that, and closes the saved descriptors it recorded in fd_close. The restore call writes to the same errmsg buffer and makes system calls that can change errno, so both need saving first, or the exception raised for the original failure changes.
This asks that a failed spawn restore the parent as a successful one does: the env, the umask, the redirections, and the saved descriptor copies. The temporary change to the parent during the spawn is not in question.