Skip to content

redirect_stdin with temp file fails inside compile workload #78

@sandyspiers

Description

@sandyspiers

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, using something like

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,

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
(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 :)

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