Skip to content

fix(runtime): copy funs stored in ETS and persistent_term with their captured values - #30

Merged
mremond merged 1 commit into
mainfrom
fm/task-b1d5802d
Nov 13, 2026
Merged

mremond merged 1 commit into
mainfrom
fm/task-b1d5802d

Conversation

@mremond

@mremond mremond commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Why

A fun stored in an ETS table or with persistent_term kept pointing into the heap of the process that stored it, so calling it after that process exited or collected garbage read freed memory.

What changed

  • A fun stored in ETS or persistent_term carries its own copy of its captured values, as on BEAM, wherever it sits in the row.
  • A fun read back with ets:tab2list/1 or persistent_term:get/1 is a copy in the reading process, so it keeps working after the table or term is deleted.
  • A new fixture checks these cases natively and on BEAM.

Before and after

Before: The new fixture failed in 5 of 5 native runs with badfun on a freed closure; a fun stored by a process that collected garbage, and one put in persistent_term by a process that exited, also raised badfun in 3 of 3 runs each.

After: The fixture passes in 10 of 10 native runs and 10 of 10 runs on OTP 29.

Only the runtime's storage copy changed; nothing on screen changes.

Checked

  • A build that copies funs into storage but not back out: The fixture fails in 3 of 3 runs, at the fun read by tab2list/1 after the table is deleted, so both directions of the copy are needed.
  • Local gate: clippy, workspace tests, allocator check, end-to-end suite: Clippy clean, 2,145 Rust tests passed and the allocator check passed; at this version 483 of 485 fixtures passed, with 2 skipped as on main.
  • Checks on Linux and macOS: Rust checks, all end-to-end shards, cross-compiles and the Cowlib, Thoas, Poolboy and macOS integration jobs passed.
How it was checked

cargo clippy --workspace --all-targets --locked -- -D warnings

cargo test --workspace --locked --no-fail-fast

python3 scripts/test-allocator-config.py

nera test -j 1 --timeout 30

nera test ets_stored_fun, ten times natively

the same fixture ten times on OTP 29

Risk

Risk above low: Blast radius, medium — Every ETS and persistent_term write and copying read goes through the changed copier.

Full risk assessment
Dimension Level Why
Blast radius medium Every ETS and persistent_term write and copying read goes through the changed copier.
Behavior low Corrects a read of freed memory; other stored terms copy as before.
Undo low One commit; reverting it restores the previous copy.
Evidence low Failing and passing fixture runs, a mutation check, the full local gate and CI.
Sensitivity low No sign-ins, keys, saved settings or releases involved.
Novelty low Adds one case to the existing copier, shaped like its tuple case.
Dependencies low No dependency changes.
Not done

Bitstrings that are not a whole number of bytes still keep pointing into the storing heap when stored in ETS; that is tracked separately.

…captured values

The ETS copy, which persistent_term also uses, copied lists, tuples, maps,
binaries, floats, bignums and references but kept any other boxed term as
it was. A fun stored in a table or persistent term therefore pointed into
the heap of the process that stored it, and once that process exited or
collected garbage, calling the fun read freed memory: a wrong result,
badfun or a crash.

Count and copy closures like tuples: the header, code pointer and arity
are copied as they are, and the captured values are queued and copied.
The same copier reads stored terms back with ets:tab2list/1 and
persistent_term:get/1, so a fun read that way is now a copy in the
reading process and keeps working after the table or term is deleted.

The ets_stored_fun fixture checks funs at the top of a row, nested in a
list, tuple and map, after update_element/3, read by tab2list/1 after the
table is deleted, stored by a process that collects garbage, and through
persistent_term, natively and on BEAM.
@github-actions github-actions Bot added this to the 0.1.1 milestone Nov 13, 2026
@mremond
mremond merged commit 40c912b into main Nov 13, 2026
22 checks passed
@mremond
mremond deleted the fm/task-b1d5802d branch November 14, 2026 11:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant