Preflight functools.partial keyword index allocations #950
<!-- cubic:review-summary:start -->
1 issue found across 4 files
<!-- cubic:review-summary:end -->
<details>
<summary>Prompt for AI agents (unresolved issues)</summary>
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="crates/monty/src/types/partial.rs">
<violation number="1" location="crates/monty/src/types/partial.rs:235">
P3: `merge_keywords` was rewritten from a `for` into this `while` loop, and its own doc comment states the keyword count is caller-controlled (`partial(f, **thousands)`) with no execution checkpoint inside. The checks this PR adds are memory-only (`check_allocation` never touches the time budgets), so a large merge can run long past `max_feed_duration`/`max_turn_duration` without any enforcement. The common path is bounded by the Python work that built the keyword list (which checkpointed), but a partial restored from a dump or built from host-supplied data has no such bound. Add an amortized `tracker.check_time_every(i)` guard (every 64th iteration) while this loop is being touched; the pool hard deadline is only a backstop when a `request_timeout` is actually configured.</violation>
</file>
</details>
<sub>Reply with feedback, questions, or to request a fix.<br /><br />Fix all with cubic | Re-trigger cubic</sub>
<!-- cubic:review-post:ai_pr_review_github_delivery_4e09f2ee40495aaecced624625ddef9c5582de675fc4ab2bb00c83e2546525a8:08b074f14d6e3d26ea23c5ce130b3f7693ca0830:badc18c8-9da5-5df3-ab2c-85a881b3cd48 -->
<!-- cubic:v=b19213c1-8edc-4d57-833b-e5ced655da3b -->
<!-- cubic:review-run=badc18c8-9da5-5df3-ab2c-85a881b3cd48 -->
<!-- metadata:{"confidence":5} -->
P3: merge_keywords was rewritten from a for into this while loop, and its own doc comment states the keyword count is caller-controlled (partial(f, **thousands)) with no execution checkpoint inside. The checks this PR adds are memory-only (check_allocation never touches the time budgets), so a large merge can run long past max_feed_duration/max_turn_duration without any enforcement. The common path is bounded by the Python work that built the keyword list (which checkpointed), but a partial restored from a dump or built from host-supplied data has no such bound. Add an amortized tracker.check_time_every(i) guard (every 64th iteration) while this loop is being touched; the pool hard deadline is only a backstop when a request_timeout is actually configured.
<details>
<summary>Prompt for AI agents</summary>
Check if this issue is valid — if so, understand the root cause and fix it. At crates/monty/src/types/partial.rs, line 235:
<comment>`merge_keywords` was rewritten from a `for` into this `while` loop, and its own doc comment states the keyword count is caller-controlled (`partial(f, **thousands)`) with no execution checkpoint inside. The checks this PR adds are memory-only (`check_allocation` never touches the time budgets), so a large merge can run long past `max_feed_duration`/`max_turn_duration` without any enforcement. The common path is bounded by the Python work that built the keyword list (which checkpointed), but a partial restored from a dump or built from host-supplied data has no such bound. Add an amortized `tracker.check_time_every(i)` guard (every 64th iteration) while this loop is being touched; the pool hard deadline is only a backstop when a `request_timeout` is actually configured.</comment>
<file context>
@@ -220,13 +224,23 @@ const KEYWORD_INDEX_THRESHOLD: usize = 8;
// and grows past the threshold during this very loop.
let mut index: Option<AHashMap<String, usize>> = None;
- for (name, value) in keywords {
+ while let Some((name, value)) = keywords.next() {
if index.is_none() && bound.len() > KEYWORD_INDEX_THRESHOLD {
- index = Some(keyword_index_map(bound, vm));
</file context>
</details>
<a href="https://www.cubic.dev/action/fix/violation/b19213c1-8edc-4d57-833b-e5ced655da3b" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://cubic.dev/buttons/fix-with-cubic-dark.svg">
<source media="(prefers-color-scheme: light)" srcset="https://cubic.dev/buttons/fix-with-cubic-light.svg">
<img alt="Fix with cubic" src="https://cubic.dev/buttons/fix-with-cubic-dark.svg">
</picture>
</a>
Codecov Report
:x: Patch coverage is
17.18750% with 53 lines in your changes missing coverage. Please review.| Files with missing lines | Patch % | Lines |
|---|---|---|
| crates/monty/src/types/partial.rs | 17.18% | 50 Missing and 3 partials :warning: |
:loudspeaker: Thoughts on this report? Let us know!
<!-- __CODSPEED_PERFORMANCE_REPORT_COMMENT__ -->
Merging this PR will not alter performance
✅ 45 untouched benchmarks ⏩ 16 skipped benchmarks[^skipped]
---
<sub>Comparing <code>Pybsama:codex/partial-keyword-memory</code> (08b074f) with <code>main</code> (15753b3)</sub>
<a href="https://app.codspeed.io/pydantic/monty/branches/Pybsama%3Acodex%2Fpartial-keyword-memory?utm_source=github&utm_medium=comment-v2&utm_content=button">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://codspeed.io/pr-report/open-in-codspeed-dark.svg">
<source media="(prefers-color-scheme: light)" srcset="https://codspeed.io/pr-report/open-in-codspeed-light.svg">
<img alt="Open in CodSpeed" src="https://codspeed.io/pr-report/open-in-codspeed-light.svg" width="169" height="32">
</picture>
</a>
[^skipped]: 16 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.
Thanks, I think this is the wrong fix.
Rather than increasing complexity in partial.rs we should seek to throw away code there. I think KwargsValues::Dict already contains a mapping form and we should aim to merge keywords into that form instead.
davidhewitt
Pybsama
Fixes #921.
functools.partialbuilds a keyword-name index once it has more than eight bound keywords. Previously, building that index copied every name without checking the configured memory budget. Nine large names under a 12 MiB limit could terminate the worker instead of returning a sandboxMemoryError.Preflight the index's name copies and table allocation before building it, including spare buckets, rounding and control bytes, and check new-name/table growth while merging. If the preflight rejects a merge, release the arguments that have not been transferred yet. This keeps the existing indexed merge behavior when memory is available. Document the preflight in the resource-limit guide and limitations page.
Validation: the new subprocess regression fails on the original code and passes with the fix, including a 32 MiB successful control and a
memory-model-checksworker build. All 67 pool integration tests and four Monty/CPythonfunctools.partialdata tests pass. Nightly formatting, Markdown formatting and targeted Clippy checks pass. The repository-widemake lint-rsis blocked on an unchangedstable_heap.rswarning from local Clippy 1.96; the targeted Clippy runs excluded that and other unchanged baseline warnings.AI assistance disclosure: OpenAI Codex helped prepare the implementation, regression test and this description.
<!-- This is an auto-generated description by cubic. -->
---
Summary by cubic
Preflights
functools.partial's keyword-name index allocations so an oversized index raises a sandboxMemoryErrorinstead of terminating the worker at the hard memory limit.<sup>Written for commit 08b074f14d6e3d26ea23c5ce130b3f7693ca0830. Summary will update on new commits.</sup>
<a href="https://cubic.dev/pr/pydantic/monty/pull/950?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="Review in cubic" src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->