Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
monitor-fd.hh
Go to the documentation of this file.
1#pragma once
3
4#include <thread>
5#include <cassert>
6
7#include <poll.h>
8#include <errno.h>
9
10#ifdef __APPLE__
11# include <sys/types.h>
12# include <sys/event.h>
13#endif
14
15#include "nix/util/signals.hh"
18
19namespace nix {
20
21class MonitorFdHup
22{
23private:
24 std::thread thread;
25 Pipe notifyPipe;
26
27 void runThread(int watchFd, int notifyFd);
28
29public:
30 MonitorFdHup(int fd);
31 MonitorFdHup(MonitorFdHup &&) = delete;
32 MonitorFdHup(const MonitorFdHup &) = delete;
33 MonitorFdHup & operator=(MonitorFdHup &&) = delete;
34 MonitorFdHup & operator=(const MonitorFdHup &) = delete;
35
36 ~MonitorFdHup()
37 {
38 // Close the write side to signal termination via POLLHUP
39 notifyPipe.writeSide.close();
40 thread.join();
41 }
42};
43
44#ifdef __APPLE__
45/* This custom kqueue usage exists because Apple's poll implementation is
46 * broken and loses event subscriptions if EVFILT_READ fires without matching
47 * the requested `events` in the pollfd.
48 *
49 * We use EVFILT_READ, which causes some spurious wakeups (at most one per write
50 * from the client, in addition to the socket lifecycle events), because the
51 * alternate API, EVFILT_SOCK, doesn't work on pipes, which this is also used
52 * to monitor in certain situations.
53 *
54 * See (EVFILT_SOCK):
55 * https://github.com/netty/netty/blob/64bd2f4eb62c2fb906bc443a2aabf894c8b7dce9/transport-classes-kqueue/src/main/java/io/netty/channel/kqueue/AbstractKQueueChannel.java#L434
56 *
57 * See: https://git.lix.systems/lix-project/lix/issues/729
58 * Apple bug in poll(2): FB17447257, available at https://openradar.appspot.com/FB17447257
59 */
60inline void MonitorFdHup::runThread(int watchFd, int notifyFd)
61{
62 int kqResult = kqueue();
63 if (kqResult < 0) {
64 throw SysError("MonitorFdHup kqueue");
65 }
66 AutoCloseFD kq{kqResult};
67
68 std::array<struct kevent, 2> kevs;
69
70 // kj uses EVFILT_WRITE for this, but it seems that it causes more spurious
71 // wakeups in our case of doing blocking IO from another thread compared to
72 // EVFILT_READ.
73 //
74 // EVFILT_WRITE and EVFILT_READ (for sockets at least, where I am familiar
75 // with the internals) both go through a common filter which catches EOFs
76 // and generates spurious wakeups for either readable/writable events.
77 EV_SET(&kevs[0], watchFd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, nullptr);
78 EV_SET(&kevs[1], notifyFd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, nullptr);
79
80 int result = kevent(kq.get(), kevs.data(), kevs.size(), nullptr, 0, nullptr);
81 if (result < 0) {
82 throw SysError("MonitorFdHup kevent add");
83 }
84
85 while (true) {
86 struct kevent event;
87 int numEvents = kevent(kq.get(), nullptr, 0, &event, 1, nullptr);
88 if (numEvents < 0) {
89 throw SysError("MonitorFdHup kevent watch");
90 }
91
92 if (numEvents > 0 && (event.flags & EV_EOF)) {
93 if (event.ident == uintptr_t(watchFd)) {
94 unix::triggerInterrupt();
95 }
96 // Either watched fd or notify fd closed, exit
97 return;
98 }
99 }
100}
101#else
102inline void MonitorFdHup::runThread(int watchFd, int notifyFd)
103{
104 while (true) {
105 struct pollfd fds[2];
106 fds[0].fd = watchFd;
107 fds[0].events = 0; // POSIX: POLLHUP is always reported
108 fds[1].fd = notifyFd;
109 fds[1].events = 0;
110
111 auto count = poll(fds, 2, -1);
112 if (count == -1) {
113 if (errno == EINTR || errno == EAGAIN) {
114 continue;
115 } else {
116 throw SysError("in MonitorFdHup poll()");
117 }
118 }
119
120 if (fds[0].revents & POLLHUP) {
121 unix::triggerInterrupt();
122 break;
123 }
124
125 if (fds[1].revents & POLLHUP) {
126 // Notify pipe closed, exit thread
127 break;
128 }
129 }
130}
131#endif
132
133inline MonitorFdHup::MonitorFdHup(int fd)
134{
135 notifyPipe.create();
136 int notifyFd = notifyPipe.readSide.get();
137 thread = std::thread([this, fd, notifyFd]() { this->runThread(fd, notifyFd); });
138};
139
140} // namespace nix
Definition file-descriptor.hh:331
Definition system-error.hh:283
File descriptor operations for almost arbitrary file descriptors.
Errors from the operating system: SystemError, and its subclasses for the native error codes,...