Skip to content

fix(emulator): don't orphan the Python functions runtime - #11232

Closed
omujahid-namaa wants to merge 1 commit into
firebase:mainfrom
omujahid-namaa:fix/python-emulator-orphan
Closed

omujahid-namaa wants to merge 1 commit into
firebase:mainfrom
omujahid-namaa:fix/python-emulator-orphan

Conversation

@omujahid-namaa

Copy link
Copy Markdown

Description

Fixes #11069.

startPython spawns functions-framework through runWithVirtualEnv's shell wrapper (. venv/bin/activate && functions-framework). Every shutdown path in functionsRuntimeWorker.ts calls runtime.process.kill(), which signals the shell only. functions-framework survives, 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:

  • startPython spawns with detached: !IS_WINDOWS, so the shell leads its own process group, and tracks the child with trackVirtualEnvChild (untracked on exit). The CLI-exit/signal cleanup then reaches it too.
  • FunctionsRuntimeInstance gets an optional kill(). For Python it calls killProcessTree(pid), which kills the whole group (or taskkill /T on Windows).
  • RuntimeWorker.kill() uses runtime.kill when set and falls back to process.kill(), so Node and Dart runtimes are unchanged. All 6 runtime.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

  • Repro of the mechanism with a real venv and a stub functions-framework (a Python HTTP server), using the compiled runWithVirtualEnv and killProcessTree:
    OLD process.kill()        serving before kill: true | still serving after kill: true
    NEW detached + group kill serving before kill: true | still serving after kill: false
    
    After the old path, ss -ltnp showed the orphaned python3 (ppid 1) still listening.
  • New unit test in functionsRuntimeWorker.spec.ts: exit() uses the runtime's own kill() and not process.kill(), and the worker reaches FINISHED.
  • functionsRuntimeWorker, functionsEmulator* and functions/python specs: 64 passing. tsc --noEmit is clean and eslint reports 0 errors.

I didn't run a full emulators:start with the real Python SDK. Linux only; the Windows path reuses killProcessTree's existing taskkill /T branch.

🤖 Generated with Claude Code

@google-cla

google-cla Bot commented Oct 3, 2026

Copy link
Copy Markdown

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 350 to 354
if (w.state === RuntimeWorkerState.IDLE) {
w.runtime.process.kill();
w.kill();
} else {
w.runtime.process.kill();
w.kill();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
  1. 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]>
@omujahid-namaa

Copy link
Copy Markdown
Author

Superseded by #11235, reopened from the account covered by my CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Functions emulator orphans the Python runtime: functions-framework survives shutdown still bound to its port

2 participants