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.
Updated by jcouball@yahoo.com (James Couball) 5 days ago
- ruby -v set to ruby 4.0.6 (2026-07-14 revision 03b6d3f889) +PRISM [x64-mingw-ucrt]
Updated by nobu (Nobuyoshi Nakada) 4 days ago
- Backport changed from 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN to 3.3: REQUIRED, 3.4: REQUIRED, 4.0: REQUIRED
Updated by nobu (Nobuyoshi Nakada) 4 days ago
- Status changed from Open to Closed
Applied in changeset git|ccaceb137d461e9f88f820886a4e94a0a0b59bf2.
[Bug #22540] Restore process options after chdir failure
Restore parent process state when spawn options fail before starting
the child process, on no-fork platforms.
Updated by k0kubun (Takashi Kokubun) 1 day ago
- Backport changed from 3.3: REQUIRED, 3.4: REQUIRED, 4.0: REQUIRED to 3.3: REQUIRED, 3.4: REQUIRED, 4.0: DONE
ruby_4_0 e7b01f90f556f852ad55d043d7ca22ee8440e175 merged revision(s) ccaceb137d461e9f88f820886a4e94a0a0b59bf2.
Updated by nagachika (Tomoyuki Chikanaga) about 13 hours ago
- Backport changed from 3.3: REQUIRED, 3.4: REQUIRED, 4.0: DONE to 3.3: REQUIRED, 3.4: DONE, 4.0: DONE
ruby_3_4 e54de060dc2d647e402239809ea6f3e6a923879e merged revision(s) ccaceb137d461e9f88f820886a4e94a0a0b59bf2.