Skip to content

Commit 9a55fc5

Browse files
rwstaunermatzbot
authored andcommitted
[ruby/rubygems] Close stdin immediately when using popen2e
It's good hygiene to close the stdin pipe as soon as you are done writing to it. This can happen for example if "ruby extconf.rb" spawns another process (for example "cargo build", which may spawn arbitrary commands to fetch credentials) and any of those subprocesses attempt to read STDIN until it is closed. We can close it as soon as it's created since we aren't writing to it at all. ruby/rubygems@ab09bfdf10
1 parent ec106b1 commit 9a55fc5

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)