Skip to content
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

Interpreter: fix fiber's resumable property #14252

Conversation

ysbaddaden
Copy link
Contributor

The resumable property of fiber contexts was wrong in interpreted code: it was never reset to 0.

This patch lets the interpreted fiber read the resumable property of the interpreter's fiber (the real one) though a new primitive. The interpreter is now responsible from reporting the value properly. This avoids having to manipulate resumable in the interpreted code, and allows to act on the actual state.

Alternative: the interpreter could set the resumable properties of the fibers in the interpreter_swapcontext primitive, it would be fake but probably not critical (the interpreter's scheduler would still act on the real property), but we don't have references back into the interpreted fibers (we could add it when interpreted) + I don't know how the interpreter updates values into an interpreted type (I could learn).

Fibers in the interpreted code are backed by real fibers run by the
interpreter. The fiber context is thus fake in the interpreted code, and
the resumable property should directly target the real fiber's context.

Also explains why we call `Fiber#resume`.
Copy link
Member

@beta-ziliani beta-ziliani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to have a test that should break in the current HEAD but work with this patch?

src/fiber/context.cr Outdated Show resolved Hide resolved
@Blacksmoke16 Blacksmoke16 added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:concurrency topic:compiler:interpreter labels Jan 23, 2024
The spec fails with the interpreter for the current crystal (1.11), but
succeeds with this patch (or when compiled with any older version).
src/fiber/context.cr Outdated Show resolved Hide resolved
Copy link
Contributor

@Sija Sija left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick

spec/std/fiber_spec.cr Outdated Show resolved Hide resolved
spec/std/fiber_spec.cr Outdated Show resolved Hide resolved
@ysbaddaden
Copy link
Contributor Author

@beta-ziliani I just added a test. It fails with the interpreter of crystal 1.11 but succeeds otherwise (this patch, or when compiled).

Co-authored-by: Sijawusz Pur Rahnama <sija@sija.pl>
src/fiber/context.cr Outdated Show resolved Hide resolved
Co-authored-by: Johannes Müller <straightshoota@gmail.com>
@beta-ziliani beta-ziliani added this to the 1.12.0 milestone Jan 30, 2024
@straight-shoota straight-shoota merged commit 14f69e3 into crystal-lang:master Jan 31, 2024
57 checks passed
@ysbaddaden ysbaddaden deleted the fix/set-resumable-in-interpreter-swapcontext branch February 1, 2024 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:interpreter topic:stdlib:concurrency
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants