Skip to content

Commit

Permalink
Fix Process.run with closed IO
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Jun 11, 2024
1 parent 5fd4595 commit 032a22f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions spec/std/process_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ pending_interpreted describe: Process do
$?.exit_code.should eq(0)
end

it "forwards closed io" do
closed_io = IO::Memory.new
closed_io.close
Process.run(*stdin_to_stdout_command, input: closed_io)
Process.run(*stdin_to_stdout_command, output: closed_io)
Process.run(*stdin_to_stdout_command, error: closed_io)
end

it "sets working directory with string" do
parent = File.dirname(Dir.current)
command = {% if flag?(:win32) %}
Expand Down
5 changes: 5 additions & 0 deletions src/crystal/system/unix/process.cr
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ struct Crystal::System::Process
end

private def self.reopen_io(src_io : IO::FileDescriptor, dst_io : IO::FileDescriptor)
if src_io.closed?
dst_io.close
return
end

src_io = to_real_fd(src_io)

dst_io.reopen(src_io)
Expand Down
8 changes: 8 additions & 0 deletions src/process.cr
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,14 @@ class Process
when IO::FileDescriptor
stdio
when IO
if stdio.closed?
if dst_io == STDIN
return File.open(File::NULL, "r").tap(&.close)
else
return File.open(File::NULL, "w").tap(&.close)
end
end

if dst_io == STDIN
fork_io, process_io = IO.pipe(read_blocking: true)

Expand Down

0 comments on commit 032a22f

Please sign in to comment.