socketry / io-event

MIT License
75 stars 16 forks source link

File IO with KQueue crashes with a TypeError #16

Closed rawrafox closed 2 years ago

rawrafox commented 2 years ago
#!/usr/bin/env ruby

$LOAD_PATH << "ext"

require_relative 'lib/io/event'

require 'fiber'

require 'io/nonblock'
selector = IO::Event::Selector::KQueue.new(Fiber.current)

events = Array.new
io = File.open("reproduction.rb")

fiber = Fiber.new do
    events << :wait_readable

    selector.io_wait(Fiber.current, io, IO::READABLE)

    events << :readable
end

events << :transfer
fiber.transfer
events << :transfer
fiber.transfer

pp events == [:transfer, :wait_readable, :transfer, :readable]

This crashes with no implicit conversion from nil to integer (TypeError), and while you can just workaround it with a if (NIL_P(result)) … at https://github.com/socketry/io-event/blob/main/ext/io/event/selector/kqueue.c#L351 I am not sure if that is the best solution?

ioquatix commented 2 years ago

Thanks for this detailed report and spending time investigating the implementation. I'll take a look and hope to fix this tonight. I also think it's funny I used the one file /dev/zero that has different semantics from every other file. Haha.

rawrafox commented 2 years ago

Yeah, that and /dev/null were the only reasonable ones I tried that it failed on. TTYs and other actually useful devices work fine.