-
Notifications
You must be signed in to change notification settings - Fork 138
Keep epoll interest alive while a duplicate of the registered fd survives #1230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Will Portnoy (willportnoy)
wants to merge
1
commit into
main
Choose a base branch
from
wportnoy/epoll-dup-survival
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+281
−43
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| // Tests: an epoll interest survives closing the registered fd as long as a | ||
| // duplicate referring to the same open file description remains open, matching | ||
| // Linux epoll(7) semantics ("a file descriptor is removed from an interest | ||
| // list only after all the file descriptors referring to the underlying open | ||
| // file description have been closed"). | ||
|
|
||
| #define _GNU_SOURCE | ||
| #include <errno.h> | ||
| #include <stdint.h> | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
| #include <sys/epoll.h> | ||
| #include <sys/eventfd.h> | ||
| #include <unistd.h> | ||
|
|
||
| #define TEST_ASSERT(cond, msg) \ | ||
| do { \ | ||
| if (!(cond)) { \ | ||
| fprintf(stderr, "FAIL: %s (line %d): %s (errno=%d: %s)\n", \ | ||
| __func__, __LINE__, msg, errno, strerror(errno)); \ | ||
| return 1; \ | ||
| } \ | ||
| } while (0) | ||
|
|
||
| int main(void) { | ||
| int efd = eventfd(0, EFD_CLOEXEC); | ||
| TEST_ASSERT(efd >= 0, "eventfd failed"); | ||
|
|
||
| int epfd = epoll_create1(EPOLL_CLOEXEC); | ||
| TEST_ASSERT(epfd >= 0, "epoll_create1 failed"); | ||
|
|
||
| struct epoll_event ev; | ||
| memset(&ev, 0, sizeof(ev)); | ||
| ev.events = EPOLLIN; | ||
| ev.data.u64 = 0x42; | ||
| TEST_ASSERT(epoll_ctl(epfd, EPOLL_CTL_ADD, efd, &ev) == 0, | ||
| "epoll_ctl ADD failed"); | ||
|
|
||
| // The duplicate shares the same open file description as efd. | ||
| int dupfd = dup(efd); | ||
| TEST_ASSERT(dupfd >= 0, "dup failed"); | ||
|
|
||
| // Close the originally-registered fd. The interest must survive because | ||
| // dupfd still refers to the same open file description. | ||
| TEST_ASSERT(close(efd) == 0, "close original failed"); | ||
|
|
||
| // Make the description readable through the surviving duplicate. | ||
| uint64_t one = 1; | ||
| TEST_ASSERT(write(dupfd, &one, sizeof(one)) == (ssize_t)sizeof(one), | ||
| "write via dup failed"); | ||
|
|
||
| // The registration must still be reported, carrying its original data. | ||
| struct epoll_event out[4]; | ||
| memset(out, 0, sizeof(out)); | ||
| int n = epoll_wait(epfd, out, 4, 1000); | ||
| TEST_ASSERT(n == 1, "epoll_wait should report the surviving registration"); | ||
| TEST_ASSERT((out[0].events & EPOLLIN) != 0, "expected EPOLLIN"); | ||
| TEST_ASSERT(out[0].data.u64 == 0x42, "event data mismatch"); | ||
|
|
||
| // The registration is durable: after draining and re-arming through the | ||
| // duplicate, a second wait still reports it. | ||
| uint64_t val = 0; | ||
| TEST_ASSERT(read(dupfd, &val, sizeof(val)) == (ssize_t)sizeof(val), | ||
| "read via dup failed"); | ||
| TEST_ASSERT(val == 1, "unexpected eventfd value"); | ||
| TEST_ASSERT(write(dupfd, &one, sizeof(one)) == (ssize_t)sizeof(one), | ||
| "second write via dup failed"); | ||
| memset(out, 0, sizeof(out)); | ||
| n = epoll_wait(epfd, out, 4, 1000); | ||
| TEST_ASSERT(n == 1, "epoll_wait should still report after re-arm"); | ||
| TEST_ASSERT(out[0].data.u64 == 0x42, "event data mismatch after re-arm"); | ||
|
|
||
| TEST_ASSERT(close(dupfd) == 0, "close dup failed"); | ||
| TEST_ASSERT(close(epfd) == 0, "close epoll failed"); | ||
|
|
||
| printf("epoll dup-survival: PASS\n"); | ||
| return 0; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One subtle issue with this lies on
litebox/litebox/src/fd/mod.rs
Lines 133 to 153 in e18e65a
Now
Arc::into_inner(old.x)...unwrap()may panic. Closing a socket cannot be done simply viaDrop. It needs to take the ownership of the entry. One potential fix suggested by copilot is to replace it withArc::try_unwrapand returnCloseResult::Duplicatedon the error path.