Skip to content

fix(bucket): fix crash from concurrent updates of the observed buckets - #1569

Merged
sven-n merged 4 commits into
MUnique:masterfrom
eduardosmaniotto:fix/observed-buckets-crash
Nov 20, 2026
Merged

sven-n merged 4 commits into
MUnique:masterfrom
eduardosmaniotto:fix/observed-buckets-crash

Conversation

@eduardosmaniotto

@eduardosmaniotto eduardosmaniotto commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The server terminates with a NullReferenceException in BucketAreaOfInterestManager.UpdateObservingBucketsAsync:

Object reference not set to an instance of an object.
   at BucketAreaOfInterestManager.<>c__DisplayClass8_0.<UpdateObservingBucketsAsync>b__2(Bucket`1 i)
   at BucketAreaOfInterestManager.UpdateObservingBucketsAsync(Point newPoint, IBucketMapObserver player)
   at BucketAreaOfInterestManager.MoveObjectAsync(...)
   at PlayerMovement.MoveAsync(Point target)
   at AttackableExtensions.MoveRandomlyAsync(IAttackable target)
   at Monster.AttackAsync(IAttackable target)
   at BasicMonsterIntelligence.SafeTick()

Root cause

A monster hit can move the player (push or random move) on the monster's timer thread, while
the player is moved by its own thread or by other monsters. The moveLock only covers the
bucket swap, so UpdateObservingBucketsAsync ran unprotected on player.ObservingBuckets,
a plain List:

  • A concurrent List.Add can expose an unfilled (null) slot, which caused the crash.
  • Two updates could subscribe the same bucket twice (duplicate notifications), or hit
    "collection was modified".
  • Updates could finish out of order, leaving the observed buckets at an older position, and the
    "object out of scope" / "in scope" notifications could be reordered.
  • The adapter and the GM chat commands read the list without synchronization.

Fix

  • A per-observer lock (ConditionalWeakTable<IBucketMapObserver, AsyncLock>) serializes
    adding, moving and removing an observer. A move takes it while it still holds the moveLock,
    and only then releases the moveLock ("hand over hand"), so the updates are applied in the order
    of the moves. Moves inside the same bucket are unaffected.
  • The diff and the (un)subscription of the observed buckets run atomically under a lock on the list.
    The list readers in ObserverToWorldViewAdapter use the same lock.
  • A removal queues behind updates in progress, and an update is skipped for an observer which
    has already left the map, so nothing subscribes after the removal.
  • The GM chat commands (/showids, move/walk/remove NPC) read through the new
    GetObservedLocateables(), which takes a snapshot under the lock.

Refactoring

The observer-side logic moved from BucketAreaOfInterestManager to a new ObservationLease:
diff, subscribe/unsubscribe and notifications. Holding the lease is the only way to change the
observed buckets, so "the caller must hold the lock" is enforced by the type. The long methods
were split into small named steps. Behavior is unchanged.

The lock order (moveLock -> observer lock; both not reentrant) is documented on
BucketAreaOfInterestManager.

Tests

New BucketAreaOfInterestConcurrencyTest.

@codacy-production

codacy-production Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 34 complexity

Metric Results
Complexity 34

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@sven-n sven-n left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for tracking this crash down. The approach looks right to me:

  • A per-observer AsyncLock, taken while the move lock is still held ("hand over hand"), so the observed buckets are updated in the order of the moves.
  • The diff and the (un)subscription are done atomically under a lock on the list, and the readers use the same lock.
  • A removal queues behind updates which are still running.

I also checked the code which runs under the new observer lock (the moved/in-scope/out-of-scope notifications and the bot observers) for a move, warp or removal of the same observer, which would deadlock. I found none.

But the test project doesn't compile, on this branch as well as merged with master:

tests/MUnique.OpenMU.Tests/BucketAreaOfInterestConcurrencyTest.cs(128,17): error CS0103: The name 'Volatile' does not exist in the current context
tests/MUnique.OpenMU.Tests/BucketAreaOfInterestConcurrencyTest.cs(134,20): error CS0103: The name 'Volatile' does not exist in the current context

The test project has no implicit System.Threading using, so please add using System.Threading; to the file. Then run dotnet build src/MUnique.OpenMU.sln and the new tests (rule 13). I'll rerun everything after that.


Generated by Claude Code

@sven-n sven-n left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, it builds now. The follow-up which checks the same-bucket case under the move lock closes the stale-position race too. The test adjustments are legitimate: they use the configured MaxAllowedWalkStartOffset instead of hard-coded distances, and they set up the start position through MoveAsync.

I built it on current master (including #1547 and #1470) and ran the tests:

  • BucketAreaOfInterestConcurrencyTest and the movement tests pass.
  • PacketsAreShownInChronologicalOrder failed once in a full run, but passed 6/6 alone, with and without this PR.
  • LoginTicketServiceTest.ConcurrentIssuersStayWithinTheLimitAsync fails now and then. Its cause is a race in PlayerInMemoryContext.DeleteExpiredLoginTicketsAsync from #1559 (it enumerates the ticket repository without the lock): "Collection was modified". It's unrelated to this PR.

Generated by Claude Code

@sven-n
sven-n merged commit 97a1f34 into MUnique:master Nov 20, 2026
1 check passed
@eduardosmaniotto
eduardosmaniotto deleted the fix/observed-buckets-crash branch November 20, 2026 14:24
nahuedomino pushed a commit to nahuedomino/OpenMU that referenced this pull request Nov 30, 2026
MUnique#1569)

* fix(bucket): NullReferenceException in BucketAreaOfInterestManager.UpdateObservingBucketsAsync

* fix(build): add missing import

* fix(bucket): Prevents concurrent moves from checking stale positions and incorrectly skipping bucket updates

* fix flaky tests

(cherry picked from commit 97a1f34)
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