Skip to content

Close the shutdown pipe under the same lock that writes it - #236

Open
adityawrk wants to merge 2 commits into
ruby:masterfrom
adityawrk:fix/shutdown-pipe
Open

adityawrk wants to merge 2 commits into
ruby:masterfrom
adityawrk:fix/shutdown-pipe

Conversation

@adityawrk

@adityawrk adityawrk commented Nov 29, 2026 •

Copy link
Copy Markdown

Fixes #102.

stop writes one byte to the shutdown pipe. shutdown and the server thread both close that pipe, from different threads. The closed? check and the write were not atomic, so write_nonblock can run after close and hit EBADF, or write a recycled descriptor. CRuby’s IO#close hides this most of the time; TruffleRuby and JRuby do not.

The write and both closes now take @shutdown_pipe_mutex. The close runs only while @shutdown_pipe is still that pipe, and a second close sees closed?. A server that is started again does not have its new pipe closed by the previous cleanup.

ruby -Ilib:test/lib -rhelper test/webrick/test_server.rb: 8 tests, 23 assertions, 0 failures. Thirty threads calling stop and shutdown together raise nothing.

Several threads calling stop and shutdown together must not raise.
stop writes the pipe while shutdown and the server thread close it.
A write that lands after close uses a dead file descriptor. The write
and both closes now share one mutex, and a close runs only once.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WEBrick has an unsafe shutdown process it tries to concurrently write and close the @shutdown_pipe

1 participant