fivem icon indicating copy to clipboard operation
fivem copied to clipboard

GlobalState returns nil on first tick when another resource registers AddStateBagChangeHandler

Open XenoS-ITA opened this issue 7 months ago • 1 comments

What happened?

When another resource registers a state bag change handler using AddStateBagChangeHandler, accessing GlobalState from a different resource during its startup (first tick) returns nil

This issue only occurs on the first tick during resource initialization, after that, GlobalState behaves normally and returns the correct values

Expected result

GlobalState should return correct values consistently, regardless of whether other resources have registered state bag change handlers

Reproduction steps

  1. Create a resource that registers a state bag change handler:
-- Resource A
AddStateBagChangeHandler('something', nil, function(bagName, _, value)
    -- no-op
end)
  1. Create another resource that sets and reads a value from GlobalState:
-- Resource B
GlobalState.Test = 10
print(GlobalState.Test)

Citizen.CreateThread(function()
    print(GlobalState.Test)
end)
  1. Ensure the start order is Resource A and then Resource B
  2. Observe that: The first print (outside the thread) outputs: nil The second print (inside the thread) outputs: 10 If the resource registering AddStateBagChangeHandler is not started, both prints correctly output 10

With Resource A (AddStateBagChangeHandler) and Resource B (GlobalState) started

Image

WITHOUT Resource A (AddStateBagChangeHandler)

Image

Importancy

There's a workaround

Area(s)

FXServer

Specific version(s)

FXServer-master SERVER v1.0.0.28009 win32

Additional information

the issue appears limited to the first tick during resource initialization, heres a zip containing 2 resources to test this issue

resources.zip

XenoS-ITA avatar Apr 15 '26 21:04 XenoS-ITA

After digging into the source code, I was able to identify the root cause of this bug.

The issue is in StateBagImpl::SetKey inside code/components/citizen-resources-core/src/StateBagComponent.cpp.

When any resource registers an AddStateBagChangeHandler, the OnStateBagChange event on the component becomes truthy. From that point on, every SetKey call (including GlobalState.Test = 10) gets rerouted through gameInterface->QueueTask(...) making it asynchronous instead of storing the value immediately.

This means:

  1. Resource A registers AddStateBagChangeHandler → OnStateBagChange is now set
  2. Resource B does GlobalState.Test = 10 → hits SetKey → because OnStateBagChange exists, the actual storage is deferred via QueueTask
  3. Resource B immediately reads GlobalState.Test on the first tick → value has not been stored yet → returns nil
  4. On the next tick → the async task completes → value is stored → reads correctly from then on

Interestingly, the developers were already aware of this problem. There is a comment directly above the relevant code:

// #TODOPERF: this will unconditionally unpack and call into svMain if *any* change handler is reg'd
// -> ideally we'd have a separate event so we can poll if there's a match for script filters before doing this

The correct fix direction would be to check whether the registered handler actually matches the specific bag/key before deciding to go async, rather than going async unconditionally whenever any handler exists. However this requires changes to how handler filters are queried before the async dispatch.

Hope this helps narrow down the fix

KAZRUTx avatar May 16 '26 20:05 KAZRUTx