-
Notifications
You must be signed in to change notification settings - Fork 19
Fix subprocess.communicate("")
raising IOError: closed stream
#78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8e111bd
Add test for empty string stdin bug
qaisjp-stripe 01400eb
Fix empty string stdin bug in communicate method
qaisjp-stripe 449a104
Improve empty string stdin fix and tests
qaisjp-stripe 5557fff
Refactor stdin handling to use stdin_closed variable
qaisjp-stripe bf65d6e
Simplify stdin handling with unified logic
qaisjp-stripe 58683ee
rewrite but tests fail
qaisjp-stripe 9cfe1fc
rewrite
qaisjp-stripe 642b66f
Tweak comments
qaisjp-stripe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -436,13 +436,15 @@ def communicate(input=nil, timeout_s=nil) | |
# the input depending on how many bytes were written | ||
input = input.dup.force_encoding('BINARY') unless input.nil? | ||
|
||
@stdin.close if (input.nil? || input.empty?) && [email protected]? | ||
# Close stdin immediately if input is nil or empty | ||
@stdin.close if @stdin && (input.nil? || input.empty?) | ||
|
||
timeout_at = Time.now + timeout_s if timeout_s | ||
|
||
self.class.catching_sigchld(pid) do |global_read, self_read| | ||
wait_r = [@stdout, @stderr, self_read, global_read].compact | ||
wait_w = [input && @stdin].compact | ||
wait_w = @stdin&.closed? ? [] : [input && @stdin].compact | ||
qaisjp-stripe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
done = false | ||
while !done | ||
# If the process has exited, we want to drain any remaining output before returning | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
require 'rubygems' | ||
gem 'minitest' | ||
require 'minitest/autorun' | ||
require 'subprocess' | ||
|
||
describe Subprocess do | ||
describe "communicate with empty string input" do | ||
# Bug report: subprocess.communicate("") doesn't properly handle stdin, | ||
# causing it to close incorrectly and result in a broken pipe. | ||
it "should not raise IOError when passing empty string" do | ||
# Before the fix, this would raise: IOError: closed stream | ||
Subprocess.check_call(['cat'], | ||
stdin: Subprocess::PIPE, | ||
stdout: Subprocess::PIPE) do |p| | ||
stdout, stderr = p.communicate("") | ||
assert_equal("", stdout, "Empty input should produce empty output") | ||
assert_equal("", stderr, "No errors expected") | ||
end | ||
end | ||
|
||
it "should work correctly with non-empty string input" do | ||
test_input = "hello world" | ||
Subprocess.check_call(['cat'], | ||
stdin: Subprocess::PIPE, | ||
stdout: Subprocess::PIPE) do |p| | ||
stdout, stderr = p.communicate(test_input) | ||
assert_equal(test_input, stdout, "Input should be echoed back") | ||
assert_equal("", stderr, "No errors expected") | ||
end | ||
end | ||
|
||
it "should work correctly with nil input" do | ||
Subprocess.check_call(['cat'], | ||
stdin: Subprocess::PIPE, | ||
stdout: Subprocess::PIPE) do |p| | ||
stdout, stderr = p.communicate(nil) | ||
assert_equal("", stdout, "Nil input should produce empty output") | ||
assert_equal("", stderr, "No errors expected") | ||
end | ||
end | ||
|
||
it "should handle already closed stdin gracefully" do | ||
# Edge case: what if stdin is already closed? | ||
p = Subprocess.popen(['cat'], stdin: Subprocess::PIPE, stdout: Subprocess::PIPE) | ||
p.stdin.close | ||
stdout, stderr = p.communicate("") | ||
assert_equal("", stdout) | ||
assert_equal("", stderr) | ||
p.wait | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.