Skip to content
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

When writing to STDOUT redirected to a closed pipe, no broken pipe #2532

Merged
merged 7 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Compatibility:
* Implement `Pathname#{empty?, glob}` (#2559, @bjfish)
* Fixed `Rational('')` to raise error like MRI (#2566, @aardvark179).
* Freeze instances of `Range` but not subclasses, like CRuby (#2570, @MattAlp).
* When writing to STDOUT redirected to a closed pipe, no broken pipe error message will be shown now. (#2532, @gogainda).

Performance:

Expand Down
24 changes: 24 additions & 0 deletions spec/ruby/core/io/write_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,27 @@
end
end
end

ruby_version_is "3.0" do
describe "IO#write on STDOUT" do
# https://bugs.ruby-lang.org/issues/14413
platform_is_not :windows do
it "raises SignalException SIGPIPE if the stream is closed instead of Errno::EPIPE like other IOs" do
stderr_file = tmp("stderr")
begin
IO.popen([*ruby_exe, "-e", "loop { puts :ok }"], "r", err: stderr_file) do |io|
io.gets.should == "ok\n"
io.close
end
status = $?
status.should_not.success?
status.should.signaled?
Signal.signame(status.termsig).should == 'PIPE'
File.read(stderr_file).should.empty?
ensure
rm_r stderr_file
end
end
end
end
end
1 change: 1 addition & 0 deletions spec/tags/core/io/write_tags.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
slow:IO.write on a FIFO writes correctly
slow:IO#write on STDOUT raises SignalException SIGPIPE if the stream is closed instead of Errno::EPIPE like other IOs
5 changes: 5 additions & 0 deletions src/main/ruby/truffleruby/core/posix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,11 @@ def self.write_string_native(io, string, continue_on_eagain)
return written
end
else
# stdout must raise a SIGPIPE SignalException instead of Errno::EPIPE
# https://bugs.ruby-lang.org/issues/14413
if fd == 1 and errno == Errno::EPIPE::Errno
raise SignalException, :SIGPIPE
end
Errno.handle_errno(errno)
end
end
Expand Down
1 change: 0 additions & 1 deletion test/mri/excludes/TestIO.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,4 @@
exclude :test_advise_pipe, "needs investigation"
exclude :test_fcntl_lock_linux, "needs investigation"
exclude :test_copy_stream_megacontent_nonblock, "needs investigation"
exclude :test_stdout_to_closed_pipe, "needs investigation"
exclude :test_select_memory_leak, "needs investigation"
1 change: 1 addition & 0 deletions test/mri/excludes/TestPipe.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
exclude :test_eof_0, "needs investigation"
exclude :test_eof_1, "needs investigation"
exclude :test_stdout_epipe, "needs investigation"
1 change: 1 addition & 0 deletions test/mri/excludes/TestPipe/WithConversion.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
exclude :test_eof_0, "needs investigation"
exclude :test_eof_1, "needs investigation"
exclude :test_stdout_epipe, "needs investigation"