Skip to content
Closed
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
10 changes: 8 additions & 2 deletions io.c
Original file line number Diff line number Diff line change
Expand Up @@ -1181,8 +1181,14 @@ io_internal_wait(VALUE thread, rb_io_t *fptr, int error, int events, struct time
return -1;
}

errno = error;
return -1;
// If there was an error BEFORE we started waiting, return it:
if (error) {
errno = error;
return -1;
} else {
// Otherwise, whatever error was generated by `nogvl_wait_for` is the one we want:
return ready;
}
}

static VALUE
Expand Down
26 changes: 26 additions & 0 deletions test/ruby/test_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4351,4 +4351,30 @@ def test_stdout_to_closed_pipe
end
end
end

def test_blocking_timeout
assert_separately([], <<~'RUBY')
IO.pipe do |r, w|
trap(:INT) do
w.puts "INT"
end

main = Thread.current
thread = Thread.new do
# Wait until the main thread has entered `$stdin.gets`:
Thread.pass until main.status == 'sleep'

# Cause an interrupt while handling `$stdin.gets`:
Process.kill :INT, $$
end

r.timeout = 1
assert_equal("INT", r.gets.chomp)
rescue IO::TimeoutError
# Ignore - some platforms don't support interrupting `gets`.
ensure
thread&.join
end
RUBY
end
end
Loading