Skip to content

fix(server): persist inventory changes outside the close event - #648

Merged
Qwerty1Verified merged 4 commits into
mainfrom
fix/persist-closed-inventory-writes
Sep 9, 2026
Merged

Qwerty1Verified merged 4 commits into
mainfrom
fix/persist-closed-inventory-writes

Conversation

@Qwerty1Verified

Copy link
Copy Markdown
Member

Summary

Inventory persistence had three gaps:

  • Inventories only reached the database on the close event (server/main.lua:210).
  • The txAdmin shutdown handler wrote only inventories that were currently open, so
    anything mutated without being opened and closed was never persisted at all.
  • onResourceStop did no saving, so a restart qb-inventory discarded in-memory
    state for every inventory, including open ones.

This closes all three:

  • New SaveInventoryItems(identifier) helper holds the upsert; the shutdown and close
    handlers now call it instead of carrying their own copies of the SQL. This consolidates writes to one helper.
  • AddItem and RemoveItem write immediately when the target is an Inventories
    entry that is not open, on the basis that an open inventory will get a close event
    and be saved then; the at-risk mutations are exactly the ones to closed inventories.
  • onResourceStop flushes open inventories when qb-inventory itself stops.

The resulting invariant: a closed inventory is always current in the database, and an
open one is written on close, on shutdown, or on resource stop. Only a hard crash
mid-session loses anything. Both existing handlers keep their isOpen conditions,
which are now sufficient rather than gaps.

Related issue

No issue. Found while working on #647; a headless AddItem to a stash nobody had opened succeeded in memory with no persistence.

Change type

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Refactor, maintenance, or performance improvement

Testing

  • FXServer artifact: b35265
  • qb-core version or commit: 1.3.0
  • Resource version or commit: 2.2.1 / 2ff8fb2
  • Server operating system: Windows Server 2022
  • Test steps and results:
  1. Headless AddItem to a closed stash, then restart qb-inventory with no open or
    close in between; item present afterwards. Previously lost.
  2. Item dragged into a stash left open, then restart qb-inventory; item present.
    Previously lost. This exercises the onResourceStop flush
  3. Dropped an item, dragged another into the bag, restarted, then checked
    SELECT identifier FROM inventories WHERE identifier LIKE 'drop-%'; empty, so
    drops remain memory-only and no phantom rows are created.
  4. Open, add, close, restart; item present, close path unchanged.
  5. /giveitem and a relog; player inventories unaffected.

Compatibility and migration

None

Checklist

  • I agree to follow the QBCore FiveM Code of Conduct.
  • My pull request title follows Conventional Commits, such as fix(scope): ... or feat(scope): ....
  • This pull request contains one focused change and does not include unrelated formatting or refactoring.
  • I tested the change on a current FXServer artifact with the relevant official resources and dependencies.
  • Existing repository checks and linting pass.
  • I documented new behavior and identified every breaking or migration-related change.
  • I added or updated configuration examples, SQL migrations, and translations where applicable.
  • I removed credentials, webhook URLs, license keys, database data, player identifiers, and other private information.
  • I have the right to submit all included code and assets under the repository's license.
  • I understand and reviewed all submitted code, including any AI-assisted code, and accept responsibility for its correctness and licensing.

@Qwerty1Verified
Qwerty1Verified merged commit 80c6800 into main Sep 9, 2026
2 checks passed
@Qwerty1Verified
Qwerty1Verified deleted the fix/persist-closed-inventory-writes branch September 9, 2026 04:54
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.

1 participant