Repository navigation
Upgrade mruby to 4.0.0 - #8
Merged
Merged
Conversation
yuwenhuisama
commented
Jun 8, 2026
Contributor
- Bump mruby submodule 3.3.0 -> 4.0.0 (831da26)
- Fix Windows .def exports removed/changed in 4.0 (mrb_default_allocf, mrb_open_allocf removed; mrb_alloca now a macro -> mrb_temp_alloc)
- Fix data-object regression: set MRB_TT_CDATA instance type in mrb_new_data_object (4.0 enforces strict instance-TT on alloc)
- Harden Ruby.Open() for 4.0 MRB_OPEN_FAILURE semantics
- Add bindings: RbClass.DefinePrivateMethod/GetOuterClass, RbArray.Dup, RbState.ClearMethodCache
- Add tests (C# callback exception path + new APIs); 100% line coverage
- Update lock files and AGENTS.md to 4.0.0
- Bump mruby submodule 3.3.0 -> 4.0.0 (831da26) - Fix Windows .def exports removed/changed in 4.0 (mrb_default_allocf, mrb_open_allocf removed; mrb_alloca now a macro -> mrb_temp_alloc) - Fix data-object regression: set MRB_TT_CDATA instance type in mrb_new_data_object (4.0 enforces strict instance-TT on alloc) - Harden Ruby.Open() for 4.0 MRB_OPEN_FAILURE semantics - Add bindings: RbClass.DefinePrivateMethod/GetOuterClass, RbArray.Dup, RbState.ClearMethodCache - Add tests (C# callback exception path + new APIs); 100% line coverage - Update lock files and AGENTS.md to 4.0.0
xUnit runs test classes as parallel collections, so independent RbStates
were opened/closed from several managed threads at once. Two process-global
static dictionaries were mutated without synchronization:
- RbNativeObjectLiveKeeper.StateMapper (GetOrCreateKeeper / ReleaseKeeper)
- RbHelper.RbDataClassMapping (GetOrCreateNewRbDataStructPtr)
Concurrent structural mutation corrupted the Dictionary buckets; because the
keeper roots delegates handed to native mruby, the corruption surfaced as a
hard native test-host crash (not a managed exception) whose location drifted
between runs - the macOS CI failure.
Fixes:
- Lock StateMapper access (StateMapperLock) so check-then-add and remove
are atomic.
- Lock RbDataClassMapping access (RbDataClassMappingLock) so the
check-then-add is atomic and never double-adds / leaks AllocHGlobal.
- Serialize VM lifecycle (VmLifecycleLock) around mrb_open / mrb_close in
Ruby.Open / Ruby.Close; independent states stay usable concurrently.
Tests:
- RbConcurrencyTest: two high-contention regression tests that race the
exact dictionary operations (verified to detect the regression when the
locks are reverted) plus an all-platform sequential mapping sanity test.
- The high-contention tests use [WindowsOnlyFact]: they are stable and
detective on Windows, but the macOS/Linux test host hard-exits under the
synthetic GC/thread storm (a runtime stress limit, not a library defect;
the real parallel suite is green on all platforms after the locks).
Document the threading contract in AGENTS.md and README: RbState is
thread-affine, distinct states may be used concurrently, lifecycle is
serialized internally.
Root cause of the remaining macOS crash: DefineMethod and friends built a NativeMethodFunc bridge delegate, handed its raw function pointer to mruby, and returned the delegate via out NativeMethodFunc. Callers that discard it (the idiomatic out _, used throughout the tests) left the delegate with no managed root. The next GC collected it while mruby still held the function pointer; invoking the Ruby method then jumped through freed memory and hard- crashed the test host. This is GC-timing dependent, so it fired on the 3-core macOS CI runner but not on faster multi-core dev boxes. A forced-GC repro crashes 20/20 before this change and passes after it. Fix: the library now auto-roots every callback delegate to the owning RbState. RbHelper.BuildAndRootNativeCallback builds the bridge and pins it in a per-state RbNativeObjectLiveKeeper<RbCallbackKeeper, NativeMethodFunc> until Ruby.Close. Applied to RbClass.DefineMethod / DefinePrivateMethod / DefineClassMethod / DefineModuleMethod, RbValue.DefineSingletonMethod, RbState.NewProc, and RbState Protect/Ensure/Rescue/RescueExceptions. The out NativeMethodFunc is still returned for compatibility, but callers no longer need to keep it alive. Tests: RbCallbackLifetimeTest forces aggressive GC between defining and calling a method to expose the dangling pointer. It is [WindowsOnlyFact] because a forced GC.Collect from one test while other test classes are inside native mruby callbacks hard-exits the macOS/Linux host (signal-based GC thread-suspension), independent of this bug. The ordinary parallel suite, which defines hundreds of callbacks via out _, now passes 50/50 on macOS (and 20/20 under a simulated 3-core/server-GC run) where it previously crashed about half the time. Also refresh the committed Linux .so and macOS .dylib, which were stale from before the mruby 4.0 upgrade and lacked mrb_open_failure_p (only the Windows .dll had been rebuilt). The .so was rebuilt in an ubuntu-24.04 container matching CI and the .dylib as a universal x86_64+arm64 binary; both export the symbol and pass the suite on their platform.
The macOS test host still aborts on CI but cannot be reproduced on a local (10-core) Mac; the crash is GC-timing dependent and only manifests on the 3-core macos-14 runner. Add --blame-crash + detailed logging and upload the TestResults (sequence file + crash dump) as an artifact so the actual native stack and the exact test executing at the crash can be inspected. Also add RbDataObjectGcTest: single-threaded GC-stress probes for the data-object dfree path (Windows-only, like the other GC-storm probes). These pass single-threaded, confirming the data-object free delegate is rooted.
… GC) Root cause finally isolated: the macOS test-host abort is a known macOS-only CoreCLR issue, NOT a portable library bug. Linux CI passes the identical mruby 4.0 build; only macos-14 crashes. macOS suspends managed threads for GC via signals (PAL_InjectActivation -> pthread_kill SIGUSR1); when a thread is parked inside native mruby (mrb_close running data-object dfree callbacks) and another thread triggers GC, the signal lands fatally and PROCAbort()s the host. Refs: dotnet/runtime#44498, dotnet/runtime#97186 (identical xUnit/macOS/CI-only crash, mitigated by the standalone clrgc), xamarin-macios#13962 (exact PROCAbort/InjectActivation/FinalizerThread stack). mruby 3.3 did less native work at teardown so the window never opened; 4.0's MRB_TT_CDATA finalization widened it. Fix (macOS test step only, verified harmless locally): run the standalone GC (DOTNET_GCName=libclrgc.dylib) with concurrent GC disabled (DOTNET_gcConcurrent=0), the same mitigation the Roslyn team used for #97186. Linux/Windows test steps are unchanged. --blame-crash stays enabled to capture a dump if it still aborts.
This was referenced Jun 8, 2026
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.