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

redirect_stdxxx works unexpected with Tasks or multi-threads #42506

Open
cihga39871 opened this issue Oct 5, 2021 · 0 comments
Open

redirect_stdxxx works unexpected with Tasks or multi-threads #42506

cihga39871 opened this issue Oct 5, 2021 · 0 comments

Comments

@cihga39871
Copy link

I found an issue that redirect_stdout fails to restore to the prior stdout when the function is calling in multiple tasks.

Background

I use Julia as the back-end server for an online website, providing bioinformatics classification service for biologists. At the back-end, the Julia server runs classification tasks with multi-threads, and collect stdout and stderr of each task in a separate log file.

However, I found some tasks are failed to output to correct log file, and sometimes Base.stdout and Base.stderr are changed to some log files.

After digging into the problem, I found redirect_stdxxx sets the global stdxxx, and is not thread save. Is is possible to create a thread safe version of those functions?

Environment

Tested in Julia 1.6.1 and 1.7.0-rc1. Version info are shown below.

Julia Version 1.7.0-rc1
Commit 9eade6195e (2021-09-12 06:45 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i9-9940X CPU @ 3.30GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, skylake-avx512)

A small case

outfile = tempname()
outio = open(outfile, "a+")

f() = redirect_stdout(outio) do
    println(stderr, "start redirect stdout")
    sleep(20)
    println(stderr, "stop redirect stdout")
end

schedule(@task f())

@show stdout
# IOStream(<file /tmp/jl_k82o7M>)

sleep(20)
@show stdout
# Base.TTY(RawFD(13) open, 0 bytes waiting)


schedule(@task f())
sleep(2)
schedule(@task f())
@show stdout

sleep(20)
@show stdout # should be TTY, but is IOStream
# IOStream(<file /tmp/jl_k82o7M>)
@cihga39871 cihga39871 changed the title redirect_stdxxx works unexpected with Tasks or multi-threads. Feature request in multi-threads redirect_stdxxx works unexpected with Tasks or multi-threads Oct 6, 2021
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

1 participant