daemon: remove workaround for macOS kernel bug that seems fixed

This was filed as https://github.com/nixos/nix/issues/7584, but as far
as I can tell, the previous solution of POLLHUP works just fine on macOS
14. I've also tested on an ancient machine with macOS 10.15.7, which
also has POLLHUP work correctly.

It's possible this might regress some older versions of macOS that have
a kernel bug, but I went looking through the history on the sources and
didn't find anything that looked terribly convincingly like a bug fix
between 2020 and today. If such a broken version exists, it seems pretty
reasonable to suggest simply updating the OS.

Change-Id: I178a038baa000f927ea2cbc4587d69d8ab786843
This commit is contained in:
jade 2024-07-13 00:27:09 +02:00
parent b3fb8d9822
commit 69e2ee5b25

View file

@ -38,26 +38,17 @@ public:
/* Wait indefinitely until a POLLHUP occurs. */ /* Wait indefinitely until a POLLHUP occurs. */
struct pollfd fds[2]; struct pollfd fds[2];
fds[0].fd = fd; fds[0].fd = fd;
/* Polling for no specific events (i.e. just waiting // There is a POSIX violation on macOS: you have to listen for
for an error/hangup) doesn't work on macOS // at least POLLHUP to receive HUP events for a FD. POSIX says
anymore. So wait for read events and ignore // this is not so, and you should just receive them regardless,
them. */ // however, as of our testing on macOS 14.5, the events do not
// FIXME(jade): we have looked at the XNU kernel code and as // get delivered in such a case.
// far as we can tell, the above is bogus. It should be the
// case that the previous version of this and the current
// version are identical: waiting for POLLHUP and POLLRDNORM in
// the kernel *should* be identical.
// https://github.com/apple-oss-distributions/xnu/blob/94d3b452840153a99b38a3a9659680b2a006908e/bsd/kern/sys_generic.c#L1751-L1758
// //
// So, this needs actual testing and we need to figure out if // This is allegedly filed as rdar://37537852.
// this is actually bogus. //
fds[0].events = // Relevant code, which backs this up:
#ifdef __APPLE__ // https://github.com/apple-oss-distributions/xnu/blob/94d3b452840153a99b38a3a9659680b2a006908e/bsd/kern/sys_generic.c#L1751-L1758
POLLRDNORM fds[0].events = POLLHUP;
#else
0
#endif
;
fds[1].fd = terminateFd; fds[1].fd = terminateFd;
fds[1].events = POLLIN; fds[1].events = POLLIN;
@ -82,14 +73,16 @@ public:
triggerInterrupt(); triggerInterrupt();
break; break;
} }
// No reason to actually look at the pipe FD, we just need it // No reason to actually look at the pipe FD if that's what
// to be able to get woken. // woke us, the only thing that actually matters is the quit
// flag.
if (quit_) { if (quit_) {
break; break;
} }
/* This will only happen on macOS. We sleep a bit to // On macOS, it is possible (although not observed on macOS
avoid waking up too often if the client is sending // 14.5) that in some limited cases on buggy kernel versions,
input. */ // all the non-POLLHUP events for the socket get delivered.
// Sleeping avoids pointlessly spinning a thread on those.
sleep(1); sleep(1);
} }
}); });
@ -100,7 +93,9 @@ public:
quit = true; quit = true;
// Poke the thread out of its poll wait // Poke the thread out of its poll wait
writeFull(terminatePipe.writeSide.get(), "*", false); writeFull(terminatePipe.writeSide.get(), "*", false);
thread.join(); if (thread.joinable()) {
thread.join();
}
} }
}; };