Skip to content

Commit 79c6796

Browse files
Move make_enp before fork (#16351)
Pulls creating the `envp` out of the pre-exec stage. It can be prepared entirely before `fork`.
1 parent 33ed0ce commit 79c6796

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

spec/std/process_spec.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,10 @@ describe Process do
393393
end
394394

395395
it "errors on invalid key" do
396-
expect_raises({% if flag?(:win32) %}ArgumentError{% else %}RuntimeError{% end %}, %(Invalid env key "")) do
396+
expect_raises(ArgumentError, %(Invalid env key "")) do
397397
Process.run(*print_env_command, env: {"" => "baz"})
398398
end
399-
expect_raises({% if flag?(:win32) %}ArgumentError{% else %}RuntimeError{% end %}, %(Invalid env key "foo=bar")) do
399+
expect_raises(ArgumentError, %(Invalid env key "foo=bar")) do
400400
Process.run(*print_env_command, env: {"foo=bar" => "baz"})
401401
end
402402
end

src/crystal/system/unix/process.cr

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,16 @@ struct Crystal::System::Process
262262
def self.spawn(prepared_args, env, clear_env, input, output, error, chdir)
263263
r, w = FileDescriptor.system_pipe
264264

265+
envp = Env.make_envp(env, clear_env)
266+
265267
pid = fork(will_exec: true) do
266268
Crystal::System::Signal.after_fork_before_exec
267269
end
268270

269271
if !pid
270272
LibC.close(r)
271273
begin
272-
self.try_replace(prepared_args, env, clear_env, input, output, error, chdir)
274+
self.try_replace(prepared_args, envp, input, output, error, chdir)
273275
byte = 1_u8
274276
errno = Errno.value.to_i32
275277
FileDescriptor.write_fully(w, pointerof(byte))
@@ -337,12 +339,11 @@ struct Crystal::System::Process
337339
{pathname, argv.to_unsafe}
338340
end
339341

340-
private def self.try_replace(prepared_args, env, clear_env, input, output, error, chdir)
342+
private def self.try_replace(prepared_args, envp, input, output, error, chdir)
341343
reopen_io(input, ORIGINAL_STDIN)
342344
reopen_io(output, ORIGINAL_STDOUT)
343345
reopen_io(error, ORIGINAL_STDERR)
344346

345-
envp = Env.make_envp(env, clear_env)
346347
::Dir.cd(chdir) if chdir
347348

348349
execvpe(*prepared_args, envp)
@@ -456,7 +457,9 @@ struct Crystal::System::Process
456457
end
457458

458459
def self.replace(command, prepared_args, env, clear_env, input, output, error, chdir)
459-
try_replace(prepared_args, env, clear_env, input, output, error, chdir)
460+
envp = Env.make_envp(env, clear_env)
461+
462+
try_replace(prepared_args, envp, input, output, error, chdir)
460463
raise_exception_from_errno(command)
461464
end
462465

0 commit comments

Comments
 (0)