From d4fc26bc82ce9136f6a44dd34ed263acaff7bf6f Mon Sep 17 00:00:00 2001 From: Chris Foster Date: Mon, 28 Oct 2019 16:50:52 +1000 Subject: [PATCH] Make finalizer warning for shredded SecretBuffer async This avoids IO in the finalizer where it's invalid to task switch. --- base/secretbuffer.jl | 2 +- test/secretbuffer.jl | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/base/secretbuffer.jl b/base/secretbuffer.jl index a3abfe2a01793..266edb91d2d5b 100644 --- a/base/secretbuffer.jl +++ b/base/secretbuffer.jl @@ -166,7 +166,7 @@ function read(io::SecretBuffer, ::Type{UInt8}) end function final_shred!(s::SecretBuffer) - !isshredded(s) && @warn("a SecretBuffer was `shred!`ed by the GC; use `shred!` manually after use to minimize exposure.") + !isshredded(s) && @async @warn("a SecretBuffer was `shred!`ed by the GC; use `shred!` manually after use to minimize exposure.") shred!(s) end diff --git a/test/secretbuffer.jl b/test/secretbuffer.jl index 9809bc5c10585..0ad4a3ae383f7 100644 --- a/test/secretbuffer.jl +++ b/test/secretbuffer.jl @@ -28,7 +28,15 @@ using Test @test !isshredded(secret_b) # TODO: ideally we'd test that the finalizer warns from GC.gc(), but that is harder - @test_logs (:warn, r".*SecretBuffer was `shred!`ed by the GC.*") finalize(secret_b) + @test_logs (:warn, r".*SecretBuffer was `shred!`ed by the GC.*") begin + finalize(secret_b) + # Allow the async task which produces the SecretBuffer warning to run. + # This is a hack, but we don't have a way to get a handle to that + # task in order to `wait` on it. + for i=1:1000 + yield() + end + end @test isshredded(secret_b) secret_b = nothing GC.gc()