Don't inherit POSIX timers across fork - #2773
Open
emkey1 wants to merge 1 commit into
Open
Conversation
saagarjha
requested changes
Jul 29, 2026
saagarjha
left a comment
Member
There was a problem hiding this comment.
While we care less about commit messages this is clear enough that it doesn't really need a body
Comment on lines
+48
to
+53
| // POSIX timers are not inherited across fork. The struct copy above | ||
| // duplicated posix_timers[], including each entry's struct timer pointer, | ||
| // so the child saw the parent's timers as its own and timer_delete() in | ||
| // the child freed a timer the parent still owns and is still running. | ||
| for (int i = 0; i < TIMERS_MAX; i++) | ||
| group->posix_timers[i] = (struct posix_timer) {}; |
Member
There was a problem hiding this comment.
Suggested change
| // POSIX timers are not inherited across fork. The struct copy above | |
| // duplicated posix_timers[], including each entry's struct timer pointer, | |
| // so the child saw the parent's timers as its own and timer_delete() in | |
| // the child freed a timer the parent still owns and is still running. | |
| for (int i = 0; i < TIMERS_MAX; i++) | |
| group->posix_timers[i] = (struct posix_timer) {}; | |
| // POSIX timers are not inherited across fork | |
| for (int i = 0; i < TIMERS_MAX; i++) { | |
| group->posix_timers[i] = (struct posix_timer) {}; | |
| } |
emkey1
force-pushed
the
fix_fork_posix_timers
branch
from
July 29, 2026 18:15
e384d05 to
8d12a90
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
tgroup_copydoes*group = *old_group, which copiesposix_timers[]wholesale — including each entry'sstruct timer *. The child therefore starts life believing it owns the parent's timers, andtimer_deleteon an inherited id frees a timer object the parent still owns and whose callback is still armed.Linux does not inherit POSIX timers across
fork. (A thread clone shares the tgroup, so that case is already correct.) This clears the array in the copy.Testing
Repro: parent creates a
SIGEV_SIGNALtimer, arms it, forks; the child callstimer_deleteon the inherited id, and the parent then deletes its own.timer_delete(inherited)-1 EINVAL0(succeeds)-1 EINVALtimer_deletestill succeedsOne thing to note while you are looking at this: POSIX timers do not appear to deliver signals at all on master, fork or no fork — a
SIGEV_SIGNALtimer armed with a 20ms interval never fired in my testing, with no fork involved. That is a separate gap and this PR does not touch it, but it means the "parent's timer keeps firing" half of the story cannot currently be demonstrated. The double-free is independently reproducible as above.Also ran a shell smoke test over an Alpine rootfs (directory walks,
/procreads, fork/exec churn, redirects, pipes) with no change in behavior.🤖 Generated with Claude Code