Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected multi-threading issues on Julia 1.8.5 #49035

Closed
jakobnissen opened this issue Mar 17, 2023 · 4 comments
Closed

Unexpected multi-threading issues on Julia 1.8.5 #49035

jakobnissen opened this issue Mar 17, 2023 · 4 comments

Comments

@jakobnissen
Copy link
Member

This might be me not understand that I do something thread-unsafe. Here is a small example, which should be run with 8 threads:

const BUFS = Vector{Vector{UInt8}}(undef, 8)

function foo(i)
    buf = IOBuffer()
    run(pipeline(`cat /my/large/file`; stdout=buf))
    BUFS[i] = copy(buf.data)
end

function bar()
    Threads.@threads for i in 1:8
        foo(i)
    end
end

for i in 1:10
    bar(); println(allequal(BUFS))
end

And I get

true
false
false
false
true
true
false
true
false
true

Adding sleep(1) after the command makes the issue go away. Removing the copy makes the issue go away.
This to me suggests that the run command wrongly reports it's finished before the data is actually inside the IOBuffer.

@vtjnash
Copy link
Member

vtjnash commented Mar 17, 2023

IOBuffer cannot be used for IO (edit: of process output). There is an open issue for me to implement an error for it.

@jakobnissen
Copy link
Member Author

I see. So, the issue is that there is no mechanism to lock the IOBuffer, as essentially the main Julia process and the external command are two distinct threads both operating on it, which is inherently thread-unsafe?

Closing this issue. Is there a thread-safe alternative for an in-memory buffer I can use?

@vtjnash
Copy link
Member

vtjnash commented Mar 17, 2023

Base.BufferStream is the correct replacement for something that will read everything with back-pressure features disable, or a Base.PipeEndpoint to read incrementally

@jakobnissen
Copy link
Member Author

For future references: The solution is #42424, possibly with a reference from the IOBuffer docstring to BufferStream

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants