Repository navigation
fix: timer thread livelock caused by mutating active timer keys - #155
Open
HopeSuffers wants to merge 1 commit into
Open
HopeSuffers wants to merge 1 commit into
HopeSuffers wants to merge 1 commit into
Conversation
HopeSuffers
force-pushed
the
fix/timer-set-key-mutation
branch
from
September 20, 2026 10:33
713aeae to
0934637
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.
Should fix BanjoRecomp/BanjoRecomp#315.
timer_thread keeps its active timers in a std::set sorted by each timer's deadline, and it reads that deadline out of game memory every time it compares two of them:
if (a->timestamp != b->timestamp) {
return a->timestamp < b->timestamp;
}
The game writes to that same field every time it re-arms a timer, and osStopTimer only queues the removal for later. So the deadline changes while the timer is still sitting in the set, and the set ends up sorted by values that no longer match where things actually are.
Once that happens, begin() still hands you the timer, but erase() can't find it any more. It walks down the wrong branch and removes nothing. So the thread keeps pulling the same timer off the front: its deadline is in the past, so it never waits, fires it, and goes straight round again. The message queue fills up until the game is wedged. In a sample of a frozen build the timer thread has zero samples in the wait and ~1200 in the enqueue.
The fix is to stop reading the deadline out of game memory. Timers are keyed on a copy taken when they're armed, so the game can't change it behind our back.
Tested on an M4. The old code strands 432,884 timers in a 200k-operation soak, the new code strands none, and I played a debug build for 2h45m with no freeze and flat memory.