-2

I am using UnixStream and I am not calling set_nonblocking(true). I thought a blocking socket would never return EWOULDBLOCK; is that true?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
user855
  • 19,048
  • 38
  • 98
  • 162
  • This really has nothing to do with Rust. UnixStream does nothing more than expose the underlying feature with a Rust API, and thus the question is "can a blocking socket return EWOULDBLOCK". – Masklinn Jul 28 '20 at 08:17
  • "can a blocking socket return EWOULDBLOCK" answer should be NO. Correct? – user855 Jul 28 '20 at 13:33
  • POSIX states that a socket can only return EAGAIN or EWOULDBLOCK for non-blocking sockets, so I believe you are correct. – Masklinn Jul 28 '20 at 17:22

2 Answers2

1

can a blocking socket return EWOULDBLOCK

It depends on the OS implementation, but NO for POSIX-compliant OSs and Linux.

From recv() of POSIX.1-2017:

[EAGAIN] or [EWOULDBLOCK]

The socket's file descriptor is marked O_NONBLOCK and no data is waiting to be received; or MSG_OOB is set and no out-of-band data is available and either the socket's file descriptor is marked O_NONBLOCK or the socket does not support blocking to await out-of-band data.

Linux is not POSIX-certified. But it's still NO. From recv(2):

EAGAIN or EWOULDBLOCK

The socket is marked nonblocking and the receive operation would block, or a receive timeout had been set and the timeout expired before data was received. POSIX.1 allows either error to be returned for this case, and does not require these constants to have the same value, so a portable application should check for both possibilities.

Hyeon Kim
  • 111
  • 5
0

The general answer is no, however I've seen two exceptions:

  • If you set a timeout on the socket with SO_RCVTIMEO or SO_SNDTIMEO - in this case, a receive or send will return with EWOULDBLOCK if the timeout elapses while no input data becomes available or the output buffer remains full, respectively.
  • If you call send/recv with the MSG_DONTWAIT flag, effectively turning the socket into a nonblocking one temporarily.

I don't know if UnixStream actually exposes the above functionalities, this is a POSIX question not a Rust one, and I'm not familiar with Rust.

itaych
  • 644
  • 5
  • 18