Skip to content

Commit 5869518

Browse files
authored
Merge pull request #9540 from Shopify/rwstauner/stdin-close
Close stdin immediately when using popen2e
2 parents fcc03ef + ab09bfd commit 5869518

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

lib/rubygems/ext/builder.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ def self.run(command, results, command_name = nil, dir = Dir.pwd, env = {})
102102
# Set $SOURCE_DATE_EPOCH for the subprocess.
103103
build_env = { "SOURCE_DATE_EPOCH" => Gem.source_date_epoch_string }.merge(env)
104104
output, status = begin
105-
Open3.popen2e(build_env, *command, chdir: dir) do |_stdin, stdouterr, wait_thread|
105+
Open3.popen2e(build_env, *command, chdir: dir) do |stdin, stdouterr, wait_thread|
106+
stdin.close
106107
output = String.new
107108
while line = stdouterr.gets
108109
output << line

test/rubygems/test_gem_ext_builder.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,22 @@ def test_custom_make_with_options
106106
assert_match(/install: OK/, results)
107107
end
108108

109+
def test_class_run_closes_stdin
110+
results = []
111+
check_stdin_script = <<~'RUBY'
112+
if IO.select([STDIN], nil, nil, 1)
113+
puts "STDIN: #{STDIN.read.inspect}"
114+
else
115+
puts "NOT_READY"
116+
end
117+
RUBY
118+
119+
Gem::Ext::Builder.run([Gem.ruby, "-e", check_stdin_script], results)
120+
121+
command_output = results.last
122+
assert_equal "STDIN: \"\"\n", command_output
123+
end
124+
109125
def test_build_extensions
110126
pend "terminates on mswin" if vc_windows? && ruby_repo?
111127

0 commit comments

Comments
 (0)