- // Wait for a packet, or a wakeup.
- bool activity = wait_for_activity(sock, POLLIN, NULL);
- if (!activity) {
- // Most likely, should_stop was set.
+ // Since we are non-blocking, we need to wait for the right state first.
+ // Wait up to 50 ms, then check should_stop.
+ pollfd pfd;
+ pfd.fd = sock;
+ pfd.events = POLLIN;
+
+ int nfds = poll(&pfd, 1, 50);
+ if (nfds == 0 || (nfds == -1 && errno == EINTR)) {