Skip to content

Commit 4816b64

Browse files
committed
Fix HUP detection on macOS
As of macOS 15.4, passing 0 or POLLHUP doesn't seem to work at all for sockets any more (though it does work for `notifyPipe`). As a workaround, also pass POLLIN. That does cause us to receive a bunch of POLLIN events we don't care about, so we sleep for a bit when receiving POLLIN.
1 parent 79211b6 commit 4816b64

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/libutil/unix/include/nix/util/monitor-fd.hh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,16 @@ public:
5555
// https://github.com/apple-oss-distributions/xnu/commit/e13b1fa57645afc8a7b2e7d868fe9845c6b08c40#diff-a5aa0b0e7f4d866ca417f60702689fc797e9cdfe33b601b05ccf43086c35d395R1468
5656
// That means added in 2007 or earlier. Should be good enough
5757
// for us.
58+
//
59+
// Update: as of macOS 15.4, passing 0 or POLLHUP
60+
// doesn't seem to work at all for sockets any more
61+
// (though it does work for `notifyPipe`). As a
62+
// workaround, also pass POLLIN. That does cause us to
63+
// receive a bunch of POLLIN events we don't care
64+
// about, so we sleep for a bit when receiving POLLIN.
5865
short hangup_events =
5966
#ifdef __APPLE__
60-
POLLHUP
67+
POLLIN | POLLHUP
6168
#else
6269
0
6370
#endif
@@ -98,6 +105,12 @@ public:
98105
if (fds[1].revents & POLLHUP) {
99106
break;
100107
}
108+
if (fds[0].revents & POLLIN) {
109+
/* macOS only: we have to pass POLLIN to receive
110+
POLLHUP, but we don't care about POLLIN. To
111+
avoid a lot of wakeups, sleep for a bit. */
112+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
113+
}
101114
// On macOS, (jade thinks that) it is possible (although not
102115
// observed on macOS 14.5) that in some limited cases on buggy
103116
// kernel versions, all the non-POLLHUP events for the socket

0 commit comments

Comments
 (0)