Skip to content

Mutable variables maintain state across calls #157

Description

@isaacabraham

I had a brief chat about this with @eiriktsarpalis but it's worth discussing here in more detail. Look at the following code sample: -

let mutable x = 5
let workflow = cloud {
    let! worker = Cloud.CurrentWorker
    x <- x + 1
    return worker.Id, x }
for _ in 1 .. 10 do
    workflow |> cluster.Run |> printfn "%A"

Running the above code produces something like the following: -

("mbrace://work-laptop:36553", 6)
("mbrace://work-laptop:36553", 7)
("mbrace://work-laptop:36553", 8)
("mbrace://work-laptop:36554", 6)
("mbrace://work-laptop:36554", 7)
("mbrace://work-laptop:36554", 8)
("mbrace://work-laptop:36554", 9)
("mbrace://work-laptop:36554", 10)
("mbrace://work-laptop:36554", 11)
("mbrace://work-laptop:36554", 12)

I would have expected the values to all be 6, whereas clearly the lifetime of a captured mutable variable is longer living. How does this behave?

What's even stranger is that if I then do

x <- 10

and rerun the workflow another ten times, I get the following output: -

("mbrace://work-laptop:36554", 11)
("mbrace://work-laptop:36554", 12)
("mbrace://work-laptop:36554", 13)
("mbrace://work-laptop:36554", 14)
("mbrace://work-laptop:36554", 15)
("mbrace://work-laptop:36555", 11)
("mbrace://work-laptop:36552", 11)
("mbrace://work-laptop:36552", 12)
("mbrace://work-laptop:36552", 13)
("mbrace://work-laptop:36552", 14)

Note that I haven't regenerated the workflow - I simple ran the loop again. So a mutable variable has some state of its own on each node, unless it's modified locally, in which case it gets reset.

By the way I'm not suggesting that this is a good pattern to adopt :-) But I didn't expect this behaviour at all.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions