Fix misindexing related to Gc.finalise_last#13502
Merged
shindere merged 2 commits intoocaml:trunkfrom Oct 7, 2024
Merged
Conversation
mshinwell
approved these changes
Sep 30, 2024
Contributor
|
This bug appears to have been present since finalisers were first added to the OCaml 5 runtime. |
2d9d851 to
6adeb4a
Compare
6adeb4a to
b01a008
Compare
Member
|
This fix looks like a good candidate for backporting to 5.2.1. Any counter-indication? |
Contributor
Author
|
(I'm not aware of any counter-indication.) |
kayceesrk
approved these changes
Oct 7, 2024
Contributor
kayceesrk
left a comment
There was a problem hiding this comment.
LGTM.
I confirmed that I had introduced the original bug. I'm a bit surprised that it got in in the first place and survived for this long. Thanks for fixing this.
dra27
pushed a commit
that referenced
this pull request
Oct 14, 2024
…last Fix misindexing related to `Gc.finalise_last` (cherry picked from commit 3e0a459)
dra27
pushed a commit
that referenced
this pull request
Oct 14, 2024
…last Fix misindexing related to `Gc.finalise_last` (cherry picked from commit 3e0a459)
Member
|
Cherry-picked for both 5.2.1 and 5.3.0 |
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.
The runtime exposes multiple ways for the users to register finalisers; one way is
Gc.finalise_last. The runtime occasionally checks whether any finaliser registered withfinalise_lastshould be run. This check first counts how much work it should do:ocaml/runtime/finalise.c
Lines 239 to 245 in e30d23f
And then adds work to the queue:
ocaml/runtime/finalise.c
Lines 261 to 276 in e30d23f
The indexing in the if-conditions are different, causing a bug. One way this bug can trigger: the first condition is true less often than the second condition, leading to the extra work enqueued (because the second condition was true) being dropped (because the length of the work queue is calculated from the number of times the first condition was true).
On my system, you can repro the issue with this program:
This program only prints
smallfor me. (Andlargewill never be printed for as long as the program runs.) The PR fixes the issue so that bothsmallandlargeare printed.