Part III — Advanced Systems · Chapter 14 · Lesson 2 of 5 · 4 min read
Lesson 2: Time, Instruction, Memory, Recursion, Stack, Collection, Output, HTTP, and File-Read Limits
Lesson level: Advanced
Purpose: Learn each resource limit the runtime can enforce, what failure looks like when it is reached, and how to choose values that stop runaway scripts without breaking legitimate work.
Release note: limit names, units, and defaults in this lesson are illustrative. Verify them against your installed v0.63 release.
Learning objectives
By the end of this lesson, you should be able to:
- Name each resource limit and the risk it controls.
- Configure limits for a host execution.
- Recognize the error raised when each limit is exceeded.
- Choose limit values from measurements.
- Combine limits with capabilities for defence in depth.
2.1 Why capabilities are not enough
A script with no capabilities can still run an infinite loop, build a list with a billion items, or recurse until the host crashes. Resource limits bound what any script can consume, even when it does nothing forbidden.
2.2 The limits
| Limit | Controls | Typical failure without it |
|---|---|---|
| Time | Wall-clock duration of an execution | A request hangs the service |
| Instructions | Number of VM or interpreter steps | A tight infinite loop burns CPU |
| Memory | Total memory the script can allocate | Host process is killed |
| Recursion depth | Nested function calls | Host stack overflow |
| Stack size | Operand-stack depth | Crash on deeply nested expressions |
| Collection size | Items in any single list or map | Memory exhaustion from one structure |
| Output | Characters or bytes printed or logged | Disk or log flooding |
| HTTP | Requests, response size, per-call timeout | Abuse of a granted endpoint |
| File read | Bytes and number of files read | Reading huge files into memory |
2.3 Configuring limits
# host limits (illustrative)
[limits]
time_seconds = 2.0
instructions = 5_000_000
memory_mb = 64
recursion_depth = 200
stack_depth = 1024
collection_items = 100_000
output_bytes = 65_536
http_requests = 10
http_response_bytes = 1_048_576
file_read_bytes = 10_485_760
2.4 What happens at a limit
The runtime raises a limit error that names the limit, its configured value, and the observed value. Execution stops cleanly: frames unwind, finally blocks run within a small grace allowance, and the host receives the error with the resource snapshot covered in Lesson 3.
2.5 Instructions versus time
Instruction limits are deterministic: the same script and input always stop at the same point, on any machine. Time limits catch waiting that instructions cannot see, such as slow host calls. Use both: instructions for CPU work, time for overall responsiveness.
2.6 Choosing values from measurements
- Run the script on realistic and worst-case legitimate inputs.
- Record peak instructions, memory, recursion, collection size, output, and time (Chapter 12 Lesson 6 profiling).
- Set each limit at a safety margin above the observed peak, commonly two to five times.
- Add tests that confirm legitimate worst cases pass and hostile inputs stop.
- Review limits when the script or its inputs change.
2.7 Per-execution and per-call limits
Some limits apply to the whole execution (time, instructions, memory). Others apply per operation (HTTP response size, per-call timeout, single file size). Set both: a script making ten small HTTP calls is different from one making a single enormous call.
2.8 Limits and async work
Concurrent tasks share the execution's budgets. Ten tasks each running 1 million instructions consume 10 million from the shared instruction limit. The task limit from Chapter 13 controls how many run; resource limits control how much they consume together.
2.9 Limits and the two engines
Instruction counts differ between the interpreter and the VM because they execute different internal steps. Calibrate instruction limits for the engine you deploy, and keep differential tests (Chapter 10) focused on results rather than exact counts.
2.10 Worked example
Profiling the order_rules single-order function shows a peak of about 4,000 instructions, recursion depth 6, and under 1 MB of memory on the largest legitimate order. The team sets 50,000 instructions, depth 50, and 16 MB per order execution, and adds tests with a deliberately deep recursion and a 10-million-item list that confirm the limits stop them with clear errors.
2.11 Common mistakes
| Mistake | Consequence | Fix |
|---|---|---|
| No limits in development | Limits surprise you in production | Run tests with production limits |
| Limits set by guesswork | Legitimate work fails or abuse passes | Measure, then add margin |
| Only a time limit | Deterministic loops vary by machine load | Add an instruction limit |
| Catching limit errors in scripts | Runaway code continues | Let them reach the host |
2.12 Guided practice
- Write a script that exceeds the recursion limit and read the error.
- Measure instructions for a function and propose a limit.
- Explain why an HTTP response-size limit is needed even with a granted host.
- Predict which limit stops a loop that appends to a list forever.
2.13 Hands-on lab
Create a limits profile for the order_rules service from measurements. Then write one hostile test per limit and confirm each is stopped by the intended limit and not by another.
Deliverable: the measurement table, the chosen limits with margins, and output for every hostile test.
2.14 Review questions
- Why are limits needed even for scripts with no capabilities?
- What is the difference between instruction and time limits?
- Why do concurrent tasks share budgets?
- How should limit values be chosen?
- Why should instruction limits be calibrated per engine?
2.15 Applied challenge
Design limit profiles for three workloads: a 50-millisecond pricing call, a 10-minute nightly batch, and an untrusted user-submitted rule. Explain how and why the values differ.
Lesson summary
- Limits bound consumption; capabilities bound authority.
- Each limit targets a specific failure mode.
- Choose values from measurements with a safety margin.
- Concurrent tasks share budgets; calibrate per engine.
Transition to Lesson 3
When a limit stops a script, operators need to know what happened. Lesson 3 covers resource budgets, snapshots, and auditability.
Lesson status: Ready for review.