Skip to content

Commit 68eaec6

Browse files
authored
Make finalizer warning for shredded SecretBuffer async (#33698)
This avoids IO in the finalizer where it's invalid to task switch.
1 parent 9c54952 commit 68eaec6

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

base/secretbuffer.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ function read(io::SecretBuffer, ::Type{UInt8})
166166
end
167167

168168
function final_shred!(s::SecretBuffer)
169-
!isshredded(s) && @warn("a SecretBuffer was `shred!`ed by the GC; use `shred!` manually after use to minimize exposure.")
169+
!isshredded(s) && @async @warn("a SecretBuffer was `shred!`ed by the GC; use `shred!` manually after use to minimize exposure.")
170170
shred!(s)
171171
end
172172

test/secretbuffer.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@ using Test
2828
@test !isshredded(secret_b)
2929

3030
# TODO: ideally we'd test that the finalizer warns from GC.gc(), but that is harder
31-
@test_logs (:warn, r".*SecretBuffer was `shred!`ed by the GC.*") finalize(secret_b)
31+
@test_logs (:warn, r".*SecretBuffer was `shred!`ed by the GC.*") begin
32+
finalize(secret_b)
33+
# Allow the async task which produces the SecretBuffer warning to run.
34+
# This is a hack, but we don't have a way to get a handle to that
35+
# task in order to `wait` on it.
36+
for i=1:1000
37+
yield()
38+
end
39+
end
3240
@test isshredded(secret_b)
3341
secret_b = nothing
3442
GC.gc()

0 commit comments

Comments
 (0)