Skip to content

Update MacOS and Fix Async Cont get cancellation race - #4713

Open
reardonj wants to merge 2 commits into
typelevel:series/3.7.xfrom
reardonj:cont-get-race
Open

reardonj wants to merge 2 commits into
typelevel:series/3.7.xfrom
reardonj:cont-get-race

Conversation

@reardonj

@reardonj reardonj commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Fixes a very narrow race condition when trying to get in an finalizer in a Cont. The implementation in IOFiber:

  1. changed the cont state to waiting
  2. concurrently, a running callback might check if the state is waiting (in stateLoop), see that it is, loop(), fail to resume, see the finalizing states disagree, and exit. The callback value is lost.
  3. back in the fiber thread, it would update wasFinalizing on the cont state and wait for a callback that already terminated

Now, wasFinalizing is set first. This ordering avoids the race.


This is probably quite obscure, but I hit this issue while working on #4710 , as I want to get after cancellation there.

EDIT: fromCompletableFuture actually does use this pattern. Maybe #4469 is related. I also hit this bug on a separate PR trying to update the MacOS build, so I have folded the two together.

@reardonj
reardonj force-pushed the cont-get-race branch 2 times, most recently from 1bc8e90 to cd35ac1 Compare November 6, 2026 23:35
Fixes a very narrow race condition when trying to `get` in an finalizer in a `Cont`. The implementation in IOFiber:

1. changed the cont state to waiting
2. concurrently, a running callback might check if the state is "waiting" (in `stateLoop`), see that it is, `loop()`, fail to resume, see the finalizing states disagree, and exit. The callback value is lost.
3. back in the fiber thread, it would update `wasFinalizing` on the cont state and wait for a callback that already terminated

Now, `wasFinalizing` is set first. This ordering avoids the race.
@reardonj

reardonj commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

As it happens, fromCompletableFuture actually does use this pattern. Maybe #4469 is related.

@reardonj reardonj changed the title Fix Async Cont get cancellation race Update MacOS and Fix Async Cont get cancellation race Nov 7, 2026
@reardonj
reardonj marked this pull request as ready for review November 7, 2026 01:59
_ <- fiber.cancel
} yield ()

one.parReplicateA_(if (!isJS) 8 else 1).replicateA_(50000)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This does take a significant number of tries to fail locally.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant