Repository navigation
fix(bucket): fix crash from concurrent updates of the observed buckets - #1569
Conversation
…dateObservingBucketsAsync
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 34 |
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
left a comment
There was a problem hiding this comment.
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
…and incorrectly skipping bucket updates
sven-n
left a comment
There was a problem hiding this comment.
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:
BucketAreaOfInterestConcurrencyTestand the movement tests pass.PacketsAreShownInChronologicalOrderfailed once in a full run, but passed 6/6 alone, with and without this PR.LoginTicketServiceTest.ConcurrentIssuersStayWithinTheLimitAsyncfails now and then. Its cause is a race inPlayerInMemoryContext.DeleteExpiredLoginTicketsAsyncfrom #1559 (it enumerates the ticket repository without the lock): "Collection was modified". It's unrelated to this PR.
Generated by Claude Code
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)
Problem
The server terminates with a
NullReferenceExceptioninBucketAreaOfInterestManager.UpdateObservingBucketsAsync: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
moveLockonly covers thebucket swap, so
UpdateObservingBucketsAsyncran unprotected onplayer.ObservingBuckets,a plain
List:List.Addcan expose an unfilled (null) slot, which caused the crash."collection was modified".
"object out of scope" / "in scope" notifications could be reordered.
Fix
ConditionalWeakTable<IBucketMapObserver, AsyncLock>) serializesadding, 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 orderof the moves. Moves inside the same bucket are unaffected.
lockon the list.The list readers in
ObserverToWorldViewAdapteruse the same lock.has already left the map, so nothing subscribes after the removal.
/showids, move/walk/remove NPC) read through the newGetObservedLocateables(), which takes a snapshot under the lock.Refactoring
The observer-side logic moved from
BucketAreaOfInterestManagerto a newObservationLease: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 onBucketAreaOfInterestManager.Tests
New
BucketAreaOfInterestConcurrencyTest.