Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IO: Detect closed IO when resuming readable/writable event #8707

Merged
merged 5 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions src/crystal/system/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ module Crystal::System::File

oflag = m | o
end

# Closes the internal file descriptor without notifying libevent.
# This is directly used after the fork of a process to close the
# parent's Crystal::Signal.@@pipe reference before re initializing
# the event loop. In the case of a fork that will exec there is even
# no need to initialize the event loop at all.
# def file_description_close
RX14 marked this conversation as resolved.
Show resolved Hide resolved
end

{% if flag?(:unix) %}
Expand Down
4 changes: 4 additions & 0 deletions src/crystal/system/unix/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ module Crystal::System::FileDescriptor
# always lead to undefined results. This is not specific to libevent.
evented_close

file_description_close
end

def file_description_close
bcardiff marked this conversation as resolved.
Show resolved Hide resolved
if LibC.close(@fd) != 0
case Errno.value
when Errno::EINTR, Errno::EINPROGRESS
Expand Down
4 changes: 4 additions & 0 deletions src/crystal/system/win32/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ module Crystal::System::FileDescriptor
end

private def system_close
file_description_close
end

def file_description_close
err = nil
if LibC._close(@fd) != 0
case Errno.value
Expand Down
4 changes: 2 additions & 2 deletions src/signal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ module Crystal::Signal
# Replaces the signal pipe so the child process won't share the file
# descriptors of the parent process and send it received signals.
def self.after_fork
@@pipe.each(&.close)
@@pipe.each(&.file_description_close)
ensure
@@pipe = IO.pipe(read_blocking: false, write_blocking: true)
end
Expand All @@ -243,7 +243,7 @@ module Crystal::Signal
end
ensure
{% unless flag?(:preview_mt) %}
@@pipe.each(&.close)
@@pipe.each(&.file_description_close)
{% end %}
end

Expand Down