Skip to main content

Overview

The ResourceLimits interface allows you to enforce limits on Python code execution to prevent resource exhaustion and ensure safe sandboxing of untrusted code. All limits are optional. Omit a field to disable that specific limit.

Interface

Fields

number
Maximum number of heap allocations allowed during execution.When this limit is reached, execution terminates with a ResourceError.Useful for preventing memory exhaustion from code that creates many objects.Example:
number
Maximum execution time in seconds (floating point).When this time limit is exceeded, execution terminates with a ResourceError.Useful for preventing infinite loops or long-running computations.Example:
number
Maximum heap memory in bytes.When heap memory usage exceeds this limit, execution terminates with a ResourceError.Useful for preventing memory exhaustion from large data structures.Example:
number
Run garbage collection every N allocations.Controls how frequently the garbage collector runs. Lower values reduce peak memory usage but may slow execution. Higher values improve performance but may increase memory usage.If not specified, garbage collection is triggered automatically based on heap pressure.Example:
number
Maximum function call stack depth.Default: 1000When the call stack exceeds this depth, execution terminates with a RecursionError.Useful for preventing stack overflow from infinite recursion.Example:

Usage Examples

Basic Resource Limiting

Preventing Infinite Recursion

Preventing Long-Running Code

Combining Multiple Limits

Using with runMontyAsync()

Best Practices

Always set resource limits when executing untrusted code to prevent denial-of-service attacks and resource exhaustion.
When a resource limit is exceeded, execution terminates immediately. The heap may contain orphaned objects with incorrect reference counts. Always discard the Monty instance after a resource limit error.

See Also