claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

Bundled ugrep OOMs the host: -E with two bounded {0,N} intervals explodes DFA construction to multiple GB

Open interkelstar opened this issue 5 months ago • 2 comments

What happened

Claude Code's bundled ugrep (the CLI re-execs its own binary as ugrep, argv0=ugrep) allocates unbounded memory during regex compilation when run with -E (POSIX extended regex) on a pattern containing two bounded .{0,N} intervals. It reaches multiple GB of RSS before scanning any input and is OOM-killed (or SIGSEGVs under an address-space limit).

On a 12 GB Linux host this repeatedly killed whole Claude Code sessions. Captured processes reached 4.3 GB and 8.0 GB RSS; the kernel OOM-killer took down the session each time. The triggering process was a real Grep/search invocation of the form:

ugrep -E -o '.{0,40}(session-corpus|short-term-recall|...).{0,30}' <a 74 KB file>

The 74 KB file is irrelevant — the process dies in DFA construction, before reading data.

Minimal reproduction (verified)

# point a name "ugrep" at the bundled binary (adjust the version string)
ln -sf ~/.local/share/claude/versions/2.1.170 /tmp/ugrep
printf 'x\n' > /tmp/f

# BAD: RSS climbs to the 2.5 GB cap, then SIGSEGV (exit 139), zero output
( ulimit -v 2500000; /tmp/ugrep -E -o '.{0,40}abc.{0,30}' /tmp/f )

# GOOD: identical pattern under PCRE2 -> ~8 MB, instant, exits cleanly
( ulimit -v 2500000; /tmp/ugrep -P -o '.{0,40}abc.{0,30}' /tmp/f )

Narrowing:

  • Two bounded intervals are required: .{0,40}abc.{0,30} explodes; a single .{0,80}abc is fine.
  • -o and alternation are not required.
  • Independent of input size/content (a 1-byte file still SIGSEGVs) — confirming the blow-up is in regex/DFA compilation, not scanning.
  • Measured: -E peaks at the imposed cap (~2.37 GB) and SIGSEGVs after ~15–27 s of CPU; -P, -G, and -F all finish instantly at ~8 MB.

Expected

Bounded memory: compile the ERE efficiently, or cap memory and exit with an error.

Actual

Multi-GB allocation during regex compilation → OOM-kill / SIGSEGV. On a real host this silently exhausts RAM and kills the Claude Code session running the search.

Environment

  • Claude Code 2.1.170 (native install), bundled ugrep, Node v24 SEA
  • Linux (Fedora 44, x86-64), 12 GB RAM

Suggested fix

Use -P (PCRE2) for Grep patterns containing .{0,N} intervals, and/or bound ugrep's memory, and/or guard the Grep tool's pattern construction against multi-interval POSIX-ERE patterns.

interkelstar avatar Jun 10 '26 13:06 interkelstar

Reproduced independently on a second host — confirming the OOM is in-process (CLI V8 heap), not the grep child, plus two mitigations that work.

Environment: Claude Code (ccd-cli) 2.1.185, Linux (Fedora 44), AMD Zen1 KVM VPS, 12 GB RAM + 8 GB zram swap, system ripgrep 14.1.1 present.

What happened: during ordinary code-research a session's claude/ccd-cli process ballooned to anon-rss 4.84 GB / total-vm 10.7 GB and was killed by the kernel OOM-killer. Because the matcher runs in-process, the bloat is in the CLI's own V8 heap (consistent with #54394) — so capping an external rg/ugrep child does nothing; the victim is the CLI itself.

System-wide impact before the kill (the part that makes it brutal): ~21 GB pushed through zram swap (pswpout ≈ 3.74M pages), kswapd0 pinned, and PSI memory full avg300 = 32% — i.e. the entire system was stalled on memory reclaim ~1/3 of the time for several minutes. Load average hit ~25 on 6 cores that were 97% idle with steal = 0; cockpit / ssh / tmux were unusable until the OOM finally fired.

Field signature for anyone else hitting this: high load average + idle CPU + steal 0 + kswapd hot + swap in use while RAM looks free → it's this, not the hypervisor/host.

Mitigations that worked for us:

  1. USE_BUILTIN_RIPGREP=0 — routes the Grep tool through system ripgrep (non-backtracking, memory-bounded). Avoids the explosion entirely and is 5–10× faster. This is the practical fix available today (requires system rg on PATH).
  2. systemd-oomd with PSI/swap kill confined to user.slice (ManagedOOMMemoryPressureLimit=50%, ManagedOOMSwap=kill) as a backstop — converts the multi-minute whole-box meltdown into a fast single-session kill. The session still dies, so it's damage-control, not a fix.

Ask: please move the embedded matcher off POSIX-ERE DFA construction for the .{0,N}…{0,M} case — ugrep -P (PCRE2), RE2, or the Rust regex crate all avoid the multi-GB compile. As filed, code-research can OOM the host, which shouldn't be possible from a search tool.

interkelstar avatar Jun 23 '26 01:06 interkelstar

Confirming the same failure family on Claude Code 2.1.199, Ubuntu 24.04 bare-metal.

Environment:

  • Claude Code: 2.1.199 native install
  • OS: Ubuntu 24.04, bare-metal Linux desktop
  • Kernel: 6.17.0-35-generic
  • RAM: about 24 GB, swap: 8 GB
  • Shell: zsh
  • Workload: multiple Claude sessions/subagents doing code

Observed behavior:

  • The kernel OOM killer killed 2.1.199 processes, with one observed at anon-rss:21407232kB.
  • Other OOM tables showed several concurrent 2.1.199 workers at multi-GB RSS.
  • Those workers were adjacent to head / sort pipeline processes, matching search commands generated by Claude.
  • The parent Claude sessions and MCP servers were not the large consumers in the OOM table.

Pattern shape:

  • The OOM windows included model-authored grep commands using bounded context extraction, for example:
    • grep -aoE ".{0,90}innerHTML=.{0,90}" file.js | head
    • grep -oE ".{0,N}...{0,M}" ... | head
    • grep -oiE ... | sort -u | head
  • This matches the . {0,N} ... . {0,M} / bounded interval family described here.
  • In my case the path was reached through Claude Code's zsh shell-snapshot grep shim, which re-execed the Claude binary as bundled ugrep:
    • ARGV0=ugrep "$_cc_bin" -G --ignore-files --hidden -I ...
  • So the visible process was 2.1.199, not a normal system grep process.

Mitigation verified:

  • Launching normal sessions with:
    • claude --allowedTools Bash,Grep,Glob ...
  • prevented new shell snapshots from installing the embedded ugrep / bfs shell shim.
  • New snapshots contain zero:
    • Shadow find/grep
    • ARGV0=ugrep
    • exec -a ugrep
  • After sourcing the new snapshot:
    • grep is an alias for grep --color=auto
    • find is /usr/bin/find
  • Memory stayed stable after restart, with active Claude parent processes around 400-500 MB and no runaway 2.1.199 -G --ignore-files ... worker.

This looks consistent with the same underlying bundled matcher problem: bounded interval regexes that are harmless under system GNU grep or ripgrep can balloon inside Claude Code's bundled search path, and parallel sessions/subagents multiply the blast radius.

ilyyeees avatar Jul 03 '26 12:07 ilyyeees

Confirming this on v2.1.207 (native Linux build), and adding two data points: the BRE path (-G, the shim's default for plain grep) is affected too, not just -E, and inside an LXC container the failure mode is worse than an OOM kill — it's a full container freeze.

Production incident (2026-07-11): an agent session ran what looked like ordinary grep commands with bounded repeats (grep -o '.\{0,200\}pattern.\{0,80\}' file.js) against a 187 KB minified JS file. The shim routed them to the embedded ugrep, which filled the container's entire 4 GiB cgroup in under two minutes. Because the allocation pressure came on gradually, the kernel never OOM-killed anything — the container sat pinned at memory.max in reclaim livelock (40–70 % CPU, host iowait 30–40 %), completely unresponsive to SSH/console. Only pct reboot recovered it. This happened twice in 20 minutes before we found the cause.

Measured comparison (same 122 KB synthetic text file, same pattern .\{0,200\}data_for_model.\{0,80\}):

binary peak RSS result
GNU grep 3.x 10.2 MB completes in 0.17 s
embedded ugrep 7.5.0 (exec -a ugrep claude -G) 2 565 MB SIGSEGV at a 3 GB rlimit, ~3 s

Repro:

python3 -c "print('\n'.join('x%d: { data_for_model: \"y-%d-block\" },' % (i,i) for i in range(1200)))" > /tmp/repro.js
# via the Bash-tool shim (or: exec -a ugrep ~/.local/bin/claude -G ...):
grep -o '.\{0,200\}data_for_model.\{0,80\}' /tmp/repro.js   # multi-GB allocation
command grep -o '.\{0,200\}data_for_model.\{0,80\}' /tmp/repro.js  # fine, ~10 MB

Embedded ugrep identifies as ugrep 7.5.0 x86_64-pc-linux-gnu.

Mitigations we've deployed (may help others until this is fixed):

  • systemctl set-property user.slice MemoryMax=3G MemorySwapMax=256M in the containers where agents run — the runaway ugrep gets OOM-killed (exit 137) instead of livelocking the container.
  • A CLAUDE.md rule to use command grep for any pattern containing {m,n} repeats.

Given that the model has no way to know grep isn't GNU grep, a cap or a fallback-to-command grep on patterns with counted repeats seems warranted; an opt-out for the shadowing (#69736) would also do.

andersmolausson avatar Jul 11 '26 14:07 andersmolausson

Independent confirmation — this is still present in both current channels:

  • stable 2.1.205 (build 2026-07-08) and latest 2.1.212 (build 2026-07-16) reproduce with near-identical growth curves (~130-150 MiB/s native anonymous RSS).
  • Our production incident: the Bash tool's shell-snapshot grep() wrapper silently routed a bounded-repeat regex (.{100}worker.{0,30}必須.{0,150}) over long-line JSONL into the embedded ugrep; one process reached 3.87 GiB anon RSS on an 8 GiB host → global OOM, and a second run with swap present caused ~19 min of thrashing requiring a manual console reboot.
  • Consistent with the DFA-construction diagnosis here: our synthetic fixture yields zero matches and growth starts immediately.
  • One forensics trap worth noting: a trailing | head masks the failure — the scanning ugrep is OOM-killed but the pipeline exit status becomes head's 0, so tool output looks like a clean empty result. Also exec -a ugrep only changes argv[0]; the kernel comm still shows the Claude version string, which misdirects OOM postmortems.

Bounded synthetic repro (fully synthetic fixture, 512 MiB cgroup cap, RSS sampler, exits with the scan's real status): https://github.com/qiyun-kxc/claude-embedded-ugrep-oom-repro

Workaround we run in production: CLAUDE_ENV_FILE executes unset -f grep before each Bash-tool command (verified: grep falls back to /usr/bin/grep), plus command grep discipline and a cgroup MemoryMax on the Claude Code service.

Wren (Claude Fable 5); investigation: Crane and Rowan (OpenAI Codex GPT-5.6 sol)

qiyun-kxc avatar Jul 17 '26 23:07 qiyun-kxc

Different failure mode from the same shim: when CLAUDE_CODE_EXECPATH is not a ugrep-capable binary, grep and find fail silently instead of OOMing

Adding a data point that sits next to this issue rather than another confirmation of the OOM. Same grep() wrapper, opposite symptom, and I would argue the silent variant is the more dangerous of the two.

Setup, stated up front because it is unsupported: this machine is a Core2Duo that cannot run the Bun-compiled binary (SIGILL, no AVX), so I run the extracted JS bundle of 2.1.212 under Node through a small loader (https://github.com/hibbes/claude-on-node). Consequently:

$ echo "$CLAUDE_CODE_EXECPATH"
/usr/bin/node

What the shim does with that. The generated snapshot contains the usual block:

function grep {
  local _cc_bin="${CLAUDE_CODE_EXECPATH:-}"
  [[ -x $_cc_bin ]] || _cc_bin=~/.local/bin/claude
  if [[ ! -x $_cc_bin ]]; then command grep ${1+"$@"}; return; fi
  ...
  (exec -a ugrep "$_cc_bin" -G --ignore-files --hidden -I --exclude-dir=.git ... ${1+"$@"})
}

The only guard is [[ -x $_cc_bin ]]. /usr/bin/node is executable, so the guard passes, the shim execs Node with argv[0] rewritten to ugrep, and hands it ugrep's options. Node rejects them one per line and exits 9. Nothing is ever scanned.

$ printf 'alpha\nbeta\ngamma\n' > t.txt

$ grep beta t.txt
ugrep: bad option: -G
ugrep: bad option: --ignore-files
ugrep: bad option: --hidden
ugrep: bad option: -I
ugrep: bad option: --exclude-dir=.git
[...]
$ echo $?
9

$ /bin/grep beta t.txt
beta
$ echo $?
0

find breaks identically through the bfs branch (bfs: bad option: -S).

Why this is worse than an OOM. An OOM is loud. This is not. The errors go to stderr and the exit code is 9, so anything written as grep -c pattern file 2>/dev/null or find ... 2>/dev/null | wc -l yields an empty result that reads exactly like "no matches" or "no such files". In one session the agent concluded that a freshly synced Gentoo ebuild repository had zero changed files. The real number was 1544, and the wrong answer was acted on before anything looked suspicious. The | head masking described in #78700 is the same class of problem; this variant does it on every invocation rather than only on pathological regexes.

Worth noting separately: exec -a rewrites argv[0] to ugrep, so the error text names a tool that is not present anywhere on the execution path. That cost me a while during diagnosis.

Suggested hardening. At call time the wrapper cannot cheaply establish whether $_cc_bin can act as ugrep or bfs. But the CLI knows this when it writes the snapshot. Emitting the shadowing block only when the running executable actually embeds those tools would close this at zero per-call cost, and it would give the "do not shadow the system grep" request from #78700 a natural opt-out for any install that does not carry them.

Mitigation, confirmed end to end here (essentially the CLAUDE_ENV_FILE approach from #78700):

# ~/.claude/bash-env.sh
unset -f grep find 2>/dev/null || true
// ~/.claude/settings.json
"env": { "CLAUDE_ENV_FILE": "/home/USER/.claude/bash-env.sh" }

This works because the session environment script is injected after the snapshot is sourced, so the unset strips the functions before the command runs. Afterwards type grep reports /usr/bin/grep, the failing calls above return correct results, and the repo scan that reported 0 reports 1544. For the record, the snapshot shadows exactly two functions, grep and find, so that single line is complete coverage.

I am not affected by the OOM itself, since no ugrep is ever reached on this install.

Investigated with Claude Code (Opus 4.8). All commands and outputs above are from the actual session.

hibbes avatar Jul 18 '26 09:07 hibbes

Independent confirmation on Claude Code 2.1.212, with two additions not established in the original report: the same 1-byte reproducer OOMs clean upstream ugrep 5.0.0, 7.5.0 and 7.8.2, and our incident shows the re-exec'd matcher survived the Bash timeout for approximately 33 minutes. Upstream report: Genivia/ugrep#549.

Incident (2026-07-17, self-hosted server running live services). An agent-issued tr ... | grep -oiE '.{0,70}dream.{0,90}' | sort -u | head -50 was routed through the shell-snapshot grep() shim into the embedded ugrep (7.5.0 per --version) on a 7.6 GiB host. The kernel OOM record identifies the large process as comm=claude.exe; the shell snapshot and process invocation establish that this was the re-exec'd matcher with argv[0]=ugrep, matching the process-identity trap described by @qiyun-kxc. ~30 minutes of swap-thrash made the host unreachable (DNS/VPN services starved) before the global OOM kill:

oom-kill:constraint=CONSTRAINT_NONE,...,global_oom,
  task_memcg=/user.slice/user-<uid>.slice/.../tmux-spawn-....scope,
  task=claude.exe,... total-vm:12848296kB, anon-rss:6654848kB

Our incident command also ended in | head -50, so the pipeline-status masking described by @qiyun-kxc applies to our case as well.

Process-attribution clarification for this incident. In our task table, the long-lived CLI session remained at approximately 216 MiB, while the re-exec'd search process held approximately 11.15 GiB across RSS and swap. This incident therefore involved a separate matcher process using the Claude executable image, not growth in the long-lived session process. This does not rule out unrelated session-heap bugs.

Upstream verification (new). We rebuilt clean ugrep from tags and ran the 1-byte reproducer under a 512 MiB cgroup (MemorySwapMax=0):

Build Result Time to 512 MiB
Ubuntu 24.04 pkg 5.0.0 OOM-kill 3.9 s
upstream v7.5.0 (tag build) OOM-kill 3.9 s
upstream v7.8.2 (tag build) OOM-kill 5.7 s

All three tests used the same printf 'x' fixture and the same -E '.{0,70}dream.{0,90}' pattern. The bundling is not the cause; the engine behavior is upstream. Filed with the narrowed reproducer as Genivia/ugrep#NNN.

Additional controls agree with the OP's compile-time diagnosis: either one-sided pattern completes at approximately 8 MiB; .{0,8}dream.{0,8} completes; .{0,70}dream.{0,90} reaches 512 MiB on a 1-byte, zero-match file. -o, -i, input delivery method and match presence do not affect the failure. -P with the identical pattern completes at ~8 MiB (verified on the embedded build, which ships -P:pcre2jit, and on the Ubuntu 5.0.0 package; our from-tag builds were compiled without PCRE2 so -P could not be tested there).

Independent lifecycle amplifier: in our incident, the 120-second Bash timeout returned while the pipeline continued as an unsupervised background job for approximately 33 additional minutes. We reproduced this behavior with a benign non-terminating command. This appears to overlap #76056 and should be fixed independently of the regex bug.

Current mitigations:

  • Use /usr/bin/grep or command grep to bypass the injected shell function.
  • Where appropriate, use -P for this pattern family.
  • Run agent sessions inside a cgroup as a damage-containment boundary, for example: systemd-run --user --scope -p MemoryMax=4G -- claude

Investigated with Claude Fable 5, Claude Opus 4.8, and GPT-5.6-sol; all runs were operator-verified.

wanderlust-n avatar Jul 18 '26 12:07 wanderlust-n

Independent confirmation on 2.1.217 (native Linux x86_64, installed 2026-07-21). Adding four things I don't see established yet in this thread: the quantified growth law (with a table you can regression-test without OOMing CI), the non-exploding neighbor patterns (which yield a precise danger predicate), child-side allocation evidence (relevant to which mitigations can work), and the upstream fix status (merged but unreleased — a version bump alone won't fix this today).

Growth law: ≈2^min(N,M), doubling per +1 on the smaller bound

Measured today on 2.1.217, exec -a ugrep <claude-binary> under systemd-run --scope -p MemoryMax=2G, peak RSS via /usr/bin/time -f %M, input = a 1-byte file (corpus-independent, this is DFA construction at pattern-compile time):

pattern (-E, 1-byte input) peak RSS
.{0,10}X.{0,10} 9 MB
.{0,14}X.{0,14} 56 MB
.{0,15}X.{0,15} 110 MB
.{0,16}X.{0,16} 219 MB
.{0,17}X.{0,17} 444 MB
.{0,20}X.{0,20} 2 GB cap hit, killed

Clean doubling per +1 on the smaller bound. The context-grabbing patterns the model likes to write (.{0,60}word.{0,90}) are astronomically past any RAM size, which matches every incident report above. -G BRE (.\{0,20\}X.\{0,20\}) hits the 2 GB cap identically — confirming @andersmolausson.

CI-safe regression test: the {0,16} pair peaks at ~220 MB on a 1-byte file. No need to OOM anything to detect the pathology in CI.

What does not explode (same harness)

pattern peak RSS
.{0,200} (single bounded quantifier, however large) 7 MB
.{0,200}X.{0,5} (asymmetric: min bound = 5) 15 MB
[0-9]{0,60}X[a-f]{0,90} (two big bounds, disjoint classes) 7 MB
-P '.{0,20}X.{0,20}' (PCRE2) 7 MB
-P '.{0,60}X.{0,90}' (PCRE2, incident-sized) 7 MB

So the danger predicate is precise: two-plus bounded quantifiers over overlapping character classes, and cost is driven by min(N,M) — not by pattern size, corpus, or a single large bound. -P is immune even at incident-sized bounds (PCRE2 backtracks instead of building a DFA).

Where the memory sits: the re-exec'd child

In our reproductions the memcg scope contains only the re-exec'd argv0=ugrep child — the CLI is not inside it — and the child alone reaches the 2 GB cap. In-session capture while a shadowed grep ran: comm=2.1.217 cmd=ugrep -G --ignore-files --hidden -I ... (same process-identity trap described by @qiyun-kxc and @wanderlust-n: kernel OOM dumps show the claude version string as comm, so the blame lands on the CLI by mistake).

We lost two long-running agent sessions in 6 days this way (OOM-killed children at 15 GB and 27.8 GB RSS on a 32 GB host). This may well coexist with the in-process V8 growth @interkelstar captured, but for this family of incidents a child-side cap would have prevented every one of ours.

Upstream status: fixed on master, released nowhere

Genivia/ugrep#549 was closed completed on 2026-07-19 (DFA growth will be constrained to the VM opcode ceiling), but the latest ugrep release is still v7.8.2 (2026-05-17), which the upstream report lists as affected (5.0.0–7.8.2). Claude Code bundles 7.5.0. So today, no version bump fixes this — the shim needs its own guard.

Practical mitigations

Shim-side (one line, product): the snapshot grep() function could set ulimit -v before the re-exec. Given the child-side evidence above, this converts a host-freeze into a clean grep: killed failure. Routing risky patterns (≥2 bounded quantifiers, min-bound ≥ ~16) to -P, or to the already-embedded ripgrep, are stronger fixes.

User-side, works today (PreToolUse hook): we run this and it has already intercepted real model-authored killers. Intentionally coarse — a false positive costs a pattern rewrite, a false negative costs the host:

#!/usr/bin/env python3
# ~/.claude/hooks/grep-quantifier-guard.py — PreToolUse[Bash]
# Blocks Bash commands invoking (shadowed) grep with >=2 bounded quantifiers:
# the bundled ugrep's DFA needs ~2^min(N,M) states at pattern-compile time.
import json, re, sys
try:
    d = json.load(sys.stdin)
except Exception:
    sys.exit(0)
if d.get("tool_name") != "Bash":
    sys.exit(0)
cmd = (d.get("tool_input") or {}).get("command") or ""
if not re.search(r"(^|[\s|;&(`])\\?grep\s", cmd):
    sys.exit(0)
if re.search(r"(^|\s)-[A-Za-z]*[PF]", cmd):  # PCRE2 / fixed-strings: safe
    sys.exit(0)
quants = [max(int(g) for g in m.groups() if g)
          for m in re.finditer(r"\\?\{(\d+)?,?(\d+)?\\?\}", cmd)
          if any(m.groups())]
if len([q for q in quants if q >= 10]) >= 2:
    sys.stderr.write(
        "Blocked: >=2 bounded quantifiers explode the bundled ugrep DFA "
        "(~2^min(N,M) states; {0,20} twice already exceeds 2 GB before "
        "reading input). Safe rewrites: add -P (PCRE2, measured 7 MB on the "
        "same pattern); use `command grep` (GNU); or keep a single bounded "
        "quantifier / disjoint classes.\n")
    sys.exit(2)
sys.exit(0)
{ "hooks": { "PreToolUse": [ { "matcher": "Bash", "hooks": [ { "type": "command",
  "command": "python3 ~/.claude/hooks/grep-quantifier-guard.py", "timeout": 10 } ] } ] } }

(Caveats we verified so the hook doesn't over-block: child scripts (bash foo.sh, bash -c) get PATH grep — the shim function is not exported — and execvp wrappers (sudo/xargs/command grep) also bypass the shim. Only direct-position grep in the tool's shell, plus eval and keyword prefixes like time, route into ugrep.)

yvonboulianne avatar Jul 22 '26 06:07 yvonboulianne

Follow-up to my earlier comment: USE_BUILTIN_RIPGREP=0 closes only one of the two paths into the embedded matcher.

That variable covers the Grep tool. Our box kept OOMing after it, because the Bash tool reaches ugrep by a different road: the shell snapshot installs grep() / find() functions that re-exec the CLI as ugrep/bfs, so any model-authored grep inside a Bash command still hits the exploding matcher. On this host all 35 files in ~/.claude/shell-snapshots/ carried the shim. @qiyun-kxc and @hibbes already pointed at CLAUDE_ENV_FILE; this is the same idea stated as a complete recipe, since "I set USE_BUILTIN_RIPGREP=0 and still get killed" keeps coming up.

Workaround (verified on 2.1.216 / 2.1.217, Fedora 44):

  1. Create ~/.claude/bash-env.sh:
unset -f grep find 2>/dev/null || true
  1. Point CLAUDE_ENV_FILE at it, in ~/.claude/settings.json:
{ "env": { "USE_BUILTIN_RIPGREP": "0", "CLAUDE_ENV_FILE": "/home/USER/.claude/bash-env.sh" } }

A plain exported variable works too — verified by removing the key from settings.json and launching with CLAUDE_ENV_FILE=… claude -p …, which still reported the system grep. Useful if you provision boxes from a shell rc rather than editing JSON.

  1. Verify in a new session — the env file is sourced after the snapshot, so running sessions keep the shim:
type grep    # → grep is /usr/bin/grep
type find    # → find is /usr/bin/find

Both grep and find need unsetting: the snapshot shadows both, and the find shim has its own failure mode (@hibbes' silent-empty-results case).

If you would rather have Claude Code apply it for you, paste this into a session:

Claude Code routes Bash-tool grep/find through its embedded ugrep, which can OOM the
host on bounded-interval regexes (anthropics/claude-code#67021). Please close that path:

1. Create ~/.claude/bash-env.sh containing exactly:
       unset -f grep find 2>/dev/null || true
2. In ~/.claude/settings.json, add "CLAUDE_ENV_FILE" pointing at that file (absolute
   path) inside the "env" object, creating "env" if absent and keeping existing keys.
3. Tell me that only sessions started after this change are affected, then verify in a
   fresh session that `type grep` prints /usr/bin/grep and not a shell function.

This is containment, not a fix — the engine behaviour reported here (and upstream in Genivia/ugrep#549) is unchanged; it just stops Claude Code from routing searches into it. A cgroup cap (systemd-run --user --scope -p MemoryMax=4G -- claude) is still worth having as a backstop, as others noted above.

interkelstar avatar Jul 22 '26 17:07 interkelstar

Five weeks of field data from one box, posted because it bears on @yvonboulianne's shim-side ulimit -v proposal. A child-side cap would have contained every incident we recorded, and it is the only mitigation we tried that would have.

The box is WSL2 (Ubuntu) with 47 GiB assigned and 8 GiB swap, running Claude Code 2.1.214 through 2.1.218. Two OOM kills took it down with nothing left to attribute them to, so we put a systemd user timer on it: sample every minute, record /proc/<pid>/cmdline, ppid and the parent chain for any comm=claude.exe process above 8 GiB, SIGKILL above 12 GiB.

That caught 11 distinct runaway processes in 5 days (2026-07-18 to 2026-07-23, 15 alert rows, still reproducing on 2.1.218). Peak RSS per process: 8.6, 8.6, 12.9, 13.5, 14.1, 14.6, 15.7, 16.7, 17.2, 18.3, 18.6, 22.7 GiB.

Every one of them carries the shell snapshot's fixed flag prefix in argv: -G --ignore-files --hidden -I --exclude-dir=.git .... Not one arrived by another route. This is the native Linux build, which has no separate Grep tool, so the Bash shim is the only door into ugrep here and the opt-out asked for in #69736 would be a complete fix rather than a partial one. The bounds that showed up in the wild, all model-authored and none typed by the user: .{0,40}, .{0,60}, .{0,90}, .{0,120}, .{0,150}, .{0,180}, [^<>]{0,200}, .{0,300}, in both -E and -G form.

The growth rate decides which mitigations are even possible. Two consecutive samples 70 seconds apart, on 2026-07-23: 10.13 GiB, then 22.72 GiB. Roughly 180 MB/s sustained, searching a 569 KB single-line HTML file for .{0,180}(multiple accounts|more than one account|one account per|separate accounts).{0,180}. Alerting alone is worthless at that speed. Our first version only paged, and by the time a human read the notification the box was already thrashing, which is why the threshold kill went in.

The memory sits in the child, and killing the child is enough. The sampler line one minute before that breach reads OK: 10 claude process(es), largest 0.59 GiB. A minute later one argv0=ugrep child was at 10.13 GiB while the CLI parent that spawned it was still at 0.52 GiB. Killing the child recovered the machine every time and cost one search instead of the session. So ulimit -v in the snapshot's grep() before the re-exec would have turned all 11 of these into a clean Killed, with no host impact and no lost work.

Two things worth knowing if you are diagnosing this from outside. comm is the CLI version string for these processes, so dmesg and ps blame Claude Code itself, and the two OOM kills that started our investigation are still unattributed because nothing captured argv at the time. Capturing /proc/<pid>/cmdline at the threshold is the entire diagnosis.

The other one: the OOM is the loud failure. On the same box the same shim silently returns empty with exit 1 on a text file holding a single NUL byte (the -I default, #56644), which is indistinguishable from "no matches". That variant is the one that actually produced a wrong answer here.

We filed #78834 before finding this thread. Same bug, with an isolation table for the fixed-versus-variable bound question. Happy to close it as a duplicate of this one.

Helban avatar Jul 23 '26 09:07 Helban

Independent confirmation on Claude Code 2.1.219 (Ubuntu 22.04 x86_64).

During an ordinary Bash tool call, Claude Code's shell snapshot shadowed grep and re-executed the Claude Code binary as the bundled ugrep. A two-sided bounded-repeat ERE on a 9 KB text file produced no result for approximately 12 minutes.

The re-executed matcher reached approximately 3.33 GB anonymous RSS on a 3.8 GiB host, exhausted 2.3 GiB of swap, and triggered a global OOM. The main Claude Code process was only approximately 152 MiB at the OOM snapshot. The resulting reclaim pressure stalled unrelated interactive sessions and restarted systemd-journald via its watchdog.

Guarded reproduction using a fully synthetic fixture, a 512 MiB virtual-memory limit, and a five-second timeout:

  • Claude Code 2.1.219 bundled matcher: SIGSEGV after 2.97 seconds, max RSS 232072 KiB
  • Saved Claude Code 2.1.218 binary: SIGSEGV after 3.20 seconds, max RSS 234700 KiB
  • GNU grep with the same ERE: completed in 0.04 seconds, max RSS 3888 KiB

The upstream issue Genivia/ugrep#549 confirmed the DFA-construction blow-up and landed a patch on July 19:

https://github.com/Genivia/ugrep/issues/549 https://github.com/Genivia/ugrep/commit/94c30f7dfd668b0567ca6474f58f0f0f5ef758b1

No fixed upstream release is available yet. Since Claude Code 2.1.219 still contains the affected path, please consider vendoring the upstream patch, or adding a shim-side complexity/resource limit or fallback until a patched ugrep release can be bundled.

gobly2333 avatar Jul 25 '26 07:07 gobly2333

adding a shim-side complexity/resource limit or fallback until a patched ugrep release can be bundled.

shim-side already described here https://github.com/anthropics/claude-code/issues/67021#issuecomment-5049415197

interkelstar avatar Jul 25 '26 10:07 interkelstar

adding a shim-side complexity/resource limit or fallback until a patched ugrep release can be bundled.

shim-side already described here https://github.com/anthropics/claude-code/issues/67021#issuecomment-5049415197

Thanks for the helpful pointer! We’ve already deployed a local workaround on our side. My comment was mainly intended to provide an independent confirmation and document that the affected path was still present in Claude Code 2.1.219.

gobly2333 avatar Jul 28 '26 08:07 gobly2333

Additional data point: macOS ARM64, 2.1.221 — a single bounded repeat is enough, and here it is match-time, not compile-time

Still reproducing on the current build. Two details differ from the characterization in this thread and in #67649, so posting them here rather than opening a new issue (#64133 carried the previous macOS evidence and is now locked).

Environment

  • Claude Code 2.1.221 (native ARM64), macOS 25.5.0, 24 GB RAM
  • Wrapper: the function grep shell-snapshot shim, which execs ugrep -G --ignore-files --hidden -I --exclude-dir=.git ... plus the user's own args
  • Baseline: /usr/bin/grep (BSD grep)

Difference 1 — one bounded repeat is sufficient

#67649 lists "single bounded repeat on either side of the core" as fine, and this issue's title says two bounded intervals. On this build a pattern with one {0,N} blows up just as reliably:

^E +[A-Za-z_.]+(Error|Exception)[^,]{0,60}

Only one counted repeat; the other quantifier is a plain unbounded +.

Difference 2 — match-time here, not compile-time

The 2-byte-input repro from #67649 does not reproduce on this build. Both that issue's pattern and mine return instantly with no input:

echo x | grep -oE '[^>]{0,100}credential[^<]{0,200}'   # exit 1, instant
echo x | grep -oE '^E +[A-Za-z_.]+(Error|Exception)[^,]{0,60}'   # exit 1, instant

Both hang only once there is real input to scan, so on macOS/2.1.221 the blowup is in matching, not in automaton construction. Whatever guard now short-circuits the compile-time case does not cover the match-time path.

Repro

Generate ~1.5 MB of varied log-like text (a quarter of the lines matching):

import random, string
random.seed(7)
mods = ['pkg.alpha','pkg.beta','sqlalchemy.exc','docker.errors','httpx','pydantic.v1','builtins','asyncio.base']
lines = []
while sum(len(l) for l in lines) < 1_500_000:
    if random.random() < 0.25:
        lines.append("E   %s.%s%s: %s" % (
            random.choice(mods),
            ''.join(random.choices(string.ascii_letters, k=random.randint(4, 12))),
            random.choice(['Error', 'Exception']),
            ''.join(random.choices(string.ascii_letters + ' ', k=random.randint(10, 90)))))
    else:
        lines.append(''.join(random.choices(string.ascii_letters + string.digits + ' ./_:', k=random.randint(20, 120))))
open('big.txt', 'w').write('\n'.join(lines) + '\n')
grep -oE "^E +[A-Za-z_.]+(Error|Exception)[^,]{0,60}" big.txt   # never completes, RSS climbs
command grep -oE "^E +[A-Za-z_.]+(Error|Exception)[^,]{0,60}" big.txt   # 0 s, 4599 matches

Memory is exponential in the single bound

Same input and wrapper, varying only N. Peak RSS sampled once per second; the last two rows were killed at 15 s while still climbing, so their peaks are cut-offs, not asymptotes.

bound outcome peak RSS
{0,20} completes 1 s ~0 MB
{0,30} completes 1 s 13 MB
{0,35} completes 3 s 334 MB
{0,38} completes 6 s 1100 MB
{0,40} completes 13 s 1955 MB
{0,45} >15 s, killed 1476 MB (climbing)
{0,60} >15 s, killed 1075 MB (climbing)

Roughly a doubling per +2–3 of the bound. /usr/bin/grep completes every one of these in 0 s.

Observed impact

An ordinary log-aggregation one-liner — grep -oE "^E +[A-Za-z_.]+(Error|Exception)[^,]{0,60}" "$F" | sort | uniq -c | sort -rn — run against a 1.45 MB pytest log took 31 minutes and reached ~17 GB before finishing. On a 24 GB machine that drove swap to 23.1 GB of 23.5 GB used and pushed kernel_task to 246 % CPU from memory-compressor churn. Swap was reclaimed (23.5 GB → 18.4 GB) the moment the process exited.

The macOS-specific hazard is that the symptom surfaces as kernel_task CPU rather than as the offending process, so the cause is hard to attribute — ps RSS for the process read 83–461 MB throughout, because most of its dirty pages were already compressed or swapped out, while top showed 14–17 GB.

Not verified

  • Whether -G (which the wrapper always injects, alongside the user's -E) contributes. Isolation runs were inconsistent and I did not settle it; since the wrapper always injects it, it is a constant in real use.
  • Whether stock Genivia ugrep reproduces this. I did not test outside the bundled binary.
  • The exponential-in-bound growth is inferred from the table above; I did not inspect the engine internals.

devseunggwan avatar Aug 04 '26 09:08 devseunggwan

Confirming on 2.1.224 (native Linux, 2026-08-07 build). Newest version reported in this thread so far appears to be 2.1.212 (2.1.220 in #83342).

Two data points I don't believe are in the thread yet:

1. A hang variant that never triggers the OOM killer. On a 4-core / 8GB VPS, the embedded ugrep plateaued at ~2.6-2.75GB RSS and then spun CPU indefinitely instead of growing further: one instance ran 2.4h, a second ran overnight. Because RSS stabilised below the OOM threshold, the kernel never intervened. The host sat at load 15-19 with sibling processes in D-state, and the agent session blocked on the tool call indefinitely (to the operator it looks like the session crashed). Both patterns were model-authored during routine web scraping of small HTML pages (112KB file, longest line 93K chars):

  • grep -oiE '[^<>"]{0,90}(review|rating|ratingValue|reviewCount|out of 5|stars)[^<>"]{0,90}' page.html
  • grep -ioE '[^<>]{0,80}(Lucy Strong|Fleetwood)[^<>]{0,80}' page.html

The same session generated the pathological shape twice in under 24 hours. "Extract text around a keyword from scraped HTML" seems to be a shape agents reach for constantly, so exposure is high for any web-research workflow.

2. Recovery detail for anyone landing here with a "frozen" session: plain SIGTERM to just the ugrep pid unwinds the pipeline and the blocked session resumes cleanly mid-task. Nothing else needs restarting. Triage: ps aux --sort=-%cpu, kill the ugrep, wait a few seconds.

Also confirming the CLAUDE_ENV_FILE / unset -f grep find mitigation from this thread works on 2.1.224 (verified type grep returns /usr/bin/grep after snapshot + env file sourcing).

HarrisonHBUFC avatar Aug 08 '26 10:08 HarrisonHBUFC

A few observations from CLI 2.1.228 on Linux, in case they're useful.

On our host the runaway was a separate child process. It grew to 6.2 GB while the parent claude process stayed flat around 550 MB. One detail that made this confusing to read: the CLI re-execs itself with exec -a ugrep, so the child's comm shows the version string (2.1.228) rather than ugrep. If that's what's showing up in OOM tables elsewhere in this thread as the CLI process itself, it might be the same thing wearing a misleading name — though I can only speak to what we saw here, and other setups may genuinely differ.

USE_BUILTIN_RIPGREP=0 didn't cover our case. It routes the Grep tool through system ripgrep, but the shell snapshot (~/.claude/shell-snapshots/*.sh) separately shadows plain grep in Bash calls with a bash function that re-execs the CLI as ugrep. In the snapshots we've looked at, that block is emitted unconditionally, while the sibling rg shadow is guarded by command -v rg. So model-authored grep -oE … in a Bash call still reached ugrep for us with that variable set.

The wrapper passes -G, but a caller-supplied -E lands later on the command line and wins — so the safe default didn't hold for the usage that triggers this.

It's a shell function rather than a PATH shim, which makes which grep report /usr/bin/grep while grep --version reports ugrep 7.5.0. That cost us some time while debugging, and it means a PATH override isn't a workaround.

With zero swap we saw no OOM-kill at all. The reports above have the OOM killer firing, with swap or zram present. Ours had none: journalctl simply ends mid-operation — no OOM, no panic, no shutdown — and the machine needed a manual reset. Mentioning it because that signature initially looked to us like a host/hypervisor fault rather than a memory bug.

What we're running in the meantime, in case it's useful to anyone else on the Bash path: a PreToolUse hook that returns updatedInput prepending unset -f grep 2>/dev/null; to any Bash command invoking grep. That drops the shadow function for that one call, so grep resolves through PATH to GNU grep, which compiles these patterns in milliseconds. We prepend rather than rewrite the command text, so shell quoting and heredocs are untouched.

JonoGrubb avatar Aug 12 '26 19:08 JonoGrubb

Confirming this exact failure signature on Fedora Linux with the npm-installed Claude Code executable, during the 2.1.226-era installation.

Two Bash-tool calls used ordinary grep:

grep -a -o -E '.{0,120}selectSuggestion\(.{0,260}' <obsidian.asar>
grep -a -o -E '.{0,120}onChooseSuggestion.{0,260}' <obsidian.asar>

Claude Code's injected shell grep() function routed both calls through:

exec -a ugrep "$CLAUDE_CODE_EXECPATH" -G --ignore-files --hidden -I ...

The shell recorded the exact subprocess deaths:

PID 2640159 Killed (exec -a ugrep ...)
PID 2640573 Killed (exec -a ugrep ...)

Those PIDs match the kernel OOM records:

Killed process 2640159 (claude.exe)
anon-rss:10150984kB

Killed process 2640573 (claude.exe)
anon-rss:11242436kB

PID 2640573 increased from approximately 1.35 GiB RSS to approximately 10.72 GiB RSS in 127 seconds. Together, the two bundled-ugrep processes exhausted approximately 31 GiB physical RAM and 8 GiB ZRAM, triggering a global OOM cascade that also killed unrelated applications.

Both expressions have the already-reported two-sided bounded-wildcard form:

.{0,N} literal .{0,M}

This incident therefore confirms that the injected ordinary-grep wrapper still exposes the DFA/state-explosion failure in a current real-world workload. It also explains why the OOM victims appeared as claude.exe: they were the Claude Code executable re-executed with argv[0] set to ugrep.

No private transcript or application content is needed to reproduce the pattern.

cybersader avatar Aug 12 '26 20:08 cybersader

Still present in 2.1.229, with a severity escalation: on a 16 GB RAM Ubuntu laptop (kernel 7.0.0-29-generic) this froze the whole host three times in one day. Two of the freezes ended with the power button; 4 GB of swap filled, every UI process stalled, and the kernel OOM killer fired on only one of the three occasions. When it did fire:

Out of memory: Killed process 32836 (2.1.229) total-vm:17097484kB, anon-rss:12436692kB, file-rss:8kB, shmem-rss:0kB, pgtables:29804kB, oom_score_adj:200

The trigger was a model-written search inside a Workflow agent, grep -oE '.{0,400}TOKEN.{0,400}' page.html, routed by the shell-snapshot grep function into the bundled ugrep. This confirms the compilation-phase finding at larger N: with .{0,400} on both sides, a 1-byte input SIGSEGVs at a ulimit -v 2097152 cap in ~8.5 s, identical timing to a 1 MB input. Uncapped, it reached the 12.4 GB shown above.

Two aggravators that may be worth folding into a fix:

  1. Session resume replays the detonation. The pattern ran as an unfinished Workflow step, and claude -r replays such steps, so every resume re-froze the machine (thrash + power cycle, kernel OOM kill, thrash + power cycle, across ~80 minutes). A compilation-memory bug inside a replayable step turns one bad pattern into a persistent trap.
  2. Neither visible process name points at Claude Code. System monitors show argv0 ugrep; the kernel's kill line shows the version-named binary (2.1.229). Diagnosis required kernel logs plus reading the shell snapshot; a user watching a system monitor has no way to connect a 12 GB "ugrep" to the CLI's grep shim.

Failure-mode contrast on the same 1 MB single-line input: GNU /usr/bin/grep -oE with the identical pattern grinds CPU (still running when stopped at 3 min) but holds a flat ~59 MB RSS. The bundled ugrep expresses the same pattern's cost as resident memory instead, which is what takes the host down. Related: #86238, #83342, #76056.

Workaround adopted on this machine: command grep / /usr/bin/grep in Bash-tool calls, and no .{0,N} double windows over fetched pages.

mill-master avatar Aug 13 '26 08:08 mill-master

Another confirmation on 2.1.229 (Debian 12, arm64 VPS, 16 GB RAM), with three data points I don't see in the thread yet:

1. Character-class bounded intervals trigger it too, with a slow-growth profile. Our pattern used [^"]{0,200} rather than .{0,N}:

grep -oiE '"[^"]{0,200}(47798|47799|4780[0-9]|Krefeld)[^"]{0,200}"' file.txt   # 79 KB input

Unlike the instant compile-time explosion in the OP, this grew steadily at roughly 60 MB/s for minutes (sampled 900 MB → 1.4 GB over 12 s) until it hit 10 GB. GNU grep on the identical command and input stays flat at 4.6 MB (it's CPU-pathological there, but memory-safe).

2. The Bash tool's timeout makes it worse, not better. The call hit the 120 s tool timeout and was "moved to the background" — after which the ugrep child kept ballooning unsupervised while the session continued other work. It died 4 minutes later at the cgroup cap. Nothing in the UI indicates the backgrounded process is eating memory.

3. Workaround for systemd-wrapped sessions. We launch the CLI via systemd-run --user --scope -p MemoryMax=10G to protect the host. The kernel's memcg OOM correctly kills only the runaway ugrep child — but systemd's default OOMPolicy=stop then tears down the scope and the whole session with it. Adding -p OOMPolicy=continue keeps the session alive; only the runaway dies:

claude() { systemd-run --user --scope -q -p MemoryMax=10G -p MemorySwapMax=2G -p OOMPolicy=continue "$HOME/.local/bin/claude" "$@"; }

This killed two sessions on our box in one day (kernel log: constraint=CONSTRAINT_MEMCG … Killed process (2.1.229) anon-rss:10128328kB). A memory ceiling (or -P/PCRE2 fallback, per the OP's finding) on the embedded ugrep would fix the class of failure at the root.

derspotter avatar Aug 13 '26 09:08 derspotter

Corroborating on 2.1.226 / 2.1.231 / 2.1.233 (Linux 6.12.33, 31 GB, no swap), with one consequence I don't see in the thread yet: on a systemd-managed host the OOM kill can take the supervisor with it, which is what turns a ~10-second recovery into a multi-hour outage.

First, confirming what's already here so it's clear this is a duplicate and not a new report: two bounded intervals (grep -n -o '.{0,120}X.{0,120}'), linear growth at ~110 MB/s, and the parent claude process staying flat at ~500 MB while the child reached 13.4 GB. @JonoGrubb's 08-12 note about comm showing the version string rather than ugrep is exactly right — we rediscovered that independently two days later after spending five days believing the agent process was the one ballooning. The kernel's own record actively misleads:

Out of memory: Killed process 2760416 (2.1.226) total-vm:14986464kB anon-rss:13305216kB
Out of memory: Killed process 3902449 (2.1.231) total-vm:14999488kB anon-rss:13041024kB

The part that may be new: blast radius under systemd

A tmux-spawned session lives in a transient tmux-spawn-<uuid>.scope. That scope's default OOMPolicy is stop, so when the kernel kills the ugrep child, systemd tears down the entire scope — including the wrapper script whose only job is to respawn the agent:

tmux-spawn-<uuid>.scope: A process of this unit has been killed by the OOM killer.
tmux-spawn-<uuid>.scope: Failed with result 'oom-kill'.

Three outages, each 6–7 hours, all of them ending only when a human noticed. The supervisor was working correctly; it had been destroyed in the same blast as the process it was there to restart.

Mitigation, verified

# ~/.config/systemd/user.conf.d/oom.conf
[Manager]
DefaultOOMPolicy=continue

Confirmed by an A/B test with a sibling process in the same scope (sibling survived the kill), and it has since held through three production ugrep kills — including an 11.7 GB excursion — with the child dying, the scope surviving, and the agent back in ~10 seconds.

⚠️ This is a fix for the blast radius, not for the allocation. The ugrep process still dies and the host still spends a couple of minutes under severe memory pressure. But if you run a long-lived agent session under tmux/systemd, this is the difference between a blip and an outage you find out about hours later.

One observation on detection, offered as a negative result

We spent some time trying to predict these from system-pressure signals so we could pre-emptively sleep the session. It doesn't work, and the data was unambiguous: our candidate predicate (ARC > 10 GB ∧ available < 5.5 GB) held for 81% of an 8-hour night with zero kills, and the single worst pressure reading of that window was strictly worse than both fatal events with nothing dying. Separately, cgroup memory.peak is useless as a trigger here — it's a scope-lifetime monotonic high-water, so after the first large event it is structurally blind; an 11.7 GB excursion moved it by +0. memory.current polling plus the cgroup oom_kill counter is the pair that actually reports.

⇒ Since the allocation is fatal at essentially every state of the box, recovery is worth more than prediction — which is the only reason the scope-policy detail above is worth posting.


Written by the Claude Code agent that operates the affected host; reviewed and posted by the account owner.

teaguejb avatar Aug 15 '26 19:08 teaguejb

Closing as a duplicate of #74143 — please follow and 👍 that issue instead so we can track it in one place. If you think this was closed in error, comment here and we'll reopen.

🤖 Generated with Claude Code

bcherny avatar Aug 17 '26 03:08 bcherny