Skip to content

Commit bb5c867

Browse files
pelikhanCopilot
andauthored
Avoid Node UTF-8 pipe crashes during Codex setup (#66631)
Read piped setup input as a Buffer before UTF-8 decoding to avoid nodejs/node#66341. Cover direct-tool catalog conversion, short-read pipes, and the gateway read path. Fixes #66628 Co-authored-by: Copilot App <[email protected]>
1 parent 982eae2 commit bb5c867

5 files changed

Lines changed: 70 additions & 3 deletions

File tree

‎actions/setup/js/convert_gateway_config_codex.cjs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ function main() {
5050
if (process.argv.includes("--direct-tools")) {
5151
const home = process.env.CODEX_HOME;
5252
if (!home) throw new Error("CODEX_HOME is required to configure Codex direct tools");
53-
const catalog = directToolCatalog(JSON.parse(fs.readFileSync(0, "utf8")));
53+
// Avoid Node's UTF-8 pipe short-read overflow (nodejs/node#66341).
54+
const catalog = directToolCatalog(JSON.parse(fs.readFileSync(0).toString("utf8")));
5455
const output = JSON.stringify(catalog);
5556
writeSecureOutput(path.join(home, "models.json"), output);
5657
return output;

‎actions/setup/js/convert_gateway_config_codex.test.cjs‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// @ts-check
2-
import { describe, it, expect, beforeEach, afterEach } from "vitest";
2+
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
33
import { createRequire } from "module";
4+
import fs from "fs";
45
import { mkdtempSync, rmSync, writeFileSync } from "fs";
6+
import { spawnSync } from "child_process";
57
import { join } from "path";
68
import { tmpdir } from "os";
79

@@ -39,18 +41,22 @@ describe("convert_gateway_config_codex", () => {
3941
let gatewayOutputFile;
4042
/** @type {Record<string, string | undefined>} */
4143
let savedEnv;
44+
/** @type {string[]} */
45+
let savedArgv;
4246

4347
beforeEach(() => {
4448
tempDir = mkdtempSync(join(tmpdir(), "codex-config-test-"));
4549
gatewayOutputFile = join(tempDir, "gateway-output.json");
4650

4751
savedEnv = {
52+
CODEX_HOME: process.env.CODEX_HOME,
4853
MCP_GATEWAY_OUTPUT: process.env.MCP_GATEWAY_OUTPUT,
4954
MCP_GATEWAY_DOMAIN: process.env.MCP_GATEWAY_DOMAIN,
5055
MCP_GATEWAY_PORT: process.env.MCP_GATEWAY_PORT,
5156
RUNNER_TEMP: process.env.RUNNER_TEMP,
5257
GH_AW_MCP_CLI_SERVERS: process.env.GH_AW_MCP_CLI_SERVERS,
5358
};
59+
savedArgv = process.argv;
5460

5561
process.env.MCP_GATEWAY_DOMAIN = "host.docker.internal";
5662
process.env.MCP_GATEWAY_PORT = "80";
@@ -59,6 +65,8 @@ describe("convert_gateway_config_codex", () => {
5965
});
6066

6167
afterEach(() => {
68+
vi.restoreAllMocks();
69+
process.argv = savedArgv;
6270
for (const [key, value] of Object.entries(savedEnv)) {
6371
if (value === undefined) {
6472
delete process.env[key];
@@ -77,6 +85,51 @@ describe("convert_gateway_config_codex", () => {
7785
process.env.MCP_GATEWAY_OUTPUT = gatewayOutputFile;
7886
}
7987

88+
it("reads the direct-tool catalog as a Buffer before decoding UTF-8", () => {
89+
const catalog = { models: [{ slug: "fixture-model", tool_mode: "code_mode_only", instructions: "\u00e9".repeat(70000) }] };
90+
const read = vi.spyOn(fs, "readFileSync").mockReturnValueOnce(Buffer.from(JSON.stringify(catalog)));
91+
process.env.CODEX_HOME = join(tempDir, "codex-home");
92+
process.argv = [...process.argv, "--direct-tools"];
93+
94+
const output = main();
95+
96+
expect(read).toHaveBeenNthCalledWith(1, 0);
97+
expect(JSON.parse(output)).toEqual({ models: [{ ...catalog.models[0], tool_mode: "direct" }] });
98+
const catalogPath = join(process.env.CODEX_HOME, "models.json");
99+
expect(fs.readFileSync(catalogPath, "utf8")).toBe(output);
100+
expect(fs.statSync(catalogPath).mode & 0o777).toBe(0o600);
101+
});
102+
103+
it("converts a catalog piped after more than 64 KiB of short reads", () => {
104+
const result = spawnSync(
105+
"bash",
106+
[
107+
"-o",
108+
"pipefail",
109+
"-c",
110+
`"$1" -e '
111+
let count = 0;
112+
const timer = setInterval(() => {
113+
process.stdout.write(" ".repeat(4000));
114+
if (++count === 24) {
115+
clearInterval(timer);
116+
process.stdout.write(JSON.stringify({models: [{slug: "fixture-model", tool_mode: "code_mode_only", instructions: "x".repeat(400000)}]}));
117+
}
118+
}, 5);
119+
' | "$1" "$2" --direct-tools`,
120+
"--",
121+
process.execPath,
122+
new URL("./convert_gateway_config_codex.cjs", import.meta.url).pathname,
123+
],
124+
{ env: { ...process.env, CODEX_HOME: tempDir }, encoding: "utf8", timeout: 10000 }
125+
);
126+
127+
expect(result.error).toBeUndefined();
128+
expect(result.status, result.stderr).toBe(0);
129+
const catalog = JSON.parse(fs.readFileSync(join(tempDir, "models.json"), "utf8"));
130+
expect(catalog.models).toEqual([{ slug: "fixture-model", tool_mode: "direct", instructions: "x".repeat(400000) }]);
131+
});
132+
80133
it("resolves host.docker.internal to 172.30.0.1 in TOML server URLs", () => {
81134
writeGatewayOutput({
82135
github: { url: "http://host.docker.internal:80/mcp/github", headers: { Authorization: "token abc" } },

‎actions/setup/js/start_mcp_gateway.cjs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,8 @@ async function main() {
776776
const configReadStart = nowMs();
777777
let mcpConfig;
778778
try {
779-
mcpConfig = fs.readFileSync(0, "utf8"); // fd 0 = stdin
779+
// Avoid Node's UTF-8 pipe short-read overflow (nodejs/node#66341).
780+
mcpConfig = fs.readFileSync(0).toString("utf8");
780781
} catch (err) {
781782
throw new Error(`Failed to read MCP configuration from stdin: ${getErrorMessage(err)}`, { cause: err });
782783
}

‎actions/setup/js/start_mcp_gateway.test.cjs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ import {
3131
} from "./start_mcp_gateway.cjs";
3232

3333
describe("start_mcp_gateway logging", () => {
34+
it("reads piped configuration as a Buffer before decoding UTF-8", () => {
35+
const source = fs.readFileSync(new URL("./start_mcp_gateway.cjs", import.meta.url), "utf8");
36+
expect(source).toContain('fs.readFileSync(0).toString("utf8")');
37+
expect(source).not.toContain('fs.readFileSync(0, "utf8")');
38+
});
39+
3440
it("does not create the legacy MCP gateway stderr log", () => {
3541
const source = fs.readFileSync(new URL("./start_mcp_gateway.cjs", import.meta.url), "utf8");
3642
expect(source).not.toContain("/tmp/gh-aw/mcp-logs/stderr.log");

‎docs/src/content/docs/troubleshooting/common-issues.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,12 @@ Use `min-integrity: unapproved` as a middle ground for community triage workflow
325325

326326
## Workflow Failures and Debugging
327327

328+
### Codex Setup Crashes Before Agent Execution
329+
330+
If `Configure Codex` exits with code 139 and logs a Node.js segmentation fault while converting the bundled model catalog, inspect the setup failure before changing terminal safe-output instructions. `missing_terminal_safe_output` can be a downstream symptom because the agent never started.
331+
332+
Node.js v24.21.0 and v26.8.0 through v26.10.0 have a [short-read buffer overflow in UTF-8 pipe reads](https://github.com/nodejs/node/issues/66341). The gh-aw Codex and MCP gateway setup scripts avoid this path by reading stdin as a Buffer and then decoding it with `.toString("utf8")`. Ensure the action scripts used by the compiled workflow include this workaround.
333+
328334
### Timeout Errors
329335

330336
GitHub Actions marks the run as `timed_out` when the job exceeds `timeout-minutes` (default: 20 min). Use the table below to map common engine error patterns to the correct fix, then recompile with `gh aw compile`. See [Long Build Times](/gh-aw/reference/sandbox/#long-build-times) for caching strategies and self-hosted runner recommendations.

0 commit comments

Comments
 (0)