ResourceLimits type is a TypedDict that configures resource constraints for Monty code execution. All limits are optional - omit a key to disable that limit.
Type Definition
Fields
int
Maximum number of heap allocations allowed. Execution will be terminated if this limit is exceeded.
float
Maximum execution time in seconds. Execution will be terminated if it exceeds this duration.
int
Maximum heap memory in bytes. Execution will be terminated if memory usage exceeds this limit.
int
Run garbage collection every N allocations. This helps control memory usage by periodically cleaning up unused objects.
int
Maximum function call stack depth. Default is 1000. This prevents infinite recursion from exhausting system resources.
Usage Examples
Basic Time Limit
Multiple Limits
Preventing Infinite Loops
Memory-Intensive Operations
Recursion Depth Control
Error Handling
When a resource limit is exceeded, aMontyRuntimeError is raised:
Best Practices
- Always set time limits when executing untrusted code to prevent infinite loops
- Use allocation limits to prevent memory exhaustion attacks
- Configure GC intervals for long-running or memory-intensive code
- Set recursion depth lower than default (1000) when running untrusted code
- Combine multiple limits for defense in depth
- Test limits with representative workloads to find appropriate values
Related
- Monty class - Main class that accepts ResourceLimits
- Errors - Error types raised when limits are exceeded
