Skip to main content

reset_thread_engine

Function reset_thread_engine 

Source
pub fn reset_thread_engine() -> bool
Expand description

Free this thread’s accumulated engine state — the three State singletons (STATE, STD_STATE, STY_STATE), the string-interner arena, the model, gullet, stomach and local assignments, and the per-conversion caches — and rebuild them empty, as a fresh thread’s init_thread_engine leaves them.

Why this exists. The engine’s roots are leaked per-thread statics (thread_root!/thread_static!, aux_macros.rs): they run no destructor at thread exit, so a thread that converts and exits leaks its engine (~110 MB for a typical document) unless it resets first. The test harness (a fresh libtest thread per test), the render and dump workers reset before their thread exits; the library API (latexml::api) resets after each call and keeps the thread for the next one, so a long-lived host holds one rebuilt engine (~36 MB) per parked worker, not one per call, and a worker about to exit runs shrink_thread_engine instead. The persistent cortex_worker never resets: it keeps the dump definitions across papers.

Returns whether the thread may convert again. The per-thread token and gullet symbol constants (TOKEN_BEGIN, …, token.rs; DEFERRED_COMMANDS, COLUMN_ENDS) are built once and hold arena symbols; they are pinned again in creation order into the new arena, and true means every one got its old symbol back — always so on a thread that ran init_thread_engine before anything else touched the arena. false: the thread must not convert again (a stale constant would alias another string, the phantom-symbol bug class of the REPORT-map fix, 7b64a48ad1).

Soundness. Resetting the interner invalidates every live SymStr on the thread, so call this only between fully independent conversions, once the previous conversion’s output is owned data. Other symbol-holding thread-locals are reset here (reset_conversion_state, the arena-keyed reports and memos, pin! site caches) or at a conversion’s start, or are scoped to a call (stacks popped by guards). Not reclaimed: kpathsea’s ls-R/cnf hash tables, 8.6 MB of C heap allocated once per process.