FSharp.Control.TaskSeq icon indicating copy to clipboard operation
FSharp.Control.TaskSeq copied to clipboard

Null reference exception when nesting loops in Release mode

Open mrakgr opened this issue 5 months ago • 0 comments

Repo: https://github.com/mrakgr/taskseq_bug_report

Code:

module ICF_Main.Main

open System
open System.Threading.Channels
open FSharp.Control

module Db =
    let service () = 
        let ch_in = Channel.CreateUnbounded()
        let loop_add_db = task {
            for events in ch_in.Reader.ReadAllAsync() do 
                for stream_name, events_grouped_by_stream_name in Array.zip events events do
                    printfn "%A" events_grouped_by_stream_name
                    ()
        }
        loop_add_db

[<EntryPoint>]
let main args =
    let t = Db.service()
    t.Wait()
    0

When I run this in Release with dotnet run -c Release taskseq_bug_report.fsproj here is what happens:

mrakgr@Marko:~/taskseq_bug_report$ dotnet run -c Release taskseq_bug_report.fsproj 
/home/mrakgr/taskseq_bug_report/Program.fs(10,27): warning FS3511: This state machine is not statically compilable. A resumable code invocation at '(12,16--12,19)' could not be reduced. An alternative dynamic implementation will be used, which may be slower. Consider adjusting your code to ensure this state machine is statically compilable, or else suppress this warning.
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AwaitUnsafeOnCompleted[TAwaiter](TAwaiter& awaiter, IAsyncStateMachineBox box)
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_1(Object state)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()

Note that it has to be run in Release mode for the error to trigger and won't in Debug mode. Given that, this could be an F# compiler bug rather than an issue with the TaskSeq library.

mrakgr avatar Aug 14 '25 10:08 mrakgr