Let hosts install a custom memory probe - #740
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
The tracker reads memory from the process-wide `LIVE_MEMORY`/`BASELINE_MEMORY` pair, which fits one interpreter per `monty-alloc` worker. A host that embeds the interpreter in-process and runs several at once cannot keep that accounting straight: arming the shared baseline for one run forgives another run's usage, and the limit silently stops enforcing. `set_memory_probe` points the read at the host's own accounting instead, say a thread-local live count fed by its global allocator, installed once like a global allocator. Enforcement is unchanged: the tracker still checks `max_memory` at checkpoints, and workers never call the new API so they keep the default read.
38f9d7a to
5aa07a1
Compare
Merging this PR will not alter performance
Comparing Footnotes
|
|
Sorry, I don't see how this works. Please can you (a human) explain how it works and how it would be used. |
|
I'm running monty in-process, and since the tracker started reading So I want to run a separate thread per monty interpreter via a simple counting Honestly this is not the best solution, but this is the fastest solution to my problem. And the problem is real, wanting to run monty not as a separate process makes sense. Maybe there are much better solutions, would love to hear from you too. |
|
Why do you want/need to run monty in the same progress? |
I use monty as the backend for my code_execution tool in my agent harness, and the agent can run it inside batch / inside subagents, starting to manage a bunch of subprocesses when unnecessary is annoying and more complex. I can manage though, if you say you don't want to ever support the use case of having multiple monty interpreters shared in a process. Just wanted to shoot a shot at fixing the issue. |
The tracker reads memory from the process-wide
LIVE_MEMORY/BASELINE_MEMORYpair, which fits one interpreter permonty-allocworker.A host that embeds the interpreter in-process and runs several at once cannot keep that accounting straight: arming the shared baseline for one run forgives another run's usage, and the limit silently stops enforcing.
set_memory_probepoints the read at the host's own accounting instead, say a thread-local live count fed by its global allocator, installed once like a global allocator.Enforcement is unchanged: the tracker still checks
max_memoryat checkpoints, and workers never call the new API so they keep the default read.Summary by cubic
Allows hosts to install a custom memory probe to replace the default
LIVE_MEMORY - BASELINE_MEMORYread, so concurrent in-process interpreters enforcemax_memoryindependently. Previously, shared counters rebased each other and could silently disable enforcement under concurrent load; default behavior remains unchanged unless a host installs a probe.Review and rollout
set_memory_probeinmonty-types; installs once viaOnceLock, a second install returns "memory probe already installed".probe_memory()delegates to the installed probe or falls back toLIVE_MEMORY - BASELINE_MEMORY.ResourceLimits::max_memoryandResourceTracker::newnow state enforcement requiresmonty-allocor a probe.monty-alloc.Written for commit 38f9d7a. Summary will update on new commits.