I’ve a couple of questions about the following behavior that I discovered today. Consider the code below:
print("Thread 1 start")
local function normalScript()
print("Thread 3 start")
print("Thread 3 finished")
end
local function timeoutScript()
print("Thread 2 start")
while true do
end
print("Thread 2 finished")
end
local bindable = Instance.new("BindableEvent")
-- This will be processed last
bindable.Event:Connect(function()
normalScript()
end)
-- This will be processed first
bindable.Event:Connect(function()
timeoutScript()
end)
bindable:Fire()
print("Thread 1 finished")
If you run this code, not only the Thread 2 will error due timeout, but thread 1 and thread 3 as well. The end result is that an event handler is capable of halting the calling thread and other event handler that was supposed to execute afterwards. I find this behavior counter intuitive because according with the documentation:
BindableEvent Theading Behavior
This code sample demonstrates how BindableEvents, like all Events, create threads of each connected function. Even if one errors, like
ohNo
does, the others continue. In addition, this sample shows how functions connected to the event can yield without blocking the thread that fired the event.
It appears that right now (no deferred events), while the calling thread is waiting for all event handlers to yield and return control to it, it’s script timer still counts and the end result is that the calling thread and the event handlers that did not started being processed yet also error with timeout.
My two questions about this situation are:
- Is there any official documentation or even a devforum discussion on the subject (couldn’t find any for myself)
- What will happen once deferred events kick in? I supposed the calleing thread will not error anymore because the event is deferred. But, events that were supposed to be processed after the timeout event will still throw error as well?