Repository navigation
fix(emulator): don't orphan the Python functions runtime - #11235
OmarMujahid wants to merge 3 commits into
Conversation
startPython spawns functions-framework through runWithVirtualEnv's shell wrapper, and every worker shutdown path called runtime.process.kill(), which only signals the shell. functions-framework survived, reparented to init, and kept serving on its port. Spawn it detached and tracked, the same way firebase#10893 handles the discovery server, and give FunctionsRuntimeInstance an optional kill() that the worker uses instead of process.kill(). For Python it kills the whole process group with killProcessTree(). Fixes firebase#11069
There was a problem hiding this comment.
Code Review
This pull request addresses an issue where the Functions emulator leaves the Python runtime running after shutdown by introducing a custom kill method to clean up the process tree and tracking virtual environment child processes. A review comment suggests simplifying a redundant if-else block in RuntimeWorkerPool where both branches execute the same w.kill() statement.
- Skip the process-group kill once the child has exited, so a recycled pid can't take down an unrelated group. - Drop the signal-time tracking for emulator runtimes: SIGINT/SIGTERM go through the emulator's coordinated shutdown, which drains in-flight requests first. Keep a process-exit safety net instead. - When a reload retires a worker whose response is only paused (backpressure), kill it after the response ends rather than on "pause", which would now truncate the response.
|
Pushed 467a2d0 after another review pass:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #11235 +/- ##
=======================================
Coverage ? 61.27%
=======================================
Files ? 654
Lines ? 44115
Branches ? 9084
=======================================
Hits ? 27033
Misses ? 14823
Partials ? 2259 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
IzaakGough
left a comment
There was a problem hiding this comment.
Thank you for the contribution! I left some comments in-line
IzaakGough
left a comment
There was a problem hiding this comment.
Right fix for #11069: the shell wrapper absorbs process.kill(), and detaching plus the group kill reaches functions-framework. I checked the mechanism with a shell-wrapped Python server on macOS, same result as your Linux run.
Two things on the description:
The pause handling in RuntimeWorker.request() is worth a line. It applies to Node and Dart workers too: a FINISHING worker now waits for a streaming response to finish before being killed, where previously it was killed on pause and the response was cut off. It is an improvement, but it is a behaviour change on all runtimes, not only Python.
The description still says startPython tracks the child with trackVirtualEnvChild, but the current commit uses its own exit listener instead. Worth updating so the two stay in step.
| // Signals go through the emulator's own coordinated shutdown, which drains | ||
| // in-flight requests before killing; this only covers an exit that skips it. | ||
| const killOnExit = (): void => killPythonRuntime(childProcess); | ||
| process.once("exit", killOnExit); |
There was a problem hiding this comment.
This adds one exit listener per Python runtime, and idle runtimes stay alive for the whole session, one per function. With the listeners cli.ts and hub.ts already register, invoking around 9 distinct Python functions in one session makes Node print MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 exit listeners added to [process]. It is only a warning, but it looks like a leak to anyone who sees it.
A single module-level listener over a Set<ChildProcess> of live runtimes would avoid it, which is what the exit half of trackVirtualEnvChild in src/functions/python.ts already does. Reusing that function directly doesn't work here because its signal handlers would SIGKILL the runtimes before the emulator's clean shutdown drains in-flight requests, so it would need the exit half on its own, or a small set local to this file.
Description
Fixes #11069.
startPythonspawnsfunctions-frameworkthroughrunWithVirtualEnv's shell wrapper (. venv/bin/activate && functions-framework). Every shutdown path infunctionsRuntimeWorker.tscallsruntime.process.kill(), which signals the shell only.functions-frameworksurvives, reparents to init, and keeps serving on its port. That covers emulator exit, code-change refresh, FATAL logs, and request errors.This applies the same approach as #10893 (the deploy-time discovery server) to the emulator:
startPythonspawns withdetached: !IS_WINDOWS, so the shell leads its own process group, and tracks the child withtrackVirtualEnvChild(untracked on exit). The CLI-exit/signal cleanup then reaches it too.FunctionsRuntimeInstancegets an optionalkill(). For Python it callskillProcessTree(pid), which kills the whole group (ortaskkill /Ton Windows).RuntimeWorker.kill()usesruntime.killwhen set and falls back toprocess.kill(), so Node and Dart runtimes are unchanged. All 6runtime.process.kill()call sites now go through it.Detaching without the group kill would make things worse: the shell's exit would untrack the child while Python kept running. That's why both changes come together.
Scenarios Tested
functions-framework(a Python HTTP server), using the compiledrunWithVirtualEnvandkillProcessTree:ss -ltnpshowed the orphanedpython3(ppid 1) still listening.functionsRuntimeWorker.spec.ts:exit()uses the runtime's ownkill()and notprocess.kill(), and the worker reachesFINISHED.functionsRuntimeWorker,functionsEmulator*andfunctions/pythonspecs: 64 passing.tsc --noEmitis clean andeslintreports 0 errors.I didn't run a full
emulators:startwith the real Python SDK. Linux only; the Windows path reuseskillProcessTree's existingtaskkill /Tbranch.Replaces #11232, which I opened from my work account by mistake; reopened from the account covered by my CLA.
🤖 Generated with Claude Code