16

I'm looking for a way to interrupt an accept() call on a blocking socket. Using signals is not an option, as this is meant to be in a library and I don't want to clutter the user signals. Using select() is another option, buf for various reason it's not very appealing in my case.

What would work well, if possible, is to set the socket to non-blocking mode (using fcntl() and O_NONBLOCK) from another thread, while the socket is blocked on an accept() call. The expected behaviour is that the accept() call will return with EAGAIN or EWOULDBLOCK in errno.

Would it indeed work like that? Is it safe? Portable?

If you know about the applicability of this method to Windows (where you need to use WSAIoctl() and FONBIO), I'm also interested.

Norswap
  • 11,740
  • 12
  • 47
  • 60

2 Answers2

12

No idea about Windows, but the behavior you want is guaranteed by POSIX:

If the listen queue is empty of connection requests and O_NONBLOCK is not set on the file descriptor for the socket, accept() shall block until a connection is present. If the listen() queue is empty of connection requests and O_NONBLOCK is set on the file descriptor for the socket, accept() shall fail and set errno to [EAGAIN] or [EWOULDBLOCK].

Source: http://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html

Also, select or poll can be used to check for incoming connections by polling for the listening socket in the reading set.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • 1
    Are you sure an already blocking `accept()` would return as specified, if the file descriptor having been passed gets `O_NONBLOCK` set asynchronously? Your quote seems ambiguous to me for this case. – alk Oct 12 '12 at 15:15
  • 3
    Oh, I misunderstood your question. No, I don't think changing the mode to non-blocking while it's already blocked is portable. Why don't you just make it nonblocking to begin with, and use `poll` or `select` prior to the `accept` call, and only call `accept` if a connection is available? There are plenty of clean ways to make `select` or `poll` return early, like the self-pipe trick. – R.. GitHub STOP HELPING ICE Oct 12 '12 at 15:20
  • Okay, so the definite answer is that it's not up possible. Both in theory and practice (I ended up trying it out, both on Linux and Windows). – Norswap Oct 13 '12 at 23:08
  • On windows you should use OVERLAPPED io. For portability I would suggest using Boost Asio. – doron Mar 28 '20 at 21:57
  • On Windows (WinSock) you can actually just close the socket from another thread. This will unblock any threads blocking on that socket. – JamieB Nov 15 '21 at 17:51
  • @JamieB: That is not a safe operation ever, and carries with it the possibility of something else being opened on the same fd number, which the accept could continue on after interruption by a signal. – R.. GitHub STOP HELPING ICE Nov 15 '21 at 18:07
1

In the question, You are saying that you do not want to use select (or poll or epoll) which are the best ways for IO multiplexing. I would recommend you using one another thread just for listening sockets while this is a bad idea!

Amir Fo
  • 5,163
  • 1
  • 43
  • 51
  • 2
    Thanks for your answer, but the question was asked 8 years ago. In truth, I do not even remember what I was trying to achieve back then! – Norswap Mar 29 '20 at 23:28