Skip to main content

init_kpathsea

Function init_kpathsea 

Source
pub fn init_kpathsea()
Expand description

Construct the process-wide kpathsea handle NOW, on the calling thread, before any other thread exists that might read the environment.

Kpaths::new runs libkpathsea’s kpathsea_set_program_name, which putenvs SELFAUTOLOC/SELFAUTODIR/SELFAUTOPARENT/ SELFAUTOGRANDPARENT (progname.c). glibc’s putenv of a new name reallocates the environ array and frees the old one; a concurrent getenv on another thread — std::env::var, chrono’s TZ read inside localtime_r, the logger’s NO_COLOR probe — walks the freed array. That is undefined behaviour with no lock to take: glibc’s getenv is lock-free by contract, so the only sound ordering is “all putenv before any second thread”. The CLI used to run this construction on the prewarm thread while the worker thread parsed options: sweep #35 (release build, 2374 docs) caught one general-protection fault in getenv+0x56 (traps: latexml_oxide.s[1711120] general protection fault … in libc.so.6, pstool/pstool at 1.1 s, unreproducible in 9 reruns — a race, not a document).

Cost: ~0.1 ms for the construction plus the two memoized kpsewhich probes (--version, -var-value=SELFAUTOPARENT) that pick the backend. The expensive part of kpathsea start-up — kpathsea_init_db on the first lookup — is read-only with respect to the environment and stays on the prewarm_kpathsea background thread.