Repository navigation
Conversation
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
There was a problem hiding this comment.
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.
| socket.once("connect", () => { | ||
| socket.end(); | ||
| // Set the worker state to IDLE for new work | ||
| this.readyForWork(); | ||
| resolve(); | ||
| }); |
There was a problem hiding this comment.
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.
| 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(); | |
| }); |
| 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)); | ||
| }); |
There was a problem hiding this comment.
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
}
}
});| const socketPath = path.join( | ||
| fs.mkdtempSync(path.join(os.tmpdir(), "fire-emu-")), | ||
| "runtime.sock", | ||
| ); |
…h-probe # Conflicts: # CHANGELOG.md
…h-probe # Conflicts: # CHANGELOG.md
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 onECONNREFUSED/ENOENTwhile the runtime starts.Scenarios Tested
Added two tests in
functionsRuntimeWorker.spec.tsthat 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. Onmainboth fail (1 request). All emulator specs pass.I didn't run the Python emulator end to end.
Sample Commands
firebase emulators:start --only functionswith 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