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 4 commits
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
6 changes: 5 additions & 1 deletion spec/std/io/io_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ describe IO do
ch = Channel(Symbol).new(1)

IO.pipe do |read, write|
spawn do
f = spawn do
ch.send :start
read.gets
rescue
Expand All @@ -838,6 +838,10 @@ describe IO do
delay(1) { ch.send :timeout }

ch.receive.should eq(:start)
while f.running?
# Wait until the fiber is blocked
Fiber.yield
end
read.close
ch.receive.should eq(:end)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/std/socket/udp_socket_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe UDPSocket do
sleep 100.milliseconds
udp.close
end
expect_raises_errno(Errno::EBADF, "Error receiving datagram: Bad file descriptor") { udp.receive }
expect_raises(IO::Error, "Closed stream") { udp.receive }
end
end
end
Expand Down
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
11 changes: 9 additions & 2 deletions src/crystal/system/unix/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ module Crystal::System::FileDescriptor
end

private def system_close
# Perform libevent cleanup before LibC.close.
# Using a file descriptor after it has been closed is never defined and can
# 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 All @@ -118,8 +127,6 @@ module Crystal::System::FileDescriptor
raise Errno.new("Error closing file")
end
end
ensure
evented_close
end

def self.pipe(read_blocking, write_blocking)
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: 4 additions & 0 deletions src/io/evented.cr
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ module IO::Evented
@read_timed_out = false
yield Timeout.new("Read timed out")
end

check_open
end

private def add_read_event(timeout = @read_timeout) : Nil
Expand All @@ -154,6 +156,8 @@ module IO::Evented
@write_timed_out = false
yield Timeout.new("Write timed out")
end

check_open
end

private def add_write_event(timeout = @write_timeout) : Nil
Expand Down
6 changes: 5 additions & 1 deletion src/io/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,11 @@ class IO::FileDescriptor < IO
private def unbuffered_close
return if @closed

system_close ensure @closed = true
# Set before the @closed state so the pending
# IO::Evented readers and writers can be cancelled
# knowing the IO is in a closed state.
@closed = true
system_close
end

private def unbuffered_flush
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
11 changes: 7 additions & 4 deletions src/socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class Socket < IO
if closed?
return
elsif Errno.value == Errno::EAGAIN
wait_readable
wait_readable rescue nil
Copy link

@jacobh0 jacobh0 Apr 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bcardiff shouldn't this be rescue return nil ? The loop doesn't break otherwise. accept? no longer times-out trying to accept a connection and just hangs forever

else
raise Errno.new("accept")
end
Expand Down Expand Up @@ -556,6 +556,12 @@ class Socket < IO
private def unbuffered_close
return if @closed

# Perform libevent cleanup before LibC.close.
# Using a file descriptor after it has been closed is never defined and can
# always lead to undefined results. This is not specific to libevent.
@closed = true
evented_close

err = nil
if LibC.close(@fd) != 0
case Errno.value
Expand All @@ -566,9 +572,6 @@ class Socket < IO
end
end

@closed = true
evented_close

raise err if err
end

Expand Down