Skip to content

R08. When the interpreter stops - #189

Merged
tamnd merged 1 commit into
mainfrom
r08-when-the-interpreter-stops
Sep 6, 2026
Merged

R08. When the interpreter stops#189
tamnd merged 1 commit into
mainfrom
r08-when-the-interpreter-stops

Conversation

@tamnd

@tamnd tamnd commented Sep 6, 2026

Copy link
Copy Markdown
Owner

R08 is the last lesson about the runtime as a thing with a lifetime. R01 asked what has already happened before your first line, and this one asks what is still to come after your last one.

Every cell starts a child interpreter, because a notebook cannot watch its own shutdown. The cells walk the order of the end with sys.is_finalizing() printed next to each step, show atexit callbacks coming back newest first and one registered during shutdown that never runs at all, ask a late __del__ what it can still reach, let an atexit callback and a finalizer both raise so you can read CPython's own ignored exception wording and watch the exit status stay at zero, catch the single generation two collection with gc.callbacks, and leave a subinterpreter open so its warning and its own atexit callback both turn up.

The case that earns the lesson is the daemon thread. Pass time.sleep and your finalizers run. Pass a function defined in your own module and its stack frame holds your module's globals, so the module dict is never cleared and nothing in it is freed. The two Tier 1 recordings run nine endings on a release build and on a debug build, and the debug one puts a number on it: 12680 references still alive at the end, against zero for the same five thousand objects held any other way.

Twelve citations, all resolving against the pinned source. Six diagrams. Three new glossary terms in the startup and shutdown group: interpreter finalisation, atexit callback, and finalizing flag.

One drive-by fix in R07, which said four of seven sample extensions were refused where the table has eight rows.

Checked with just check. Under Pyodide all nine code cells run and each one says it cannot start a process, which is the honest answer in a browser tab.

The last lesson about the runtime as a thing with a lifetime. R01 asked what
has already happened before your first line. This one asks what is still to
come after your last one.

Every cell starts a child interpreter, because a notebook cannot watch its own
shutdown. The cells cover the order of the end with sys.is_finalizing() next
to each step, atexit coming back newest first and a callback registered during
shutdown that never runs, what a late __del__ can still reach, an atexit
callback and a finalizer that both raise while the exit status stays zero, the
single generation two collection, and a subinterpreter nobody closed.

The case worth the lesson is the daemon thread. Pass time.sleep and your
finalizers run. Pass a function defined in your own module and its stack frame
holds your module globals, the module dict is never cleared, and nothing in it
is freed. Two Tier 1 recordings run nine endings on a release build and on a
debug build, where that one thread leaves 12680 references alive against zero
for the same objects held any other way.

Six diagrams, three glossary terms, and a stale count fixed in R07, which said
seven sample extensions where the table has eight.
@tamnd tamnd added this to the M8 Concurrency and runtime milestone Sep 6, 2026
@tamnd tamnd added kind/lesson A chapter: prose, notebook, experiments, boss fight area/runtime Startup, shutdown, import, extension modules and the C API labels Sep 6, 2026
@tamnd
tamnd merged commit aa03a5c into main Sep 6, 2026
16 checks passed
@tamnd
tamnd deleted the r08-when-the-interpreter-stops branch September 6, 2026 12:32
@tamnd tamnd mentioned this pull request Sep 6, 2026
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/runtime Startup, shutdown, import, extension modules and the C API kind/lesson A chapter: prose, notebook, experiments, boss fight

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant