From: "k0kubun (Takashi Kokubun) via ruby-core" Date: 2026-11-16T18:02:34+00:00 Subject: [ruby-core:127529] [Ruby Bug#22540] Process.spawn leaves the env, umask, and redirections applied in the parent on Windows when chdir: fails Issue #22540 has been updated by k0kubun (Takashi Kokubun). 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 commit:e7b01f90f556f852ad55d043d7ca22ee8440e175 merged revision(s) commit:ccaceb137d461e9f88f820886a4e94a0a0b59bf2. ---------------------------------------- Bug #22540: Process.spawn leaves the env, umask, and redirections applied in the parent on Windows when chdir: fails https://bugs.ruby-lang.org/issues/22540#change-119997 * Author: jcouball@yahoo.com (James Couball) * Status: Closed * ruby -v: ruby 4.0.6 (2026-07-14 revision 03b6d3f889) +PRISM [x64-mingw-ucrt] * Backport: 3.3: REQUIRED, 3.4: REQUIRED, 4.0: DONE ---------------------------------------- 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. ```ruby 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. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/