Skip to content

Commit 3e3d293

Browse files
committed
Fix spawn issue when given :close option
1 parent fe736bb commit 3e3d293

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Bug fixes:
1212
* Fixed issue when extending FFI from File (#2094).
1313
* Fixed issue with `Kernel#freeze` not freezing singleton class (#2093).
1414
* Fixed `String#encode` with options issue (#2091, #2095, @LillianZ)
15+
* Fixed issue with `spawn` when `:close` redirect is used (#2097).
1516

1617
Compatibility:
1718

spec/ruby/core/process/spawn_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,15 @@ def child_pids(pid)
536536
File.read(@name).should == "glarkbang"
537537
end
538538

539+
it "closes STDERR in the child if :err => :close" do
540+
File.open(@name, 'w') do |file|
541+
-> do
542+
code = "begin; STDOUT.puts 'out'; STDERR.puts 'hello'; rescue => e; puts 'rescued'; end"
543+
Process.wait Process.spawn(ruby_cmd(code), :out => file, :err => :close)
544+
end.should output_to_fd("out\nrescued\n", file)
545+
end
546+
end
547+
539548
# :close_others
540549

541550
platform_is_not :windows do

spec/tags/core/process/spawn_tags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,4 @@ slow:Process.spawn inside Dir.chdir does not create extra process without chdir
8080
slow:Process.spawn inside Dir.chdir kills extra chdir processes
8181
slow:Process.spawn redirects both STDERR and STDOUT to the given file descriptor
8282
slow:Process.spawn defaults :close_others to false
83+
slow:Process.spawn closes STDERR in the child if :err => :close

src/main/ruby/truffleruby/core/truffle/process_operations.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,12 @@ def parse_options(options)
262262

263263
def redirect(from, to)
264264
map = (@options[:redirect_fd] ||= [])
265-
map << from << to
265+
if to.nil?
266+
@options[:fds_to_close] ||= []
267+
@options[:fds_to_close] << from
268+
else
269+
map << from << to
270+
end
266271
end
267272

268273
def convert_io_fd(obj)
@@ -491,6 +496,8 @@ def posix_spawnp(command, args, env_array, options)
491496
end
492497
when :chdir
493498
chdir = value
499+
when :fds_to_close
500+
value.each { |fd| fds_to_close << fd }
494501
else
495502
raise "Unknown spawn option: #{key}"
496503
end

0 commit comments

Comments
 (0)