Skip to content

Commit

Permalink
Fix: signal handler musn't depend on the event loop
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, ...).
  • Loading branch information
ysbaddaden committed Jan 7, 2025
1 parent ba15364 commit f131156
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/crystal/system/unix/signal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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 Expand Up @@ -85,7 +85,9 @@ module Crystal::System::Signal
private def self.start_loop
spawn(name: "signal-loop") do
loop do
value = reader.read_bytes(Int32)
buf = uninitialized StaticArray(UInt8, 4)
reader.read_fully(buf.to_slice)
value = buf.unsafe_as(Int32)
rescue IO::Error
next
else
Expand Down

0 comments on commit f131156

Please sign in to comment.