Skip to content

Commit

Permalink
windows: fix sometimes crash when FDImpl.uv() is called on stdio (#13719
Browse files Browse the repository at this point in the history
)

Co-authored-by: Jarred Sumner <[email protected]>
Co-authored-by: Dave Caruso <[email protected]>
Co-authored-by: nektro <[email protected]>
  • Loading branch information
4 people authored Sep 7, 2024
1 parent f0a4b9f commit de5809b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/fd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,29 @@ pub const FDImpl = packed struct {
return switch (env.os) {
else => numberToHandle(this.value.as_system),
.windows => switch (this.kind) {
.system => std.debug.panic(
\\Cast {} -> FDImpl.UV makes closing impossible!
\\
\\The supplier of this FileDescriptor should call 'bun.toLibUVOwnedFD'
\\or 'FDImpl.makeLibUVOwned', probably where open() was called.
,
.{this},
),
.system => {
const w = std.os.windows;

const S = struct {
fn is_stdio_handle(id: w.DWORD, handle: w.HANDLE) bool {
const h = w.GetStdHandle(id) catch return false;
return handle == h;
}
};
const handle = this.encode().cast();
if (S.is_stdio_handle(w.STD_INPUT_HANDLE, handle)) return 0;
if (S.is_stdio_handle(w.STD_OUTPUT_HANDLE, handle)) return 1;
if (S.is_stdio_handle(w.STD_ERROR_HANDLE, handle)) return 2;

std.debug.panic(
\\Cast {} -> FDImpl.UV makes closing impossible!
\\
\\The supplier of this FileDescriptor should call 'bun.toLibUVOwnedFD'
\\or 'FDImpl.makeLibUVOwned', probably where open() was called.
,
.{this},
);
},
.uv => this.value.as_uv,
},
};
Expand Down
Binary file modified test/bun.lockb
Binary file not shown.
32 changes: 32 additions & 0 deletions test/js/third_party/pino/pino.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expect, it } from "bun:test";
import { bunEnv, bunExe } from "harness";

it("using pino does not crash, particularly on windows", async () => {
const proc = Bun.spawnSync({
cmd: [
bunExe(),
"-e",
`
const pino = require("pino");
const logger = pino({
transport: {
target: "pino-pretty",
options: { colorize: true },
},
});
logger.info("hi");
`,
],
env: bunEnv,
stdio: ["inherit", "pipe", "pipe"],
cwd: import.meta.dir,
});

const err = proc.stderr.toString("utf8");
const out = proc.stdout.toString("utf8");

expect(err).toBeEmpty();
expect(out).toContain("\u001B[32mINFO\u001B[39m");
expect(out).toContain("\u001B[36mhi\u001B[39m\n");
expect(proc.exitCode).toBe(0);
});
2 changes: 2 additions & 0 deletions test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
"nodemailer": "6.9.3",
"pg": "8.11.1",
"pg-connection-string": "2.6.1",
"pino": "9.4.0",
"pino-pretty": "11.2.2",
"postgres": "3.3.5",
"prisma": "5.1.1",
"prompts": "2.4.2",
Expand Down

0 comments on commit de5809b

Please sign in to comment.