WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

asyncmap: silently incorrect results with StructArrays (at least) #60304

@aplavin

Description

@aplavin

MWE:

julia> using StructArrays

# asyncmap() with sleep() inside returns nothings:
julia> asyncmap(StructArray(a=[1,2,3])) do x
               sleep(1)
               "abc $x"
       end
3-element Vector{Nothing}:
 nothing
 nothing
 nothing

# fine without sleep():
julia> asyncmap(StructArray(a=[1,2,3])) do x
               "abc $x"
       end
3-element Vector{String}:
 "abc (a = 1,)"
 "abc (a = 2,)"
 "abc (a = 3,)"

# fine with map() instead of asyncmap():
julia> map(StructArray(a=[1,2,3])) do x
               sleep(1)
               "abc $x"
       end
3-element Vector{String}:
 "abc (a = 1,)"
 "abc (a = 2,)"
 "abc (a = 3,)"

The bug seems to be in asyncmap, it assumes too much about the collection. asyncmap works like this:

  • create array of Refs in
    asyncrun = map(wrapped_f, c...)
  • loop over these Refs (in multiple tasks, but the multi-task aspect doesn't matter)
  • mutate ref.x to store the resulting element
    exec_func = (r,args) -> (r.x = f(args...))

In the MWE above with StructArrays, the asyncrun variable is naturally also a StructArray (it results from map). But extracting an element from a StructArray creates a new object, not a copy – no way around it. So, the r.x = ... assignment doesn't mutate anything in the resulting array that gets returned.

I guess, to be generic, the Channel should store indices in the original array instead of elements?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions