#921·monty

functools.partial keyword index can terminate a memory-limited worker

Author: Vasanthadithya-mundrathiCreated Sep 23, 2026Updated Sep 23, 2026

Version and observed scope

I reproduced this locally with pydantic-monty==1.0.0b2 (v1.0.0-beta.2). The relevant partial.rs code is unchanged in current main (f221900306a20bcdd6555cd6c1cc8c276b5e2f75). This report concerns one isolated worker; I did not observe a parent-process crash, WebSocket-server effect, or sandbox escape.

Reproduction

Run this as repro.py with uv run --no-project --prerelease=allow --with 'pydantic-monty==1.0.0b2' python repro.py:

python
from pydantic_monty import Monty

setup = '''import functools
def f(**kw): return 1
d={}
for i in range(9):
    d['x'*1000000+str(i)] = i
p=functools.partial(f, **d)
len(d)'''

with Monty() as pool:
    for mib in (12, 32):
        with pool.checkout(limits={'max_memory': mib*1024*1024, 'max_feed_duration_secs': 3}) as session:
            print(f'{mib}MiB setup:', session.feed_run(setup), flush=True)
            try:
                print(f'{mib}MiB call:', session.feed_run('p(z=0)'), flush=True)
            except Exception as exc:
                print(f'{mib}MiB error:', type(exc).__name__, str(exc), flush=True)

Observed locally (the allocator message is on worker stderr):

12MiB setup: 9
monty worker: allocation of 1000001 bytes exceeds the memory limit
12MiB error: MontyRuntimeError MemoryError: the worker exceeded its memory limit and was terminated
32MiB setup: 9
32MiB call: 1

Each bound keyword name has 1,000,001 characters. Constructing the partial succeeds within the 12 MiB limit; invoking it with one new keyword is the step that terminates the worker. The 32 MiB control confirms that the same call otherwise returns normally.

Suspected code path

merge_keywords switches to a name index when there are more than eight bound keywords. On p(z=0), keyword_index_map copies each bound keyword name into an owned AHashMap<String, usize> using to_owned(). I do not see a resource preflight for those copies before allocation; the observed attempted 1,000,001-byte allocation is consistent with that path. This explanation is based on source review; the worker stderr proves the allocation-limit termination, not a Rust stack trace.

Expected behavior / impact

I would expect this allocation path to raise a normal memory-limit error before a hard allocator abort, as other preflighted bulk-copy paths do. The directly demonstrated impact is loss of this one worker/session under a configured memory limit. I did not show a challenge-server crash, cross-session impact, or information disclosure.

This is a separate trigger from #700, which concerned list growth rather than functools.partial keyword-index copies. A regression test could exercise nine large bound keyword names and a low max_memory ceiling, then assert the intended limit-error behavior without terminating the worker.