Persistent Perl interpreter accumulates mixed old and new code after repeated do FILE
0 reputation · 04 Jan 2021, 02:26 UTC
0 reputation · 04 Jan 2021, 02:26 UTC
The goal is to update code in a small, long-running Perl application without dropping the listening socket or interrupting in-flight requests.
Perl's require records a path in %INC and skips re-execution on later calls, so it cannot hot-swap changed definitions in the same interpreter. do FILE re-executes code but does not maintain %INC the same way, and repeated execution can leave duplicate package definitions, stale symbol-table entries, or a mixture of old and new subroutines. XS modules, fork behavior, thread support, and signal handling vary by Perl build, so the safe cutover path depends on the target interpreter.
Should the migration use in-process reload with strict single-loader ownership and drained workers, or start new Perl processes and transfer the listening socket? Which target Perl build characteristics determine whether socket handoff is viable? What checks detect mixed old and new symbol-table state before retiring old workers?
35775 reputation · 04 Jan 2021, 07:05 UTC
Prefer a socket-owning supervisor that starts fresh Perl processes and retires old workers over repeated do FILE in one interpreter. Use in-process reload only when the application is pure Perl, has a single reload coordinator, and you can prove the symbol table is clean; otherwise stale XS state and duplicate subroutines make mixed code likely.
do FILE accumulates mixed coderequire records the path in %INC and skips later loads, so it cannot hot-swap definitions. do FILE re-executes the file, but it does not clear the package stash or update %INC consistently. Repeated execution can redefine subroutines while older symbol-table entries remain, especially when package declarations or lexical scopes change between versions. XS modules add C-level state that do cannot reset.
useithreads: threaded builds share file-descriptor tables and complicate passing sockets between threads.fork() lets descriptors survive; Windows uses pseudo-fork via threads, making true socket handoff harder.fork()/exec() or survive in-process reload unsafely.useshrplib and signal handling: shared libraries and installed signal handlers can persist after reload.Check the build with perl -V and look for useithreads, d_fork, and useshrplib. These steps assume Perl 5 on a Unix-like operating system unless otherwise noted.
perl -V to confirm whether the build is threaded and whether fork() is available.delete $INC{'Module.pm'}; undef %Module::;, reset %SIG, and drain requests.&Package::sub before and after reload. A changed reference means the subroutine was replaced; an unchanged reference may indicate stale code.Devel::Symdump->new('Package')->functions to enumerate subroutines and look for duplicates or orphaned entries.Devel::MAT after several reload cycles to detect accumulated stashes.Does the application load any XS modules? If yes, rule out in-process reload regardless of Perl build.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.