Skip to content

MailboxProcessor still process messages(and receives) after Dispose is invoked #10720

@vshapenko

Description

@vshapenko

Please provide a succinct description of the issue.

Given a mailbox, i invoke Dispose on it by (b :> IDisposable).Dispose(). After that it continues to receive and process messages

Provide the steps required to reproduce the problem:

open System
open System.Threading

// Define a function to construct a message to print
type Msg =
  | Print of string

[<EntryPoint>]
let main argv =
    let mutable skipMessages = false
    let inbox =
        MailboxProcessor.Start( fun b ->
            let rec loop() = async{
                match! b.Receive() with
                | Print msg ->
                    if(skipMessages)
                    then
                        printfn "skipping message %s" msg
                        return! loop()
                        
                    else 
                        do! Async.Sleep 1000
                        if(skipMessages) then () else
                           printfn "%s" msg
                        return! loop()
                  
            }
            loop()
        )
    [1..10] |> Seq.iter (fun x -> string x |> Print |> inbox.Post )
    async{
        do! Async.Sleep 2500
        skipMessages <- true
        (inbox:>IDisposable).Dispose()
        inbox.Post (Print "11")
        printfn "Mailbox disposed"
        printfn "cts cancelled, queue count %i" inbox.CurrentQueueLength
    } |> Async.Start
    Console.ReadLine() |> ignore
    0 // return an integer exit code

Expected behavior

After dispose is called, mailbox should stop receiving and processing messages

Actual behavior

After dispose is called, message receiving and processing is not stopped

Known workarounds

Add a flag and skip message processing based on its value

Related information

Not related to specific net core or fsharp version

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-AsyncAsync supportBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.

    Type

    No type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions