Summary
When a defer is used inside a function it does not always run, even if the function is cancelled from outside. The defer only runs when it is followed by a simple loop. Any other async method like race, rush, branch, or sync will prevent the defer from being executed when the function is cancelled.
Please select what you are reporting on:
Verse
What Type of Bug are you experiencing?
Verse
Steps to Reproduce
Use the provided snippet pasted below to reproduce the issue. In the snippet, I use a race that calls the function with the defer, but the race can be swapped out with a branch or rush and it will produce the same issue.
async_test_manager := class(creative_device):
@editable
StopAsyncTaskButton : button_device = button_device{}
var AsyncInstances : []async_inst = array{}
OnBegin<override>():void=
StopAsyncTaskButton.InteractedWithEvent.Subscribe(HandleStopAsyncTask)
NewAsync := async_inst{}
spawn. NewAsync.Start()
set AsyncInstances = array{NewAsync}
HandleStopAsyncTask(Agent:agent):void=
for(Inst:AsyncInstances):
Inst.StopEvent.Signal()
async_inst := class:
StopEvent<public>:event()=event(){}
Start<public>()<suspends>:void={
rush:
AsyncFunction1()
AsyncFunction2()
StopEvent.Await()
}
AsyncFunction1<private>()<suspends>:void={
defer{
Print("Defer success on loop")
# This DOES run
}
# Basic Async task here
loop:
Sleep(0.0)
}
AsyncFunction2<private>()<suspends>:void={
defer{
Print("Defer success on race")
# This does NOT run...
}
# Async race task here
race:
loop:
Sleep(0.0)
loop:
Sleep(0.0)
}
``
Expected Result
The defer block should run when the function it is contained in is cancelled.
Observed Result
The defer block does not run when a race, rush, or sync is used afterwards in the same function and that function is cancelled.
Platform(s)
PC