GeezerVison is showing github.com — Epoll: Correctly delete fd from epoll if there is nothing to handle (… · netty/netty@0ec3d97
File tree
Expand file treeCollapse file tree
Open diff view settings
transport-classes-epoll/src/main/java/io/netty/channel/epoll
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: transport-classes-epoll/src/main/java/io/netty/channel/epoll/EpollIoHandler.java +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -261,9 +261,21 @@ public long submit(IoOps ops) { |
261 | 261 | case Cancelled: |
262 | 262 | return -1; |
263 | 263 | case Pending: |
| 264 | + if (epollIoOps.value == EpollIoOps.NONE.value) { |
| 265 | + // 0 is a special value that basically means we should remove the registration. |
| 266 | + // As we did not add the fd yet we should just return. |
| 267 | + return 0; |
| 268 | + } |
264 | 269 | Native.epollCtlAdd(epollFd.intValue(), handle.fd().intValue(), epollIoOps.value); |
265 | 270 | state = RegistrationState.Added; |
| 271 | + return epollIoOps.value; |
266 | 272 | case Added: |
| 273 | + if (epollIoOps.value == EpollIoOps.NONE.value) { |
| 274 | + // 0 means there is nothing to handle anymore, unregister the fd as otherwise |
| 275 | + // we might get notified forever because of EPOLLHUP / EPOLLERR. |
| 276 | + Native.epollCtlDel(epollFd.intValue(), handle.fd().intValue()); |
| 277 | + return 0; |
| 278 | + } |
267 | 279 | Native.epollCtlMod(epollFd.intValue(), handle.fd().intValue(), epollIoOps.value); |
268 | 280 | return epollIoOps.value; |
269 | 281 | default: |
|
Collapse file
Copy file name to clipboardExpand all lines: transport-classes-epoll/src/main/java/io/netty/channel/epoll/EpollIoOps.java +8-1Lines changed: 8 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -51,6 +51,12 @@ public final class EpollIoOps implements IoOps { |
51 | 51 |
|
52 | 52 | public static final EpollIoOps EPOLLET = new EpollIoOps(Native.EPOLLET); |
53 | 53 |
|
| 54 | + /** |
| 55 | + * Special {@link EpollIoOps} which basically means we are not interested in any event and so should remove the |
| 56 | + * fd from underlying epoll fd. |
| 57 | + */ |
| 58 | + public static final EpollIoOps NONE = new EpollIoOps(0); |
| 59 | + |
54 | 60 | static final int EPOLL_ERR_OUT_MASK = EpollIoOps.EPOLLERR.value | EpollIoOps.EPOLLOUT.value; |
55 | 61 | static final int EPOLL_ERR_IN_MASK = EpollIoOps.EPOLLERR.value | EpollIoOps.EPOLLIN.value; |
56 | 62 | static final int EPOLL_RDHUP_MASK = EpollIoOps.EPOLLRDHUP.value; |
@@ -60,7 +66,8 @@ public final class EpollIoOps implements IoOps { |
60 | 66 |
|
61 | 67 | static { |
62 | 68 | EpollIoOps all = new EpollIoOps(EPOLLOUT.value | EPOLLIN.value | EPOLLERR.value | EPOLLRDHUP.value); |
63 | | - EVENTS = new EpollIoEvent[all.value + 1]; |
| 69 | + EVENTS = new EpollIoEvent[all.value + 2]; |
| 70 | + addToArray(EVENTS, NONE); |
64 | 71 | addToArray(EVENTS, EPOLLOUT); |
65 | 72 | addToArray(EVENTS, EPOLLIN); |
66 | 73 | addToArray(EVENTS, EPOLLERR); |
|