Skip to content

fix: serialize extended-class allocate/register to prevent duplicate class names - #421

Merged
NathanWalker merged 2 commits into
mainfrom
fix/extended-class-registration-race
Jul 31, 2026
Merged

fix: serialize extended-class allocate/register to prevent duplicate class names#421
NathanWalker merged 2 commits into
mainfrom
fix/extended-class-registration-race

Conversation

@edusperoni

@edusperoni edusperoni commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

Primary fix for #420 — the long-standing intermittent exceeded 600 seconds ... Jasmine tests CI wedge.

Root cause (captured live — full analysis in #420)

objc_allocateClassPair only rejects names that are already registered: the name enters the runtime's lookup table in objc_registerClassPairaddNamedClass, not at allocation (verified against objc4 source). GetExtendedClass's allocate-as-collision-check strategy is therefore racy across threads: several workers extending the same class name concurrently (e.g. each require-ing a module that extends TimerTargetImpl) can all pass the nil-check inside the retry ladder and register duplicate same-named classes. objc keeps both and logs Class X is implemented in both … — that warning appears in every captured failure, CI and local.

With duplicate names, name-based class resolution is ambiguous. Instrumented runs caught the consequence in-frame: two workers minted TimerTargetImpl10 2 ms apart, and one then ran +initialize of the other worker's class — whose synthesized +initialize takes a v8::Locker on the creating isolate, inside the objc class-init monitor. That closes a lock-order cycle with the main thread's synchronous invocation of worker-owned closures (nil-queue NSNotificationCenter block observers), wedging the app until the test harness times out.

Fix

Serialize the allocate → register window under a process-global os_unfair_lock.

Why not the in-tree SpinLock: by that header's own criteria the section doesn't qualify — it takes objc's runtimeLock internally (the holder can sleep) and contenders run at worker-chosen QoS (iosPriority), so a spinning waiter burns CPU against a sleeping holder and risks priority inversion. os_unfair_lock donates priority and doesn't spin. The lock is a leaf — the section never acquires a v8::Locker — so it cannot form a cycle with the isolate locks.

User-facing class names are unchanged: first claimant of a name still gets it verbatim (MyAppDelegate stays MyAppDelegate), later same-name extends still walk the existing retry ladder — the lock only makes the ladder's collision detection actually atomic.

Validation

Local repro loop (samples the app when it outlives its normal lifetime; recipe in #420):

  • Baseline: wedged on attempts 2, 8, and 7 of three loops (~1-in-8 per run), always accompanied by implemented in both warnings.
  • With the fix: 42/42 consecutive runs clean (30 validating the lock scope + 12 on the final os_unfair_lock form), zero duplicate-class warnings. P ≈ 0.4% of that happening by chance at the baseline rate.

Related: #419 hardens the harness/CI side (report delivery retries, fail-fast sentinel, diagnostics upload) so any residual wedge variant stays diagnosable.

Summary by CodeRabbit

  • Bug Fixes
    • Improved stability when creating and registering extended classes concurrently.
    • Preserved fallback behavior when class allocation encounters naming conflicts.
    • Added safeguards to prevent allocation retries from continuing indefinitely.

…class names

objc_allocateClassPair only rejects names that are already registered -
the name enters the lookup table in objc_registerClassPair, not at
allocation. Concurrent extends of the same class name (e.g. several
workers requiring a module that extends a named class) could therefore
both pass the collision check and register duplicate same-named classes;
objc keeps both, logs 'Class X is implemented in both', and name-based
class resolution becomes ambiguous.

That ambiguity let a worker first-touch another isolate's freshly
extended class, whose synthesized +initialize takes a v8::Locker on the
creating isolate inside the objc class-init monitor - closing a
lock-order cycle with the main thread's synchronous invocation of
worker-owned closures and wedging the app until the test harness's 600s
timeout (the long-standing intermittent CI failure).

Serialize the allocate -> register window under a process-global
os_unfair_lock. Captured live via thread samples and per-class probes;
with the lock, 42 consecutive local suite runs are clean versus a
roughly 1-in-8 baseline wedge rate.

Primary fix for #420.
@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

GetExtendedClass now uses an RAII UnfairMutex, efficiently probes numeric suffixes, caps allocation retries at 100 failures, and returns nil when exhausted. Class-extension callbacks assert successful class creation, while SpinLock guidance is expanded.

Changes

Class registration synchronization

Layer / File(s) Summary
Unfair mutex contract
NativeScript/runtime/UnfairLock.h, NativeScript/runtime/SpinLock.h
Adds the UnfairMutex BasicLockable wrapper with optional adaptive spinning and clarifies when blocking code should use it instead of SpinLock.
Bounded class allocation
NativeScript/runtime/ClassBuilder.cpp
Serializes allocation and registration with RAII locking, locates free suffixes using galloping and binary search, and stops after 100 consecutive allocation failures.
Generated-class validation
NativeScript/runtime/ClassBuilder.mm
Asserts that generated extended classes are non-null before registration and setup.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Poem

I’m a bunny by the class-building gate,
With a mutex and suffixes that never wait.
A hundred hops, then nil says “done,”
And null checks guard each rising one.
Thump, thump—registration runs!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the primary fix: serializing extended-class allocation and registration to prevent duplicate class names.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
NativeScript/runtime/ClassBuilder.cpp (1)

36-49: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Use RAII for extendedClassRegistrationLock.

The retry loop calls std::string assignments while holding the lock; if C++ exceptions are enabled, an allocation failure will bypass os_unfair_lock_unlock(&extendedClassRegistrationLock) and leave future class-extension calls blocked. Wrap the lock in a scope guard/guard object centered around the allocate/register window.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@NativeScript/runtime/ClassBuilder.cpp` around lines 36 - 49, Replace the
manual lock/unlock sequence around class allocation and registration with an
RAII guard for extendedClassRegistrationLock, ensuring the guard is acquired
before objc_allocateClassPair and released automatically after
objc_registerClassPair, including when string operations or allocation throw.
Keep the retry behavior in the existing allocation loop unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@NativeScript/runtime/ClassBuilder.cpp`:
- Around line 33-36: Update the class allocation/registration retry loop guarded
by extendedClassRegistrationLock to count failed objc_allocateClassPair
attempts, distinguish repeated name collisions from other allocation failures,
and stop after a bounded number of attempts. Always unlock
extendedClassRegistrationLock before returning the allocation failure so later
class creation is not blocked.

---

Outside diff comments:
In `@NativeScript/runtime/ClassBuilder.cpp`:
- Around line 36-49: Replace the manual lock/unlock sequence around class
allocation and registration with an RAII guard for
extendedClassRegistrationLock, ensuring the guard is acquired before
objc_allocateClassPair and released automatically after objc_registerClassPair,
including when string operations or allocation throw. Keep the retry behavior in
the existing allocation loop unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: e974c5ff-0144-4fb4-be6d-3500e46884b8

📥 Commits

Reviewing files that changed from the base of the PR and between e5e84e0 and 0927f24.

📒 Files selected for processing (1)
  • NativeScript/runtime/ClassBuilder.cpp

Comment thread NativeScript/runtime/ClassBuilder.cpp Outdated
Review round on the registration lock:

- Promote the os_unfair_lock wrapper to UnfairLock.h as a BasicLockable
  UnfairMutex driven by std::lock_guard, so other call sites can use it
  and no raw lock/unlock pairs remain. Benchmarks against the in-tree
  SpinLock (8-thread burst: ~30x faster at ~33x less CPU; sleeping
  holder: ~100x less CPU) are summarized in the header, with a pointer
  from SpinLock.h. The kernel-informed adaptive-spin variant (private
  API, the Firefox approach) is documented behind
  NATIVESCRIPT_UNFAIR_LOCK_ADAPTIVE_SPIN for experiments only.
- Replace the unbounded collision retry ladder with a stateless
  gallop + binary search over objc_getClass probes: same-name reuse
  (worker-heavy suites, HMR re-extends) stays O(log N) per extend with
  no cap to outgrow and no side state, and the remaining bound only
  trips on consecutive allocation failures, i.e. allocation broken for
  reasons other than a name collision.
- Assert non-nil extended classes at both GetExtendedClass call sites.
@edusperoni

Copy link
Copy Markdown
Collaborator Author

Review round in b39662f:

  • Lock promoted to UnfairLock.hUnfairMutex (BasicLockable + try_lock over os_unfair_lock) driven by std::lock_guard; no raw lock/unlock pairs. SpinLock.h now cross-references it.
  • Kernel-adaptive-spin variant (the Firefox approach, private API) documented behind NATIVESCRIPT_UNFAIR_LOCK_ADAPTIVE_SPIN for experiments — never to ship.
  • Collision ladder is now stateless O(log N) (gallop + bisect over objc_getClass), with the failure bound covering only genuinely-broken allocation.

Lock benchmark backing the primitive choice (M-series 14-core, best-of-3, wall/CPU):

scenario SpinMutex os_unfair_lock adaptive (private)
uncontended (ns/op) 1.9 2.0 2.0
short section, 2 thr 32ms/60ms 60/120 28/55
short section, 8 thr 537/4,024 15/114 500/3,909
short section, 16 thr 1,233/14,679 19/156 408/4,997
100µs sleeping holder, 11 thr 72/379 72/4.2 72/4.1
bg-QoS holder inversion, 8 thr 685/3,967 480/620 900/6,961

The sleeping-holder row is this lock's actual regime (the section blocks on objc's runtimeLock): identical wall time, ~100× less CPU than spinning — which matters most on the small CI VMs where the original deadlock lived. The adaptive private variant only wins nanosecond-hot low-contention locks and degenerates to spinlock behavior under bursts, hence documentation-only.

Full suite green on the final state; cumulative post-fix validation across all variants: 48+ consecutive clean runs vs a ~1-in-8 baseline wedge rate.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
NativeScript/runtime/UnfairLock.h (1)

42-58: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Delete copy/move on UnfairMutex to match os_unfair_lock's address-stability requirement.

os_unfair_lock is a value type whose kernel-side identity is tied to its memory address; copying/moving it silently produces two independent locks with no crash, just broken mutual exclusion. UnfairMutex doesn't currently prevent this the way std::mutex does. Not a live bug today (single static instance), but a cheap guard against a very well-known footgun class if this type is ever stored in a copyable object or reallocated container.

🔒 Suggested fix
 struct UnfairMutex {
   os_unfair_lock lock_ = OS_UNFAIR_LOCK_INIT;
+
+  UnfairMutex() = default;
+  UnfairMutex(const UnfairMutex&) = delete;
+  UnfairMutex& operator=(const UnfairMutex&) = delete;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@NativeScript/runtime/UnfairLock.h` around lines 42 - 58, Delete copy and move
construction and assignment for UnfairMutex, keeping its default construction
available. Add the deleted special-member declarations directly in the
UnfairMutex definition so instances cannot be copied or moved while preserving
the existing lock(), try_lock(), and unlock() behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@NativeScript/runtime/ClassBuilder.mm`:
- Line 66: Update ExtendCallback around the GetExtendedClass call to detect and
handle a pending NullptrException before evaluating tns::Assert(extendedClass,
isolate). Ensure the exception is propagated or returned through the existing
error path so execution cannot continue into later allocation work when
GetExtendedClass fails.

---

Nitpick comments:
In `@NativeScript/runtime/UnfairLock.h`:
- Around line 42-58: Delete copy and move construction and assignment for
UnfairMutex, keeping its default construction available. Add the deleted
special-member declarations directly in the UnfairMutex definition so instances
cannot be copied or moved while preserving the existing lock(), try_lock(), and
unlock() behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 145d0c0f-97d6-432d-9d43-9f3562cb1fe1

📥 Commits

Reviewing files that changed from the base of the PR and between 0927f24 and b39662f.

📒 Files selected for processing (4)
  • NativeScript/runtime/ClassBuilder.cpp
  • NativeScript/runtime/ClassBuilder.mm
  • NativeScript/runtime/SpinLock.h
  • NativeScript/runtime/UnfairLock.h


Class extendedClass = ClassBuilder::GetExtendedClass(baseClassName, staticClassName,
std::to_string(isolateId) + "_");
tns::Assert(extendedClass != nil, isolate);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Confirm tns::Assert's failure behavior (abort/throw) and that it isn't a debug-only no-op.
rg -n -A 15 'void Assert' NativeScript/runtime/Helpers.h NativeScript/runtime/Helpers.mm NativeScript/runtime/Helpers.cpp 2>/dev/null

Repository: NativeScript/ios

Length of output: 1521


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== Locate Assert definition/usages =="
rg -n -C 4 'Assert\(|StopExecutionAndLogStackTrace|asserts?|NATIVE_ASSERT|AssertError' NativeScript/runtime 2>/dev/null | head -n 240

echo
echo "== Helpers.h section =="
sed -n '318,365p' NativeScript/runtime/Helpers.h 2>/dev/null || true

echo
echo "== Helpers.c/cpp/mm files =="
fd 'Helpers\.(c|cpp|mm|mm.h)$' NativeScript/runtime -t f | tr '\n' '\n'

Repository: NativeScript/ios

Length of output: 21637


🏁 Script executed:

#!/bin/bash
set -euo pipefail
rg -n -C 8 '^void tns::Assert|tns::Assert\b|^Assert\b\(bool' NativeScript/runtime -g '*.mm' -g '*.cpp' -g '*.hpp' | head -n 220

Repository: NativeScript/ios

Length of output: 16303


🏁 Script executed:

#!/bin/bash
set -euo pipefail
echo "== NativeScript/runtime/Helpers.mm first 500 lines =="
sed -n '1,350p' NativeScript/runtime/Helpers.mm

echo
echo "== Assert implementations beyond Helpers =="
rg -n -C 10 'void\s+Assert|Assert\b\(bool' NativeScript -g '*.cpp' -g '*.mm' -g '*.h' -g '*.hpp' | head -n 300

Repository: NativeScript/ios

Length of output: 14908


Guard against NullptrException in GetExtendedClass before asserting extendedClass.

The two assertions prevent a nil class when allocation fails, but GetExtendedClass can also throw NullptrException(isolate) directly; callers of ExtendCallback need to ensure that exception is handled before this guard is evaluated so the failure does not fall through to later allocation work.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@NativeScript/runtime/ClassBuilder.mm` at line 66, Update ExtendCallback
around the GetExtendedClass call to detect and handle a pending NullptrException
before evaluating tns::Assert(extendedClass, isolate). Ensure the exception is
propagated or returned through the existing error path so execution cannot
continue into later allocation work when GetExtendedClass fails.

@NathanWalker
NathanWalker merged commit f5f9200 into main Jul 31, 2026
9 checks passed
@NathanWalker
NathanWalker deleted the fix/extended-class-registration-race branch July 31, 2026 02:17
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.

2 participants