diff options
| author | KJ Tsanaktsidis <ktsanaktsidis@zendesk.com> | 2024-09-13 17:40:30 +1000 |
|---|---|---|
| committer | Takashi Kokubun <takashikkbn@gmail.com> | 2024-09-23 09:25:10 -0700 |
| commit | 5b6009870dff883a8e71a05e60f175cea1d00d55 (patch) | |
| tree | f6726cb7f3a00e8967a789a0264ce2dfe5274ff4 /test | |
| parent | 4e59e7d35fbd6ff87f63cd0aa5d6a2f923323fee (diff) | |
Ensure fiber scheduler is woken up when close interrupts read
If one thread is reading and another closes that socket, the close
blocks waiting for the read to abort cleanly. This ensures that Ruby is
totally done with the file descriptor _BEFORE_ we tell the OS to close
and potentially re-use it.
When the read is correctly terminated, the close should be unblocked.
That currently works if closing is happening on a thread, but if it's
happening on a fiber with a fiber scheduler, it does NOT work.
This patch ensures that if the close happened in a fiber scheduled
thread, that the scheduler is notified that the fiber is unblocked.
[Bug #20723]
Diffstat (limited to 'test')
| -rw-r--r-- | test/fiber/test_io.rb | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/test/fiber/test_io.rb b/test/fiber/test_io.rb index 0e3e086d5a..7973399acb 100644 --- a/test/fiber/test_io.rb +++ b/test/fiber/test_io.rb @@ -234,4 +234,47 @@ class TestFiberIO < Test::Unit::TestCase assert_equal "ok\n", result end + + # Tests for https://bugs.ruby-lang.org/issues/20723 which would + # otherwise deadlock this test. + def test_close_while_reading_on_thread + # Windows has UNIXSocket, but only with VS 2019+ + omit "UNIXSocket is not defined!" unless defined?(UNIXSocket) + + i, o = Socket.pair(:UNIX, :STREAM) + if RUBY_PLATFORM=~/mswin|mingw/ + i.nonblock = true + o.nonblock = true + end + + message = nil + + reading_thread = Thread.new do + Thread.current.report_on_exception = false + i.wait_readable + end + + fs_thread = Thread.new do + # Wait until the reading thread is blocked on read: + Thread.pass until reading_thread.status == "sleep" + + scheduler = Scheduler.new + Fiber.set_scheduler scheduler + Fiber.schedule do + i.close + end + end + + assert_raise(IOError) { reading_thread.join } + refute_nil fs_thread.join(5), "expected thread to terminate within 5 seconds" + + assert_predicate(i, :closed?) + ensure + fs_thread&.kill + fs_thread&.join rescue nil + reading_thread&.kill + reading_thread&.join rescue nil + i&.close + o&.close + end end |
