Skip to content

Commit

Permalink
fix(server/inventory): possible race condition when saving?
Browse files Browse the repository at this point in the history
Potential race condition where an item is moved, a save is triggered, and
the inventory is removed from memory (before the save completes). Opening
the inventory after it is removed (Inventory.Remove) causes it to re-fetch
from the database, which might contain old save data.

This was tested by adding a large delay (5s) before triggering the save, and
removing the time check for removal (i.e. the inventory is only removed if
not used for 20m). Would be hard to consistently repro.

- take 1 burger from stash
- stash is not opened for 20 minutes
- save is initiated and the stash is cleared from memory
- player opens the stash before db is updated, gets old stash data
- burger is back

Hitches, slow db access, or large save data would increase the window for
this to occur.
  • Loading branch information
thelindat committed Jun 17, 2023
1 parent 987687f commit 865292f
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions modules/inventory/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2285,8 +2285,14 @@ local function saveInventories(manual)
size[index] += 1
parameters[index][size[index]] = data
end
end

db.saveInventories(parameters[1], parameters[2], parameters[3], parameters[4])

if not manual then return end

if not manual and not inv.open then
for _, inv in pairs(Inventories) do
if not inv.open then
if inv.datastore and inv.netid and (inv.type == 'trunk' or inv.type == 'glovebox') then
if NetworkGetEntityFromNetworkId(inv.netid) == 0 then
Inventory.Remove(inv)
Expand All @@ -2297,9 +2303,7 @@ local function saveInventories(manual)
Inventory.Remove(inv)
end
end
end

db.saveInventories(parameters[1], parameters[2], parameters[3], parameters[4])
end
end

lib.cron.new('*/5 * * * *', function()
Expand Down

0 comments on commit 865292f

Please sign in to comment.