Verse: defer not always working

Please select what you are reporting on:

Verse

What Type of Bug are you experiencing?

Verse

Summary

Code in ‘defer’ blocks sometimes doesn’t get called. I’m seeing it specifically in async functions that are cancelled by a race block. There are some cases where you can cancel an aync function with a race, and the defer block does get called. In this repro example, the function is in a race with an Await() on an event that the function signals itself, and in this case the defer doesn’t happen.

Steps to Reproduce

1: Create a new device and paste this code inside the class definition:

TestEvent : event() = event(){}

OnBegin<override>()<suspends>:void=
    Sleep(1.0)
    Print("Starting race")
    race:
        Func1()
        TestEvent.Await()
    Print("Finished race")

Func1()<suspends>:void=
    Print("Enter Func1")
    defer:
        Print("Exit Func1")
    Sleep(1.0)
    TestEvent.Signal()
    Sleep(Inf)

2: create an instance of this device in the map and run
3: observe the output

Expected Result

I would expect the following output:
LogVerse: : Starting race
LogVerse: : Enter Func1
LogVerse: : Exit Func1
LogVerse: : Finished race

Observed Result

I get the following output:
LogVerse: : Starting race
LogVerse: : Enter Func1
LogVerse: : Finished race

Platform(s)

Windows PC

The status of FORT-788902 incident has been moved from ‘Needs Triage’ to ‘To Do’.

I can confirm on that, defer isn’t executed when function is exited early in race. Please fix

I’m having issues with defer not working in suspends functions that are ended early from being in a race.

can you link to this card so we can follow it?