Fix: raise on manual fiber resume from sleep#15744
Merged
straight-shoota merged 4 commits intocrystal-lang:masterfrom May 8, 2025
Merged
Conversation
We can suspend a fiber by calling `sleep(time)` and we expect the fiber to only be resumed when the sleep time expires, but there is nothing preventing to enqueue the fiber (because of errors), which will unexpectedly resume the fiber early. This leads to at best errors (`wake_at` can't be nil) as well as segfaults because the event, allocated on the stack, is still in timers the sleep method returned, or to double resumes, ... This should usually not happen, unless there is a programming error in the runtime, but someone wants to use `sleep` as a timeout mechanism (it's not).
Collaborator
Author
|
Related: I think the This is missing a resume-once safety mechanism, as for the timeout action. Maybe we should introduce a general timeout mechanism for a sleep that can be safely resumed early, and it would allow to implement timeouts in sync primitives 🤔 |
ysbaddaden
commented
May 6, 2025
straight-shoota
approved these changes
May 8, 2025
1 task
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.
We can suspend a fiber by calling
sleep(time)and we expect the fiber to only be resumed when the sleep time expires, but there is nothing preventing to enqueue the fiber (because of errors), which will unexpectedly resume the fiber early.This leads to at best errors (
wake_atcan't be nil) as well as segfaults because the event, allocated on the stack, is still in timers the sleep method returned, or to double resumes, ...This should usually not happen, unless there is a programming error in the runtime, or someone wants to use
sleepas a timeout mechanism (it's not).I discovered this the hard way while implementing sync primitives. Having an explicit exception is much nicer than segfaults and timer related errors because
Fiber.yieldgot manually resumed.