Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backports-release-1.11] Don't free regex objects in exit-time finalizer calls #57834

Open
wants to merge 2 commits into
base: backports-release-1.11
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ mutable struct Regex <: AbstractPattern
end
re = compile(new(pattern, compile_options, match_options, C_NULL))
finalizer(re) do re
re.regex == C_NULL || PCRE.free_re(re.regex)
# don't free during exit because tasks may still be running and
# using it. Issue #57817
during_exit = Base._atexit_hooks_finished
if re.regex != C_NULL && !during_exit
PCRE.free_re(re.regex)
end
end
re
end
Expand Down
8 changes: 8 additions & 0 deletions test/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,11 @@ end
@test match(re, "ababc").match === SubString("ababc", 3:5)
end
end

@testset "#57817: Don't free Regex during exit finalizer calls" begin
# this shouldn't segfault
cmd = `$(Base.julia_cmd()) -t2 --startup-file=no -e 're = Regex(""); Threads.@spawn match(re, "", 1, UInt32(0))'`
for i in 1:10
@test success(pipeline(cmd, stderr=stderr))
end
end