JuliaLang / JuliaLang/PrecompileTools.jl

`redirect_stdin` with temp file fails inside compile workload

Open
#78 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
249
Forks
18
PR merge metrics
No merged PRs in 30d

Description

Hi,

Testing functionality that requires stdin is usually done using a temporary file and `redirect_stdin`.

Or at least, this is how this is tested in [julia](https://github.com/JuliaLang/julia/blob/7d591fcb00cafb2f0c7b2fe7f240bdfb6299b811/test/show.jl#L738), using something like

```julia
julia> let filename = tempname()
open(filename, "w") do io
print(io, "hello")
end

ret = open(filename) do f
redirect_stdin(f) do
readline()
end
end

@info ret
end
[ Info: hello
```

But this same code fails inside a `@compile_workload`,

```julia
module PrecompileStdin

using PrecompileTools

@setup_workload begin
@compile_workload begin
let filename = tempname()
open(filename, "w") do io
print(io, "hello")
end

ret = open(filename) do f
redirect_stdin(f) do
readline()
end
end

@info ret
end
end
end

end
```
```julia
(PrecompileStdin) pkg> precompile
✗ PrecompileStdin
Precompiling packages finished.
0 dependencies successfully precompiled in 2 seconds. 6 already precompiled.

ERROR: The following 1 direct dependency failed to precompile:

PrecompileStdin

Failed to precompile PrecompileStdin [8f563060-54df-4cc8-b9ee-fdd41108d2e7] to "/home/sandy/.julia/compiled/v1.12/PrecompileStdin/jl_S7650f".
ERROR: LoadError: SystemError: dup: Bad file descriptor
Stacktrace:
[1] systemerror(p::String, errno::Int32; extrainfo::Nothing)
@ Base ./error.jl:186
[2] systemerror
@ ./error.jl:185 [inlined]
[3] dup
@ ./libc.jl:47 [inlined]
[4] _redirect_io_libc
@ ./stream.jl:1252 [inlined]
[5] (::Base.RedirectStdStream)(handle::Base.PipeEndpoint)
@ Base ./stream.jl:1262
[6] (::Base.RedirectStdStream)(thunk::PrecompileStdin.var"#8#9", stream::IOStream)
@ Base ./stream.jl:1466
[7] #6
@ ~/docs/quicks/PrecompileStdin/src/PrecompileStdin.jl:13 [inlined]
[8] open(f::PrecompileStdin.var"#6#7", args::String; kwargs::@Kwargs{})
@ Base ./io.jl:410
[9] open(f::Function, args::String)
@ Base ./io.jl:407
[10] macro expansion
@ ~/docs/quicks/PrecompileStdin/src/PrecompileStdin.jl:12 [inlined]
[11] macro expansion
@ ~/.julia/packages/PrecompileTools/gn08A/src/workloads.jl:73 [inlined]
[12] macro expansion
@ ~/docs/quicks/PrecompileStdin/src/PrecompileStdin.jl:6 [inlined]
[13] macro expansion
@ ~/.julia/packages/PrecompileTools/gn08A/src/workloads.jl:121 [inlined]
[14] top-level scope
@ ~/docs/quicks/PrecompileStdin/src/PrecompileStdin.jl:5
[15] include(mod::Module, _path::String)
@ Base ./Base.jl:306
[16] include_package_for_output(pkg::Base.PkgId, input::String, depot_path::Vector{String}, dl_load_path::Vector{String}, load_path::Vector{String}, concrete_deps::Vector{Pair{Base.PkgId, UInt128}}, source::Nothing)
@ Base ./loading.jl:3024
[17] top-level scope
@ stdin:5
[18] eval(m::Module, e::Any)
@ Core ./boot.jl:489
[19] include_string(mapexpr::typeof(identity), mod::Module, code::String, filename::String)
@ Base ./loading.jl:2870
[20] include_string
@ ./loading.jl:2880 [inlined]
[21] exec_options(opts::Base.JLOptions)
@ Base ./client.jl:315
[22] _start()
@ Base ./client.jl:550
in expression starting at /home/sandy/docs/quicks/PrecompileStdin/src/PrecompileStdin.jl:1
in expression starting at stdin:
```

I am not sure if this is intended or a bug.

Thanks in advance :)

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the failure with the shown PrecompileStdin workload, then read the redirect implementation in Base's stream.jl around lines 1252, 1262, and 1466. Compare that path with the workload expansion in PrecompileTools' workloads.jl around lines 73 and 121. Done means the behavior of redirect_stdin inside @compile_workload is established and the reproducer either succeeds or has a clearly documented limitation.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.