Check str case methods and re.escape against max_memory #1389
All Vouch review confidence checks passed
Residual risk 0.22 = Change Risk (0.50) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.50 · 597 lines diff across 7 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.383
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1389"
general
weight: 0.35
davidhewitt
Summary
With
max_memoryset, thestrcase methods andre.escapecan kill the worker instead of raisingMemoryError:monty --max-memory 12MB -c "s = 'x' 9_000_000; y = s.upper()"
monty worker: allocation of 9000000 bytes exceeds the memory limit (exit 65)
monty --max-memory 8MB -c "import re; y = re.escape(' ' 4_500_000)"
monty worker: allocation of 9000000 bytes exceeds the memory limit (exit 65)
Both build their result in a plain
Stringthat the tracker never sees, so a large enough input crosses the hard ceiling before the next checkpoint. Lowercasing or escaping a document that is a good fraction of the budget is ordinary code, so this seemed worth the same graceful path the split and findall preflights have.Changes:
re.escape: pre-check the existing2 lenreservation withcheck_estimated_size. The loop is unchanged.lower/upper/casefold: pre-checks.len(), which is exact.capitalize/title/swapcase) goes through a newmap_casehelper inunicode_type.rs. A mapping can be longer than its input ('ɐ'.upper()is 3 bytes from 2,'ΐ'.upper()is 6 from 2), so a check sized to the input is not enough. For inputs overLARGE_RESULT_THRESHOLD / 12bytes,map_casechecks the initial buffer and every later growth withcheck_allocation, and maps in chunks that cannot reallocate behind the check. Smaller inputs take the old path unchanged. I didn't useStringBuilderhere because its 2x growth would refuse results that fit, and the per-charResultcost about 20% on short non-ASCII strings.limitations/resource_limits.md,docs/resource-limits.mdand CLAUDE.md, and droppeds.to_lowercase()from CLAUDE.md's "already bounded" examples.bytes.lower()and friends are left alone because #1208 already addscheck_allocationto them.Tests
large_allocations_are_rejected_before_the_hard_limit: ASCIIlowerandtitle,'é'(non-ASCII, same length),'ΐ'.upper()(fails in the growth check, not the initial one) andre.escape. On main the test fails withmonty worker: allocation of 16777216 bytes exceeds the memory limit/unexpected EOF from worker. With this change it passes.test_cases/str__methods.pywith inputs over the threshold, so the chunked path is checked against CPython (expansion, final sigma, title, swapcase, capitalize, casefold).cargo test -p monty-runtime --test subprocess,cargo test -p montyandmake test-casesall pass, along withmake format-rs lint-rs lint-py lint-md.ɐ ΐ İ é ß), 4 methods and 15 sizes up to 15 MiB at a 24 MiB limit: main killed the worker in 8 of 300 runs, this branch in 0. Those 8 now raiseMemoryError, and every other run gives the same result on both.upper()is about 30% faster, because it reallocates a few times instead of doubling.re.escapeis unchanged.<!-- This is an auto-generated description by cubic. -->
---
Summary by cubic
Fixes
strcase methods andre.escapekilling the worker withmax_memoryset instead of raisingMemoryError. Both previously built their result in a plainStringthat the memory tracker never saw, so a large enough input crossed the hard ceiling before the next checkpoint.re.escapepre-checks its existing2 lenreservation withcheck_estimated_size; the loop is unchanged.lower/upper/casefoldpre-checks.len(), which is exact.map_casehelper that checks the initial buffer and every later growth for inputs overLARGE_RESULT_THRESHOLD / 12bytes; smaller inputs take the old path.bytes.lower()and friends are left alone; #1208 already addscheck_allocationto them.<sup>Written for commit 210682f1396a23dd6fc9284d261bdd0e21cdd2f4. Summary will update on new commits.</sup>
<a href="https://cubic.dev/pr/pydantic/monty/pull/1389?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->