jnr / jnr-unixsocket

UNIX domain sockets (AF_UNIX) for Java
Apache License 2.0
278 stars 75 forks source link

read returns 0 for read when nonblock and no data is available #106

Open headius opened 1 month ago

headius commented 1 month ago

There's some peculiar behavior when read is called with no data available, for sure when nonblock but possibly also when blocking: it always just returns 0. I have traced it all the way to the libc call. The fix for jnr/jnr-enxio#44 does not appear to improve the behavior.

This prevents JRuby working with nonblocking UNIX sockets properly:

require 'io/nonblock'
require 'socket'
require 'tmpdir'

buffer = IO::Buffer.new(1000)

Dir.mktmpdir {|d|
  f = File.join(d, "foo")
  server = UNIXServer.new(f)
  client = UNIXSocket.new(f)
  client.nonblock {|client|
    p buffer.read(client, 0, 10)
  }
}

Outputs -35 on CRuby but 0 on JRuby.

headius commented 1 month ago

Note this requires the fiber_async branch currently in progress based on 9.5-dev and current master from this repository. A previous fix on the JRuby branch added a missing negation to the errno returned, which led to the discovery that unix sockets still always returned 0.