MarkR
MarkR

Reputation: 63548

Does a socket shutdown call from another thread always make blocking recv() threads wake up?

I can't find much documentation to say whether this is supposed to happen or not:

  1. Some thread opens a TCP (or other stream) socket
  2. Thread 1 starts a blocking recv()
  3. Thread 2 calls shutdown() on the socket with SHUT_RDWR (or SHUT_RD I think)
  4. Thread 1 is now "woken up" from its blocking call, and returns zero, as it would if the other party closed its socket.

This behaviour appears on modern Linux and FreeBSD systems. I haven't tested it with any others.

A comment on a Microsoft MSDN help page here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms740481%28v=vs.85%29.aspx suggests that this behaviour is "responsible" in Windows; it also states that this is "not currently the case" but this may be out of date.

Is this behaviour specified anywhere? Can I rely on it?

Upvotes: 3

Views: 649

Answers (1)

ArtemB
ArtemB

Reputation: 3622

I don't think you can rely on it. shutdown() initiales socket shutdown, but the details depend on particular circumstances. Some protocols may indeed close connection and socket immediately which would wake up processes sleeping on that socket. In other cases, shutdown just kicks protocol state machine into action, but it would take some time until it would get to the point where it would make sense to wake up anyone. For instance, established TCP connection would have to transition through few states until it reaches CLOSED state. You will eventually wake up, but you can't rely on it happening right away.

Upvotes: 1

Related Questions