Skip to content

Commit 5f3b080

Browse files
Don't free regex objects in exit-time finalizer calls (#57834)
Fixes #57817 on 1.11
1 parent ec3c02a commit 5f3b080

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

base/regex.jl

+6-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ mutable struct Regex <: AbstractPattern
3939
end
4040
re = compile(new(pattern, compile_options, match_options, C_NULL))
4141
finalizer(re) do re
42-
re.regex == C_NULL || PCRE.free_re(re.regex)
42+
# don't free during exit because tasks may still be running and
43+
# using it. Issue #57817
44+
during_exit = Base._atexit_hooks_finished
45+
if re.regex != C_NULL && !during_exit
46+
PCRE.free_re(re.regex)
47+
end
4348
end
4449
re
4550
end

test/regex.jl

+8
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,11 @@ end
245245
@test match(re, "ababc").match === SubString("ababc", 3:5)
246246
end
247247
end
248+
249+
@testset "#57817: Don't free Regex during exit finalizer calls" begin
250+
# this shouldn't segfault
251+
cmd = `$(Base.julia_cmd()) -t2 --startup-file=no -e 're = Regex(""); Threads.@spawn match(re, "", 1, UInt32(0))'`
252+
for i in 1:10
253+
@test success(pipeline(cmd, stderr=stderr))
254+
end
255+
end

0 commit comments

Comments
 (0)