You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
std.regex: every Regex compile leaks the pcre2_maketables tables and a 256-byte block (pre-existing, inherited from the shim) #1036
Debug-allocator evidence (2026-09-03, darwin-arm64), a program that compiles four patterns (Regex.compile("(") failing, then three succeeding compiles plus a clone) and runs captures/replace/split:
Identical verdict on the shim-era std.regex (with_regex_compile in rt/regex_runtime.w) and on the C4 facade (lib/std/regex.w calling pcre2 directly), so the leak predates batch C4 — it is one pair per compile, including a failed one.
The 2,048-byte block is the character tables pcre2_maketables_8(gcontext) builds per compile (regex_compile_code sets ccontext.tables to them; the shim did the same). pcre2 keeps the pointer in the compiled code (re->tables), so the tables must outlive the pattern and be released with pcre2_maketables_free_8 when the Regex drops — today nothing frees them, and Regex has no field to carry them (its ten fields are pinned by CodegenDispatch.gen_regex_literal_value). The 256-byte block per compile is not yet attributed (a tools/debug_drop.w site run will name it).
Two candidate fixes: (a) stop calling pcre2_maketables_8 — the default compile context already carries _pcre2_default_tables_8, and in the "C" locale (With never calls setlocale) maketables produces the same tables, so the call buys nothing; or (b) carry the tables in Regex and free them in drop (touches the literal's codegen layout).
Repro: build any std.regex user with --debug-alloc; e.g. the facade test in the C4 handoff (scratchpad/lib/std/refacade_test.w) or test/behavior/behav_regex_std.w.
Still open for regex literals (found during #2049). A compiled Regex now releases its pair: test/debug_alloc/da_regex_owned_lifetimes.w passes. A literal does not. CodegenDispatch.gen_regex_literal_value caches the compiled code in a per-site global (__regex_lit_<node>, filled once by Regex.__literal_code), and nothing releases it at exit. So every literal site that runs leaves its 256-byte and 2048-byte blocks live.
use std.regex
fn main:
if "zzz" =~ /(\d)/:
print($1)
else:
print("miss")
run --debug-alloc prints two LEAK lines (sizes 256 and 2048) and leak count=2. --debug-alloc-filter=non-root gives the same count: the cache is not marked as a process-lifetime root (with_debug_alloc_mark_root). With three literal sites, behav_regex_capture_fstring and behav_regex_language_semantics show exactly 3 x 2 such leaks once #2049's Captures/binding leaks are fixed.
Fix: release the cached code at exit, or mark it as a root.
Triage 2026-10-25, with v0.15.3.0 (main a261b65): does not reproduce for literals or compiled patterns.
$ cat main.w
use std.regex
fn main:
if "zzz" =~ /(\d)/:
print($1)
else:
print("miss")
$ with run --debug-alloc main.w
miss
debug-alloc: leak count=0 allocations=12
$ with run --debug-alloc compiled.w # a failing compile, three compiles, three clones, is_match
4
debug-alloc: leak count=0 allocations=60
Activity
fix(regex): own compiled patterns, tables, and capture metadata
ehartford commentedon Oct 3, 2026
Still open for regex literals (found during #2049). A compiled
Regexnow releases its pair: test/debug_alloc/da_regex_owned_lifetimes.w passes. A literal does not.CodegenDispatch.gen_regex_literal_valuecaches the compiled code in a per-site global (__regex_lit_<node>, filled once byRegex.__literal_code), and nothing releases it at exit. So every literal site that runs leaves its 256-byte and 2048-byte blocks live.Repro, on main 01ec974 plus the #2049 branch:
run --debug-allocprints two LEAK lines (sizes 256 and 2048) andleak count=2.--debug-alloc-filter=non-rootgives the same count: the cache is not marked as a process-lifetime root (with_debug_alloc_mark_root). With three literal sites, behav_regex_capture_fstring and behav_regex_language_semantics show exactly 3 x 2 such leaks once #2049's Captures/binding leaks are fixed.Fix: release the cached code at exit, or mark it as a root.
#2049/#1036 WIP: regex literal code is owned by a module global the e…
#2049: ownership-validator verdicts in 24 behavior programs fixed at …
ehartford commentedon Oct 25, 2026
Triage 2026-10-25,
with v0.15.3.0(main a261b65): does not reproduce for literals or compiled patterns.