Repository navigation
fix(emulator): don't orphan the Python functions runtime - #11232
omujahid-namaa wants to merge 1 commit into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request fixes an issue where the Functions emulator leaves the Python runtime running after shutdown. It introduces a custom kill handler for Python runtime instances using killProcessTree and updates the RuntimeWorker to utilize this custom shutdown mechanism. Additionally, unit tests were added to verify the new shutdown behavior. The reviewer suggested simplifying a redundant if-else block in functionsRuntimeWorker.ts where both branches call w.kill(), which would also reduce unnecessary nesting in accordance with the style guide.
| if (w.state === RuntimeWorkerState.IDLE) { | ||
| w.runtime.process.kill(); | ||
| w.kill(); | ||
| } else { | ||
| w.runtime.process.kill(); | ||
| w.kill(); | ||
| } |
There was a problem hiding this comment.
The if-else statement here is redundant because both branches execute the exact same w.kill() statement. We can simplify this by directly calling w.kill() for all workers in the pool, which also reduces unnecessary nesting.
w.kill();References
- Reduce nesting as much as possible: Code should avoid unnecessarily deep nesting or long periods of nesting. (link)
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 Co-Authored-By: Claude Opus 5.5 <[email protected]>
58987e4 to
d8a2adb
Compare
|
Superseded by #11235, reopened from the account covered by my CLA. |
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.🤖 Generated with Claude Code