Repository navigation
fix: Freeze the length unit across isolates - #128
Merged
Merged
Conversation
The flag that freezes the length unit once a world exists was a Dart static, so each isolate had its own copy while the length unit itself is process-wide. The native backend now keeps the flag next to the world lock and checks it under that lock, and the WebAssembly backend keeps it in the backend instance.
spydon
force-pushed
the
fix/length-unit-lock-across-isolates
branch
from
October 19, 2026 08:17
f355538 to
3037def
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.
Description
Box2D's length unit is process-wide, and
initializeForge2Drefuses to change it once aWorldexists, since every world was built for the old unit. The flag that tracked whether a world exists was a Dart static, though, and each isolate has its own copy of Dart statics. So isolate B could still change the length unit under isolate A's live worlds, which is exactly the silent corruption theStateErroris meant to prevent. #127 documents that several isolates can each run worlds, which makes this easier to hit.This PR:
RawBox2D.setLengthUnitsPerMeterbecomestrySetLengthUnitsPerMeter, which refuses the change once a world has been created, and a newresetLengthUnitLockbacksdebugResetLengthUnitLock;native/ffi/f2d_world_lock.c. Creating a world sets it, and setting the length unit checks it, both under the world lock from fix: Create and destroy worlds under a process-wide lock #127, so the check and the change cannot race with another isolate creating a world;markWorldCreatedfromWorld, since the backends now track this themselves;length_unit_test.dartreset the flag first, since earlier suites in the same process have now created worlds.No public API changes. Code that changes the length unit from another isolate after a world exists now gets the
StateErrorinstead of silently corrupting the simulation.Testing (macOS arm64): the new test fails without the fix. With it, the full suite (117 tests) passes on the VM, the length unit, world and tolerance tests pass on the WebAssembly backend in Chrome, and
dart analyze --fatal-infosanddart formatare clean.Checklist
fix:,feat:,docs:etc).docsand added dartdoc comments with///.examples.Breaking Change
Related Issues
Follow-up to #127.