Project

General

Profile

Actions

Bug #22540

closed

Process.spawn leaves the env, umask, and redirections applied in the parent on Windows when chdir: fails

Bug #22540: Process.spawn leaves the env, umask, and redirections applied in the parent on Windows when chdir: fails

Added by jcouball@yahoo.com (James Couball) 4 days ago. Updated about 7 hours ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 4.0.6 (2026-07-14 revision 03b6d3f889) +PRISM [x64-mingw-ucrt]
[ruby-core:127451]

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 raises Errno::EACCES, where a missing one raises Errno::ENOENT. Either way, the env, umask, and redirections are left applied in the parent.
  • out: 99, where fd 99 isn't open, raises Errno::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: true to any of these leaves the parent's ENV holding only the variables the call passed.
  • Adding umask: 0o222 to the example above, or to the out: 99 case, leaves the parent's umask at 0200, up from 0000, and a file the parent creates afterward is read-only. A spawn with the same umask: 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.

Actions

Also available in: PDF Atom