ADR 0026: The ownership discipline for Str¶
Status: Final Type: Architecture Created: 2026-07-30 Amended-By: 0007 (a handled exception record is released, and its message with it) Related-ADRs: 0024 (the slot rule this extends to a second counted type; the general rules of slots, provenance and the instrument live there), 0025 (the representation this discipline manages), 0007 (the propagate path that produces placeholders)
Context¶
0025 gave every Str a header with a reference count that nothing yet read; this ADR is where the count starts governing. The rule is 0024's slot rule with the word "instance" removed — one slot machinery, one prologue list, one epilogue loop for all counted types — and everything below is what Str needed beyond that rule.
Decision¶
Where Str differs from an instance¶
| a class instance | a Str |
|
|---|---|---|
| at a task boundary | refused (sendability rule) | copied |
| a static value of the type | none | a literal, immortal, never freed |
| the null a slot holds | NULL |
the immortal empty string |
| producers | the allocator and POST functions | a dozen runtime helpers, some handing back somebody else's reference |
The null differs because a Str placeholder is readable. An instance placeholder meets only incref/decref (no-ops on NULL); a Str placeholder can reach print, len, + and ==, all of which read the header, so the placeholder is the immortal empty string. An immortal literal (rc == -1) gets no slot at all: count operations on it are no-ops and a release could never do anything.
The hook is the emitted call site¶
An instance's new references are parked per AST node, but a Str's cannot be. An f-string folds to a chain of concat calls, and only the last result belongs to an expression the AST-level hook wraps. An AST-keyed rule would own one string per f-string and leak the rest. So owning producers are parked at the moment their call is emitted, one slot per static call site. A producer inside a loop reuses the slot and releases the previous iteration's string on the next; a producer inside a lowering is covered without the lowering knowing. A borrowed Str gets no slot, since adopting a borrowed reference is a use-after-free.
Copy-on-send¶
Every Str crossing a task boundary is copied into the receiving side, so counts stay non-atomic for all types. The design rule "heaps are never shared" makes all counts non-atomic. The copy keeps that rule true. Atomic counts for one type would abandon the rule. Four crossings, one policy:
- send copies before the send and frees the copy if the send does not happen (a closed channel raises; it must not also leak its payload);
- receive needs no code: the channel holds the send's
+1and the receiving frame becomes its only owner; - select parks a copy per
Strsend case before the wait; the runtime frees the uncommitted ones (a per-slot flag says which slots areStr), and reading a committed receive takes the string and empties the slot so a second read cannot double-free; - a spawn argument or closure capture copies into the task's argument box; the trampoline releases after the body returns, and a spawn the runtime declines frees the copies with the box.
The escape valve is a shared refcounted buffer for large flat data, the one place an atomic would be admitted. It is designed but not built; its trigger is a measurement showing the copy dominating a real program.
The exception record¶
raise ValueError("row " + s) builds its message in a frame slot. The propagate block is a returning block, so the frame releases that slot on the way out. Then the handler reads str(e). The record therefore retains name and msg at allocation (it does not adopt: the frontend's callers are slots that release themselves; the few C callers that hand over a fresh reference release it immediately after). The frame is correct and the record is correct; the rule lives in the sentence between them. The record is counted (0007), so a handled record releases its message with it, and a loop of caught raises leaves no Str behind. A record that reaches the top of the program keeps its message until the process ends.
The stable C ABI¶
A Str crossing to C is still a NUL-terminated const char * at the first payload byte; the emitted header is unchanged and Str still exports where bytes does not. The wrapper adopts a Str parameter on entry (a prefix header cannot borrow a caller's buffer) and releases it after the call. The adoption is hoisted to a statement, because an adoption nested in an argument list has nowhere for its release to go. A Str a wrapper returns is a +1 handed to a caller that has no destructor and no release entry point in the stable ABI. It is therefore retained for the life of the process, one per call. This documented residue is filed upstream as the question of who owns a string crossing the package ABI. The CPython callback direction has no residue: the returned Str is converted to a PyObject (a copy) and the POST reference released immediately.
Consequences¶
- One rule owns two types (now six). What
Stradded (the readable null, the emit-site hook, copy-on-send, the record's retain) takes one sentence apiece. - Memory that grew with the input becomes memory bounded by the source: one string per static producing site stays live until frame exit.
- The residues are enumerated and each is a decision: the C-ABI return, the message of a record that reaches the top of the program, and copy-on-send's price on small payloads.
Alternatives considered¶
- A second, acquire-side lifetime invariant in the verifier: rejected. The release-only asymmetry is the invariant ("a reference is only released through a slot the entry block owns"); the things an acquire may legitimately name (a slot, a value about to be returned, a borrowed field read, a literal) form a list, and
Stris the type that proved it. - Pointer-move across channels (the receiver retains the sender's string): rejected as the default; it abandons the never-shared-heap rule that makes every count non-atomic. It remains possible only as a future compiler optimisation where uniqueness is provable.
- Adopting at the record: rejected, because the frontend's two dozen raise sites all hand over slot-owned references that the frames release themselves; adoption would double-free each one.