Skip to content

fix(callback): scope register's event to local instead of leaking to _G#771

Merged
thelindat merged 1 commit into
overextended:mainfrom
Kenshiin13:callbackEventLocal
May 4, 2026
Merged

fix(callback): scope register's event to local instead of leaking to _G#771
thelindat merged 1 commit into
overextended:mainfrom
Kenshiin13:callbackEventLocal

Conversation

@Kenshiin13
Copy link
Copy Markdown
Contributor

Description

lib.callback.register builds an event name and stores it in a variable named event, but the variable is missing the local keyword. In Lua, an assignment without local writes to the global namespace, so every call to lib.callback.register clobbers _G.event in the calling resource:

function lib.callback.register(name, cb)
    event = cbEvent:format(name)        -- writes to _G.event
    lib.setValidCallback(name, true)
    RegisterNetEvent(event, function(...) ... end)
end

Each resource using ox_lib loads its own copy of imports/callback/*.lua into its own Lua state (per resource/init.lua's lazy loader), so the leak is into the calling resource's globals, not ox_lib's. Any user code that uses event as a variable name has it overwritten the moment it calls lib.callback.register.

Reproduction

-- in any resource using ox_lib
event = 'my-resource-event'
print('before:', event)               -- my-resource-event

lib.callback.register('do_thing', function(source)
    return true
end)

print('after:',  event)               -- __ox_cb_do_thing

If the resource later does TriggerEvent(event) expecting 'my-resource-event', it now fires '__ox_cb_do_thing' — a callback channel it didn't intend to invoke. event is a fairly common variable name in event-driven code, so the collision isn't theoretical.

Fix

Add local to both register functions:

 function lib.callback.register(name, cb)
-    event = cbEvent:format(name)
+    local event = cbEvent:format(name)

Copy link
Copy Markdown
Member

@unitysync unitysync left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks.

@thelindat thelindat merged commit e77dfae into overextended:main May 4, 2026
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.

3 participants