PeaBox Lab › PeaLang › Learn › Chapter 14 › Lesson 2

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:

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

LimitControlsTypical failure without it
TimeWall-clock duration of an executionA request hangs the service
InstructionsNumber of VM or interpreter stepsA tight infinite loop burns CPU
MemoryTotal memory the script can allocateHost process is killed
Recursion depthNested function callsHost stack overflow
Stack sizeOperand-stack depthCrash on deeply nested expressions
Collection sizeItems in any single list or mapMemory exhaustion from one structure
OutputCharacters or bytes printed or loggedDisk or log flooding
HTTPRequests, response size, per-call timeoutAbuse of a granted endpoint
File readBytes and number of files readReading 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

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

MistakeConsequenceFix
No limits in developmentLimits surprise you in productionRun tests with production limits
Limits set by guessworkLegitimate work fails or abuse passesMeasure, then add margin
Only a time limitDeterministic loops vary by machine loadAdd an instruction limit
Catching limit errors in scriptsRunaway code continuesLet them reach the host

2.12 Guided practice

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

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

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.

← Previous Next →