Skip to content

Commit

Permalink
Fix: signal handler musn't depend on the event loop (#15325)
Browse files Browse the repository at this point in the history
Only a limited set of POSIX functions are signal safe, and the system functions that the event loop implementations can rely on isn't in the list (e.g. epoll, kevent, malloc, ...).

Now, the writer side of the pipe is blocking, so we should never reach a nonblocking case that would trigger an event loop wait, but going to the event loop may still be doing far too much or dangerous things: an event loop might not be available (e.g. bare thread) and it might be lazily allocated (signal unsafe).
  • Loading branch information
ysbaddaden authored Jan 13, 2025
1 parent 410a6c8 commit bb9df97
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/crystal/system/unix/signal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Crystal::System::Signal
action.sa_flags = LibC::SA_RESTART

action.sa_sigaction = LibC::SigactionHandlerT.new do |value, _, _|
writer.write_bytes(value) unless writer.closed?
FileDescriptor.write_fully(writer.fd, pointerof(value)) unless writer.closed?
end
LibC.sigemptyset(pointerof(action.@sa_mask))
LibC.sigaction(signal, pointerof(action), nil)
Expand Down

0 comments on commit bb9df97

Please sign in to comment.