GlobalState returns nil on first tick when another resource registers AddStateBagChangeHandler
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
- Create a resource that registers a state bag change handler:
-- Resource A
AddStateBagChangeHandler('something', nil, function(bagName, _, value)
-- no-op
end)
- 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)
- Ensure the start order is Resource A and then Resource B
- Observe that:
The first
print(outside the thread) outputs:nilThe secondprint(inside the thread) outputs:10If the resource registeringAddStateBagChangeHandleris not started, both prints correctly output 10
With Resource A (AddStateBagChangeHandler) and Resource B (GlobalState) started
WITHOUT Resource A (AddStateBagChangeHandler)
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
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:
- Resource A registers
AddStateBagChangeHandler→OnStateBagChangeis now set - Resource B does
GlobalState.Test = 10→ hitsSetKey→ becauseOnStateBagChangeexists, the actual storage is deferred viaQueueTask - Resource B immediately reads
GlobalState.Teston the first tick → value has not been stored yet → returnsnil - 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