Skip to content

Functions emulator decodes runtime output per chunk and shares one line buffer between stdout and stderr #11522

Open

Description

@trackoor

[REQUIRED] Environment info

firebase-tools: 15.36.0 (also reproduced on main at bd1d9f9)

Platform: Debian GNU/Linux 12 (x86_64), Node.js v26.13.0

[REQUIRED] Test case

firebase.json:

{
  "functions": { "source": "functions", "runtime": "nodejs24" },
  "emulators": { "functions": { "port": 5001 } }
}

functions/package.json:

{
  "name": "functions",
  "main": "index.js",
  "dependencies": {
    "firebase-admin": "^13.0.0",
    "firebase-functions": "^7.0.0"
  }
}

functions/index.js:

const { onRequest } = require("firebase-functions/v2/https");

exports.hello = onRequest(async (req, res) => {
  console.log("日".repeat(25000));

  process.stdout.write("stdout-1 ");
  await new Promise((r) => setTimeout(r, 50));
  console.error("stderr-line");
  await new Promise((r) => setTimeout(r, 50));
  process.stdout.write("stdout-2\n");

  res.send("ok\n");
});

[REQUIRED] Steps to reproduce

npm --prefix functions install
npx [email protected] emulators:exec --project demo-test --only functions \
  "curl -fsS http://127.0.0.1:5001/demo-test/us-central1/hello"

[REQUIRED] Expected behavior

  1. The first log line consists of 25,000 "日" characters with no U+FFFD replacement characters.
  2. stderr and stdout are buffered independently, producing > stderr-line followed by > stdout-1 stdout-2.

[REQUIRED] Actual behavior

  1. In the first log line, the "日" straddling the 64 KiB pipe read boundary (e6 | 97 a5) is decoded as three U+FFFD replacement characters instead of "日".
  2. The unfinished stdout write and the stderr line are concatenated into one log entry:
>  stdout-1 stderr-line
>  stdout-2

In src/emulator/functionsRuntimeWorker.ts, RuntimeWorker passes both childProc.stdout and childProc.stderr chunks into processStream(data, stdBuffer), which shares a single stdBuffer across both streams and decodes each Buffer chunk independently with buf += s.toString() instead of using a per-stream StringDecoder or setEncoding("utf8").

Credit: https://ucsbplse.github.io/

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions