Repository navigation
Retry poll when interrupted by a signal #181
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
jattisha
wants to merge
2
commits into
kohlschutter:main
Choose a base branch
from
jattisha:fix-poll-eintr
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,76 @@ | ||
| /* | ||
| * junixsocket | ||
| * | ||
| * Copyright 2009-2026 Christian Kohlschütter | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #ifndef polling_with_deadline_h | ||
| #define polling_with_deadline_h | ||
|
|
||
| #include "config.h" | ||
|
|
||
| #if __TOS_MVS__ | ||
| #include "gettod_zos.h" | ||
| #endif | ||
|
|
||
| static int pollMonotonicTime(struct timespec* out) { | ||
| #if __TOS_MVS__ | ||
| return gettimeofdayMonotonic(out); | ||
| #else | ||
| return clock_gettime(CLOCK_MONOTONIC, out); | ||
| #endif | ||
| } | ||
|
|
||
| static uint64_t pollTimespecToMillis(const struct timespec* ts) { | ||
| return (uint64_t)ts->tv_sec * 1000 + (uint64_t)ts->tv_nsec / 1000000; | ||
| } | ||
|
|
||
| #if !defined(_WIN32) | ||
|
|
||
| static int pollWithDeadline(struct pollfd* pollFd, nfds_t nfds, int timeout) { | ||
| uint64_t deadlineMillis = 0; | ||
|
|
||
| if(timeout > 0) { | ||
| struct timespec now; | ||
| if(pollMonotonicTime(&now) == -1) { | ||
| return -1; | ||
| } | ||
| deadlineMillis = pollTimespecToMillis(&now) + (uint64_t)timeout; | ||
| } | ||
|
|
||
| for(;;) { | ||
| int ret = poll(pollFd, nfds, timeout); | ||
| if(ret != -1 || socket_errno != EINTR) { | ||
| return ret; | ||
| } | ||
|
|
||
| if(timeout > 0) { | ||
| struct timespec now; | ||
| if(pollMonotonicTime(&now) == -1) { | ||
| return -1; | ||
| } | ||
|
|
||
| uint64_t nowMillis = pollTimespecToMillis(&now); | ||
| if(nowMillis >= deadlineMillis) { | ||
| return 0; | ||
| } | ||
| timeout = (int)(deadlineMillis - nowMillis); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #endif /* !_WIN32 */ | ||
|
|
||
| #endif /* polling_with_deadline_h */ |
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,123 @@ | ||
| /* | ||
| * junixsocket | ||
| * | ||
| * Copyright 2009-2026 Christian Kohlschütter | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #if defined(_WIN32) | ||
|
|
||
| int main(void) { | ||
| return 0; | ||
| } | ||
|
|
||
| #else | ||
|
|
||
| #include <signal.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <sys/wait.h> | ||
| #include <unistd.h> | ||
|
|
||
| #include "../../main/c/polling_with_deadline.h" | ||
|
|
||
| static volatile sig_atomic_t signalReceived = 0; | ||
|
|
||
| static void signalHandler(int signalNumber CK_UNUSED) { | ||
| signalReceived = 1; | ||
| } | ||
|
|
||
| static void sleepMillis(long millis) { | ||
| struct timespec delay = { | ||
| .tv_sec = millis / 1000, | ||
| .tv_nsec = (millis % 1000) * 1000000, | ||
| }; | ||
| while(nanosleep(&delay, &delay) == -1 && errno == EINTR) { | ||
| } | ||
| } | ||
|
|
||
| static int pipeWriteFd = -1; | ||
|
|
||
| static void writeAfterDeadline(void) { | ||
| sleepMillis(2500); | ||
| char value = 1; | ||
| if(write(pipeWriteFd, &value, sizeof(value)) != sizeof(value)) { | ||
| _exit(EXIT_FAILURE); | ||
| } | ||
| } | ||
|
|
||
| static int waitForChild(pid_t child) { | ||
| int status; | ||
| while(waitpid(child, &status, 0) == -1) { | ||
| if(errno != EINTR) { | ||
| return -1; | ||
| } | ||
| } | ||
| return WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS ? 0 : -1; | ||
| } | ||
|
|
||
| int main(void) { | ||
| int pipeFds[2]; | ||
| if(pipe(pipeFds) == -1) { | ||
| perror("pipe"); | ||
| return EXIT_FAILURE; | ||
| } | ||
| pipeWriteFd = pipeFds[1]; | ||
|
|
||
| struct sigaction action = { | ||
| .sa_handler = signalHandler, | ||
| }; | ||
| sigemptyset(&action.sa_mask); | ||
| if(sigaction(SIGALRM, &action, NULL) == -1) { | ||
| perror("sigaction"); | ||
| return EXIT_FAILURE; | ||
| } | ||
|
|
||
| pid_t writerChild = fork(); | ||
| if(writerChild == -1) { | ||
| perror("fork"); | ||
| return EXIT_FAILURE; | ||
| } else if(writerChild == 0) { | ||
| writeAfterDeadline(); | ||
| _exit(EXIT_SUCCESS); | ||
| } | ||
|
|
||
| struct pollfd pollFd = { | ||
| .fd = pipeFds[0], | ||
| .events = POLLIN, | ||
| }; | ||
| alarm(1); | ||
| int ret = pollWithDeadline(&pollFd, 1, 2000); | ||
| alarm(0); | ||
|
|
||
| int childFailed = waitForChild(writerChild); | ||
| close(pipeFds[0]); | ||
| close(pipeFds[1]); | ||
|
|
||
| if(childFailed != 0) { | ||
| fprintf(stderr, "The writer child failed\n"); | ||
| return EXIT_FAILURE; | ||
| } | ||
| if(!signalReceived) { | ||
| fprintf(stderr, "SIGALRM was not received\n"); | ||
| return EXIT_FAILURE; | ||
| } | ||
| if(ret != 0) { | ||
| fprintf(stderr, "Expected timeout after EINTR, got poll result %d\n", ret); | ||
| return EXIT_FAILURE; | ||
| } | ||
| return EXIT_SUCCESS; | ||
| } | ||
|
|
||
| #endif |
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.
why is Windows excluded from this logic?
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.
This change addresses POSIX
poll()returningEINTR.WSAPolldoes not appear to document an equivalent interruption error (and I did use some AI to search through and I couldn't find an equivalent either). If there is an equivalent Windows case that should have the same behavior, I’m open to handling it here as well.