Skip to content

fix(emulator): don't send an HTTP request to check runtime readiness - #11563

Open
yeegz wants to merge 4 commits into
firebase:mainfrom
yeegz:fix/8774-python-health-probe
Open

yeegz wants to merge 4 commits into
firebase:mainfrom
yeegz:fix/8774-python-health-probe

Conversation

@yeegz

@yeegz yeegz commented Nov 19, 2026

Copy link
Copy Markdown

Description

Fixes #8774.

The emulator checks that a function runtime is up by sending GET /__/health. The Node runtime handles that route itself, but Python's functions-framework sends every path to the user's function. So for Python, the health check actually ran the function once before the first real request, which is the "runs twice" in the issue.

This changes RuntimeWorker.isSocketReady() to just open a connection to the runtime's socket or port and close it. That's all the old check really proved, since the Node handler only returns 200, and it works the same for Node, Python and Dart. waitForSocketReady() still retries on ECONNREFUSED/ENOENT while the runtime starts.

Scenarios Tested

Added two tests in functionsRuntimeWorker.spec.ts that run a real HTTP server and count requests. Over TCP and over a Unix socket, the worker becomes ready and the server gets 0 requests. On main both fail (1 request). All emulator specs pass.

I didn't run the Python emulator end to end.

Sample Commands

firebase emulators:start --only functions with a Python @https_fn.on_request() function: the first request now runs it once.


I used an AI coding assistant for this and reviewed it myself.

🤖 Generated with Claude Code

The Functions emulator checked whether a runtime was ready by sending
GET /__/health. The Node runtime answers that itself, but the Python
functions-framework routes every path to the user's function, so the
probe ran the function once before the first real request.

Check readiness by opening a connection to the runtime's socket or port
instead, which needs no HTTP request.

Fixes firebase#8774

@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 updates the Functions emulator's readiness check to use a raw TCP/IPC connection instead of sending an HTTP request, preventing Python HTTP functions from being invoked twice on their first request. The review feedback suggests improving socket resource management by using socket.destroy() instead of socket.end() to avoid potential file descriptor leaks, and cleaning up temporary directories created during unit tests to prevent resource leaks in the test environment.

Comment on lines +253 to +258
socket.once("connect", () => {
socket.end();
// Set the worker state to IDLE for new work
this.readyForWork();
resolve();
});

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

Using socket.end() only half-closes the TCP connection (sends a FIN packet) and waits for the other side to close its end. Since this is a simple readiness probe, we should immediately release the socket resources using socket.destroy(). This prevents potential file descriptor leaks or sockets getting stuck in CLOSE_WAIT / TIME_WAIT states if the runtime process does not close the connection promptly.

Suggested change
socket.once("connect", () => {
socket.end();
// Set the worker state to IDLE for new work
this.readyForWork();
resolve();
});
socket.once("connect", () => {
socket.destroy();
// Set the worker state to IDLE for new work
this.readyForWork();
resolve();
});

Comment on lines +335 to +348
let server: http.Server;
let requests: number;

beforeEach(() => {
requests = 0;
server = http.createServer((req, res) => {
requests++;
res.end();
});
});

afterEach(async () => {
await new Promise((resolve) => server.close(resolve));
});

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 test creates a temporary directory using fs.mkdtempSync but never cleans it up. To prevent leaking temporary directories on the host machine or CI environment, we should track the created directories and clean them up in afterEach after the server has been closed.

  let server: http.Server;
  let requests: number;
  let tempDirs: string[] = [];

  beforeEach(() => {
    requests = 0;
    tempDirs = [];
    server = http.createServer((req, res) => {
      requests++;
      res.end();
    });
  });

  afterEach(async () => {
    await new Promise((resolve) => server.close(resolve));
    for (const dir of tempDirs) {
      try {
        fs.rmSync(dir, { recursive: true, force: true });
      } catch (err) {
        // Ignore cleanup errors
      }
    }
  });

Comment on lines +363 to +366
const socketPath = path.join(
fs.mkdtempSync(path.join(os.tmpdir(), "fire-emu-")),
"runtime.sock",
);

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

Track the created temporary directory so that it can be cleaned up in afterEach.

    const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "fire-emu-"));
    tempDirs.push(tempDir);
    const socketPath = path.join(tempDir, "runtime.sock");

This branch has not been deployed

No deployments
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.

Firestore emulator calls functions twice on startup

2 participants