server/item/inventory: fix data race between a slot change and SlotFunc or Close - #1417
Open
schphe wants to merge 4 commits into
Open
server/item/inventory: fix data race between a slot change and SlotFunc or Close#1417schphe wants to merge 4 commits into
schphe wants to merge 4 commits into
Conversation
schphe
force-pushed
the
fix/inventory-slotfunc-race
branch
2 times, most recently
from
August 12, 2026 01:36
50285a7 to
0b0c40f
Compare
schphe
force-pushed
the
fix/inventory-slotfunc-race
branch
from
August 12, 2026 05:08
a8e09b7 to
efd813f
Compare
HashimTheArab
approved these changes
Aug 12, 2026
schphe
force-pushed
the
fix/inventory-slotfunc-race
branch
from
August 18, 2026 00:45
8503f9a to
b788bc8
Compare
…nc or Close setItem returns a closure that its callers run after the inventory has been unlocked, so that the function called on a slot change does not run while the inventory is locked. That closure read inv.f when it ran rather than when it was built, while SlotFunc and Close write inv.f under the lock, so a slot being written concurrently with either was a data race on a type documented as safe for concurrent use. The function is now read while the lock is still held. A change already in progress therefore calls the function that was installed when it started, which is the same guarantee the lock gives every other field.
schphe
force-pushed
the
fix/inventory-slotfunc-race
branch
from
August 20, 2026 07:51
b788bc8 to
9c5c7f9
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.
What happens and why
setItem returns a closure that its callers run after the inventory has been
unlocked, so that the function called on a slot change does not run while the
inventory is locked. That closure read inv.f when it ran rather than when it
was built, while SlotFunc and Close write inv.f under the lock, so a slot
being written concurrently with either was a data race on a type documented as
safe for concurrent use.
The function is now read while the lock is still held. A change already in
progress therefore calls the function that was installed when it started,
which is the same guarantee the lock gives every other field.
Verification
Regression test reports
WARNING: DATA RACEwithout the fix, undergo test -race. Note the repository's CI does not run with the race detector, so this test only earns its place if run that way.