From 9665f7720e7e92f9095871062460719600555f32 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Tue, 1 Oct 2024 16:35:56 -0400 Subject: [PATCH 1/4] also redirect JL_STDERR etc. when redirecting to devnull --- base/stream.jl | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/base/stream.jl b/base/stream.jl index 93aeead79eb9c..65b78bf9aa1eb 100644 --- a/base/stream.jl +++ b/base/stream.jl @@ -1244,6 +1244,14 @@ function _redirect_io_libc(stream, unix_fd::Int) dup(posix_fd, RawFD(unix_fd)) nothing end +function _redirect_io_cglobal(handle::Union{LibuvStream, IOStream}, unix_fd::Int) + c_sym = unix_fd == 0 ? cglobal(:jl_uv_stdin, Ptr{Cvoid}) : + unix_fd == 1 ? cglobal(:jl_uv_stdout, Ptr{Cvoid}) : + unix_fd == 2 ? cglobal(:jl_uv_stderr, Ptr{Cvoid}) : + C_NULL + c_sym == C_NULL || unsafe_store!(c_sym, handle.handle) + nothing +end function _redirect_io_global(io, unix_fd::Int) unix_fd == 0 && (global stdin = io) unix_fd == 1 && (global stdout = io) @@ -1252,11 +1260,7 @@ function _redirect_io_global(io, unix_fd::Int) end function (f::RedirectStdStream)(handle::Union{LibuvStream, IOStream}) _redirect_io_libc(handle, f.unix_fd) - c_sym = f.unix_fd == 0 ? cglobal(:jl_uv_stdin, Ptr{Cvoid}) : - f.unix_fd == 1 ? cglobal(:jl_uv_stdout, Ptr{Cvoid}) : - f.unix_fd == 2 ? cglobal(:jl_uv_stderr, Ptr{Cvoid}) : - C_NULL - c_sym == C_NULL || unsafe_store!(c_sym, handle.handle) + _redirect_io_cglobal(handle, f.unix_fd) _redirect_io_global(handle, f.unix_fd) return handle end @@ -1264,6 +1268,7 @@ function (f::RedirectStdStream)(::DevNull) nulldev = @static Sys.iswindows() ? "NUL" : "/dev/null" handle = open(nulldev, write=f.writable) _redirect_io_libc(handle, f.unix_fd) + _redirect_io_cglobal(handle, f.unix_fd) close(handle) # handle has been dup'ed in _redirect_io_libc _redirect_io_global(devnull, f.unix_fd) return devnull From 8679995e84938441dc1b86d2a1c52549dfb049fe Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Fri, 3 Oct 2025 13:10:16 -0400 Subject: [PATCH 2/4] Update stream.jl --- base/stream.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/base/stream.jl b/base/stream.jl index 65b78bf9aa1eb..38fd0fd61f45e 100644 --- a/base/stream.jl +++ b/base/stream.jl @@ -1244,12 +1244,12 @@ function _redirect_io_libc(stream, unix_fd::Int) dup(posix_fd, RawFD(unix_fd)) nothing end -function _redirect_io_cglobal(handle::Union{LibuvStream, IOStream}, unix_fd::Int) +function _redirect_io_cglobal(handle::Union{LibuvStream, IOStream, RawFD}, unix_fd::Int) c_sym = unix_fd == 0 ? cglobal(:jl_uv_stdin, Ptr{Cvoid}) : unix_fd == 1 ? cglobal(:jl_uv_stdout, Ptr{Cvoid}) : unix_fd == 2 ? cglobal(:jl_uv_stderr, Ptr{Cvoid}) : C_NULL - c_sym == C_NULL || unsafe_store!(c_sym, handle.handle) + c_sym == C_NULL || unsafe_store!(c_sym, handle isa RawFD ? handle : handle.handle) nothing end function _redirect_io_global(io, unix_fd::Int) @@ -1268,8 +1268,8 @@ function (f::RedirectStdStream)(::DevNull) nulldev = @static Sys.iswindows() ? "NUL" : "/dev/null" handle = open(nulldev, write=f.writable) _redirect_io_libc(handle, f.unix_fd) - _redirect_io_cglobal(handle, f.unix_fd) close(handle) # handle has been dup'ed in _redirect_io_libc + _redirect_io_cglobal(RawFD(f.unix_fd), f.unix_fd) _redirect_io_global(devnull, f.unix_fd) return devnull end From 49bc66b8c71807af4f2276cb0a9dfb58036aafd8 Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Fri, 3 Oct 2025 13:11:52 -0400 Subject: [PATCH 3/4] Update stream.jl --- base/stream.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/base/stream.jl b/base/stream.jl index 38fd0fd61f45e..633a15065b891 100644 --- a/base/stream.jl +++ b/base/stream.jl @@ -1244,12 +1244,12 @@ function _redirect_io_libc(stream, unix_fd::Int) dup(posix_fd, RawFD(unix_fd)) nothing end -function _redirect_io_cglobal(handle::Union{LibuvStream, IOStream, RawFD}, unix_fd::Int) +function _redirect_io_cglobal(handle::Union{LibuvStream, IOStream, DevNull}, unix_fd::Int) c_sym = unix_fd == 0 ? cglobal(:jl_uv_stdin, Ptr{Cvoid}) : unix_fd == 1 ? cglobal(:jl_uv_stdout, Ptr{Cvoid}) : unix_fd == 2 ? cglobal(:jl_uv_stderr, Ptr{Cvoid}) : C_NULL - c_sym == C_NULL || unsafe_store!(c_sym, handle isa RawFD ? handle : handle.handle) + c_sym == C_NULL || unsafe_store!(c_sym, handle isa DevNull ? Ptr{Cvoid}(unix_fd) : handle.handle) nothing end function _redirect_io_global(io, unix_fd::Int) @@ -1269,7 +1269,7 @@ function (f::RedirectStdStream)(::DevNull) handle = open(nulldev, write=f.writable) _redirect_io_libc(handle, f.unix_fd) close(handle) # handle has been dup'ed in _redirect_io_libc - _redirect_io_cglobal(RawFD(f.unix_fd), f.unix_fd) + _redirect_io_cglobal(devnull, f.unix_fd) _redirect_io_global(devnull, f.unix_fd) return devnull end From c5710cf19bb5c79b585dc2bcd9057d1f168ee67f Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Fri, 3 Oct 2025 13:14:09 -0400 Subject: [PATCH 4/4] Update stream.jl --- base/stream.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/base/stream.jl b/base/stream.jl index 633a15065b891..6e2f4cd38ee57 100644 --- a/base/stream.jl +++ b/base/stream.jl @@ -1241,15 +1241,15 @@ function _redirect_io_libc(stream, unix_fd::Int) -10 - unix_fd, Libc._get_osfhandle(posix_fd)) end end - dup(posix_fd, RawFD(unix_fd)) + GC.@preserve stream dup(posix_fd, RawFD(unix_fd)) nothing end -function _redirect_io_cglobal(handle::Union{LibuvStream, IOStream, DevNull}, unix_fd::Int) +function _redirect_io_cglobal(handle::Union{LibuvStream, IOStream, Nothing}, unix_fd::Int) c_sym = unix_fd == 0 ? cglobal(:jl_uv_stdin, Ptr{Cvoid}) : unix_fd == 1 ? cglobal(:jl_uv_stdout, Ptr{Cvoid}) : unix_fd == 2 ? cglobal(:jl_uv_stderr, Ptr{Cvoid}) : C_NULL - c_sym == C_NULL || unsafe_store!(c_sym, handle isa DevNull ? Ptr{Cvoid}(unix_fd) : handle.handle) + c_sym == C_NULL || unsafe_store!(c_sym, handle === nothing ? Ptr{Cvoid}(unix_fd) : handle.handle) nothing end function _redirect_io_global(io, unix_fd::Int) @@ -1269,7 +1269,7 @@ function (f::RedirectStdStream)(::DevNull) handle = open(nulldev, write=f.writable) _redirect_io_libc(handle, f.unix_fd) close(handle) # handle has been dup'ed in _redirect_io_libc - _redirect_io_cglobal(devnull, f.unix_fd) + _redirect_io_cglobal(nothing, f.unix_fd) _redirect_io_global(devnull, f.unix_fd) return devnull end