oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.87k stars 2.74k forks source link

\x01 byte written to child process #13799

Open mattfysh opened 1 month ago

mattfysh commented 1 month ago

What version of Bun is running?

1.1.27+267afa293

What platform is your computer?

Darwin 23.6.0 arm64 arm

What steps can reproduce the bug?

index.ts

import { spawn } from 'bun'

const proc = spawn({
  cmd: ['python3', 'child.py'],
  serialization: 'json',
  stdio: ['inherit', 'inherit', 'inherit'],
  ipc(msg) {},
})

proc.send({ test: true })

child.py

import os

fd = 3
read_pipe = os.fdopen(fd, "r")
line = read_pipe.readline().strip()
print("recv: ", line.encode())

What is the expected behavior?

recv:  b'{"test":true}'

What do you see instead?

Note the \x01 byte

recv:  b'\x01{"test":true}'

Additional information

The following code run with node produces the correct result:

import { spawn } from 'node:child_process'

const proc = spawn('python3', ['child.py'], {
  stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
})

proc.send({ test: true })
kumanoko24 commented 3 weeks ago

Not sure if it is related:

OS:

System Version: macOS 14.6.1 (23G93)
Kernel Version: Darwin 23.6.0 (arm64)

Bun version: 1.1.29

Files: bun ./src/index.ts:

import { resolve } from "node:path";

const nodeSubProcess = Bun.spawn({
  cmd: ["node", "./src/node/hello.js"],
  cwd: resolve(import.meta.dir, ".."),
  ipc(message) {
    console.log("bun", message, "from node");
  },
  stdio: ["inherit", "inherit", "inherit"],
  serialization: "json",
});

setInterval(() => {
  nodeSubProcess.send("123");
}, 2000);

process.on("exit", () => {
  nodeSubProcess.kill();
});

./src/node/hello.js:

process.on("message", (msg) => {
  console.log("i am node:", msg, "from bun");

  process.send("i got " + msg);
});

process.on("uncaughtException", (a, b) => {
  console.error(a, b);
});

// keep it running
setInterval(() => {}, 1000);

The output looks like:

SyntaxError: Unexpected token '', ""123"" is not valid JSON
    at parse (<anonymous>)
    at parseChannelMessages (node:internal/child_process/serialization:152:15)
    at parseChannelMessages.next (<anonymous>)
    at channel.onread (node:internal/child_process:624:18) uncaughtException
SyntaxError: Unexpected token '', ""123"" is not valid JSON
    at parse (<anonymous>)
    at parseChannelMessages (node:internal/child_process/serialization:152:15)
    at parseChannelMessages.next (<anonymous>)
    at channel.onread (node:internal/child_process:624:18) uncaughtException
.
.
.
mattfysh commented 2 weeks ago

I think this could be related - the character that trips up the JSON parser does not render visibly, so if you're logging the contents of the json string you're parsing you might not see the error initially. try encoding the string into a buffer then logging the bytes (eg. as hex) - you'll see it there

nicktrn commented 6 days ago

Looks like this broke in v1.1.25. Tested with: https://bun.sh/docs/api/spawn#ipc-between-bun-node-js

According to git bisect and bun-pr this is where it broke: d55b5cc169c4b4661d28ce588ed0d0756122e3c9 (https://github.com/oven-sh/bun/pull/13381)

My Zig's no good but I hope this helps. Maybe one for the Head Baker @Jarred-Sumner