Fix windows regression in Bun.main (#9156)

* Fix windows regression in Bun.main

* Handle invalid handles

* Fix flaky test

* Better launch config

* Fixup
This commit is contained in:
Jarred Sumner
2024-02-28 17:17:14 -08:00
parent 6901640e1f
commit 607facb858
7 changed files with 122 additions and 79 deletions

132
.vscode/launch.json generated vendored
View File

@@ -18,7 +18,7 @@
"env": {
"FORCE_COLOR": "1",
"BUN_DEBUG_QUIET_LOGS": "1",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2"
},
"console": "internalConsole"
},
@@ -47,7 +47,7 @@
"env": {
"FORCE_COLOR": "1",
"BUN_DEBUG_QUIET_LOGS": "1",
"BUN_GARBAGE_COLLECTOR_LEVEL": "0",
"BUN_GARBAGE_COLLECTOR_LEVEL": "0"
},
"console": "internalConsole"
},
@@ -61,7 +61,7 @@
"env": {
"FORCE_COLOR": "1",
"BUN_DEBUG_QUIET_LOGS": "0",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2"
},
"console": "internalConsole"
},
@@ -75,7 +75,7 @@
"env": {
"FORCE_COLOR": "1",
"BUN_DEBUG_QUIET_LOGS": "1",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2"
},
"console": "internalConsole"
},
@@ -89,7 +89,7 @@
"env": {
"FORCE_COLOR": "1",
"BUN_DEBUG_QUIET_LOGS": "1",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2"
},
"console": "internalConsole"
},
@@ -108,7 +108,7 @@
},
"console": "internalConsole",
"serverReadyAction": {
"pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/",
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
"action": "openExternally"
}
@@ -128,7 +128,7 @@
},
"console": "internalConsole",
"serverReadyAction": {
"pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/",
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
"action": "openExternally"
}
@@ -144,9 +144,9 @@
"env": {
"FORCE_COLOR": "0",
"BUN_DEBUG_QUIET_LOGS": "1",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2"
},
"console": "internalConsole",
"console": "internalConsole"
},
{
"type": "lldb",
@@ -158,7 +158,7 @@
"env": {
"FORCE_COLOR": "1",
"BUN_DEBUG_QUIET_LOGS": "1",
"BUN_GARBAGE_COLLECTOR_LEVEL": "0",
"BUN_GARBAGE_COLLECTOR_LEVEL": "0"
},
"console": "internalConsole"
},
@@ -172,7 +172,7 @@
"env": {
"FORCE_COLOR": "1",
"BUN_DEBUG_QUIET_LOGS": "0",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2"
},
"console": "internalConsole"
},
@@ -186,7 +186,7 @@
"env": {
"FORCE_COLOR": "1",
"BUN_DEBUG_QUIET_LOGS": "1",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2"
},
"console": "internalConsole"
},
@@ -200,7 +200,7 @@
"env": {
"FORCE_COLOR": "1",
"BUN_DEBUG_QUIET_LOGS": "1",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2"
},
"console": "internalConsole"
},
@@ -219,10 +219,10 @@
},
"console": "internalConsole",
"serverReadyAction": {
"pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/",
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
"action": "openExternally"
},
}
},
{
"type": "lldb",
@@ -239,10 +239,10 @@
},
"console": "internalConsole",
"serverReadyAction": {
"pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/",
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
"action": "openExternally"
},
}
},
// bun test [...]
{
@@ -255,7 +255,7 @@
"env": {
"FORCE_COLOR": "1",
"BUN_DEBUG_QUIET_LOGS": "1",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2"
},
"console": "internalConsole"
},
@@ -269,7 +269,7 @@
"env": {
"FORCE_COLOR": "1",
"BUN_DEBUG_QUIET_LOGS": "1",
"BUN_GARBAGE_COLLECTOR_LEVEL": "0",
"BUN_GARBAGE_COLLECTOR_LEVEL": "0"
},
"console": "internalConsole"
},
@@ -283,7 +283,7 @@
"env": {
"FORCE_COLOR": "1",
"BUN_DEBUG_QUIET_LOGS": "0",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2"
},
"console": "internalConsole"
},
@@ -297,7 +297,7 @@
"env": {
"FORCE_COLOR": "1",
"BUN_DEBUG_QUIET_LOGS": "1",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2"
},
"console": "internalConsole"
},
@@ -311,7 +311,7 @@
"env": {
"FORCE_COLOR": "1",
"BUN_DEBUG_QUIET_LOGS": "1",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2"
},
"console": "internalConsole"
},
@@ -330,7 +330,7 @@
},
"console": "internalConsole",
"serverReadyAction": {
"pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/",
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
"action": "openExternally"
}
@@ -350,7 +350,7 @@
},
"console": "internalConsole",
"serverReadyAction": {
"pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/",
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
"action": "openExternally"
}
@@ -380,7 +380,7 @@
"env": {
"FORCE_COLOR": "1",
"BUN_DEBUG_QUIET_LOGS": "1",
"BUN_GARBAGE_COLLECTOR_LEVEL": "0",
"BUN_GARBAGE_COLLECTOR_LEVEL": "0"
},
"console": "internalConsole"
},
@@ -399,7 +399,7 @@
},
"console": "internalConsole",
"serverReadyAction": {
"pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/",
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
"action": "openExternally"
}
@@ -434,7 +434,29 @@
"name": "BUN_GARBAGE_COLLECTOR_LEVEL",
"value": "2"
}
],
]
},
{
"type": "cppvsdbg",
"request": "launch",
"name": "Windows: bun test --only [file]",
"program": "${workspaceFolder}/build/bun-debug.exe",
"args": ["test", "--only", "${file}"],
"cwd": "${workspaceFolder}/test",
"environment": [
{
"name": "FORCE_COLOR",
"value": "1"
},
{
"name": "BUN_DEBUG_QUIET_LOGS",
"value": "1"
},
{
"name": "BUN_GARBAGE_COLLECTOR_LEVEL",
"value": "2"
}
]
},
{
"type": "cppvsdbg",
@@ -456,7 +478,7 @@
"name": "BUN_GARBAGE_COLLECTOR_LEVEL",
"value": "0"
}
],
]
},
{
"type": "cppvsdbg",
@@ -478,7 +500,7 @@
"name": "BUN_GARBAGE_COLLECTOR_LEVEL",
"value": "2"
}
],
]
},
{
"type": "cppvsdbg",
@@ -506,10 +528,10 @@
}
],
"serverReadyAction": {
"pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/",
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
"action": "openExternally"
},
}
},
{
"type": "cppvsdbg",
@@ -537,10 +559,10 @@
}
],
"serverReadyAction": {
"pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/",
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
"action": "openExternally"
},
}
},
// Windows: bun run [file]
{
@@ -563,7 +585,7 @@
"name": "BUN_GARBAGE_COLLECTOR_LEVEL",
"value": "2"
}
],
]
},
{
"type": "cppvsdbg",
@@ -585,7 +607,7 @@
"name": "BUN_GARBAGE_COLLECTOR_LEVEL",
"value": "0"
}
],
]
},
{
"type": "cppvsdbg",
@@ -607,7 +629,7 @@
"name": "BUN_GARBAGE_COLLECTOR_LEVEL",
"value": "2"
}
],
]
},
{
"type": "cppvsdbg",
@@ -635,10 +657,10 @@
}
],
"serverReadyAction": {
"pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/",
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
"action": "openExternally"
},
}
},
{
"type": "cppvsdbg",
@@ -666,10 +688,10 @@
}
],
"serverReadyAction": {
"pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/",
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
"action": "openExternally"
},
}
},
// Windows: bun test [...]
{
@@ -692,7 +714,7 @@
"name": "BUN_GARBAGE_COLLECTOR_LEVEL",
"value": "2"
}
],
]
},
{
"type": "cppvsdbg",
@@ -714,7 +736,7 @@
"name": "BUN_GARBAGE_COLLECTOR_LEVEL",
"value": "0"
}
],
]
},
{
"type": "cppvsdbg",
@@ -736,7 +758,7 @@
"name": "BUN_GARBAGE_COLLECTOR_LEVEL",
"value": "2"
}
],
]
},
{
"type": "cppvsdbg",
@@ -758,7 +780,7 @@
"name": "BUN_GARBAGE_COLLECTOR_LEVEL",
"value": "2"
}
],
]
},
{
"type": "cppvsdbg",
@@ -780,7 +802,7 @@
"name": "BUN_GARBAGE_COLLECTOR_LEVEL",
"value": "2"
}
],
]
},
{
"type": "cppvsdbg",
@@ -808,10 +830,10 @@
}
],
"serverReadyAction": {
"pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/",
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
"action": "openExternally"
},
}
},
{
"type": "cppvsdbg",
@@ -839,10 +861,10 @@
}
],
"serverReadyAction": {
"pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/",
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
"action": "openExternally"
},
}
},
// Windows: bun test [*]
{
@@ -865,7 +887,7 @@
"name": "BUN_GARBAGE_COLLECTOR_LEVEL",
"value": "2"
}
],
]
},
{
"type": "cppvsdbg",
@@ -887,7 +909,7 @@
"name": "BUN_GARBAGE_COLLECTOR_LEVEL",
"value": "0"
}
],
]
},
{
"type": "cppvsdbg",
@@ -915,10 +937,10 @@
}
],
"serverReadyAction": {
"pattern": "https:\/\/debug.bun.sh\/#localhost:([0-9]+)/",
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
"action": "openExternally"
},
}
},
{
"type": "cppvsdbg",
@@ -928,7 +950,7 @@
"args": ["src/runner.node.mjs"],
"cwd": "${workspaceFolder}/packages/bun-internal-test",
"console": "internalConsole"
},
}
],
"inputs": [
{
@@ -940,6 +962,6 @@
"id": "testName",
"type": "promptString",
"description": "Usage: bun test [...]"
},
}
]
}

View File

@@ -992,11 +992,9 @@ pub fn getMain(
break :use_resolved_path;
}
const fd = bun.sys.openat(
// avoid going thorugh libuv for this one.
bun.toFD(std.fs.cwd().fd),
&(std.os.toPosixPath(vm.main) catch break :use_resolved_path),
const fd = bun.sys.openatA(
if (comptime Environment.isWindows) bun.invalid_fd else bun.toFD(std.fs.cwd().fd),
vm.main,
// Open with the minimum permissions necessary for resolving the file path.
if (comptime Environment.isLinux) std.os.O.PATH else std.os.O.RDONLY,

View File

@@ -46,7 +46,7 @@ pub const Cli = struct {
var wait_group: sync.WaitGroup = undefined;
var log_: logger.Log = undefined;
pub fn startTransform(_: std.mem.Allocator, _: Api.TransformOptions, _: *logger.Log) anyerror!void {}
pub fn start(allocator: std.mem.Allocator, _: anytype, _: anytype, comptime MainPanicHandler: type) void {
pub fn start(allocator: std.mem.Allocator, comptime MainPanicHandler: type) void {
start_time = std.time.nanoTimestamp();
log_ = logger.Log.init(allocator);

View File

@@ -101,6 +101,13 @@ pub const FDImpl = packed struct {
}
}
pub fn fromSystemWithoutAssertion(system_fd: System) FDImpl {
return FDImpl{
.kind = .system,
.value = .{ .as_system = handleToNumber(system_fd) },
};
}
pub fn fromSystem(system_fd: System) FDImpl {
if (env.os == .windows) {
// the current process fd is max usize
@@ -108,10 +115,7 @@ pub const FDImpl = packed struct {
std.debug.assert(@intFromPtr(system_fd) <= std.math.maxInt(SystemAsInt));
}
return FDImpl{
.kind = .system,
.value = .{ .as_system = handleToNumber(system_fd) },
};
return fromSystemWithoutAssertion(system_fd);
}
pub fn fromUV(uv_fd: UV) FDImpl {

View File

@@ -39,11 +39,16 @@ pub fn main() void {
if (Environment.isWindows) {
environ = @ptrCast(std.os.environ.ptr);
_environ = @ptrCast(std.os.environ.ptr);
bun.win32.STDOUT_FD = bun.toFD(std.io.getStdOut().handle);
bun.win32.STDERR_FD = bun.toFD(std.io.getStdErr().handle);
bun.win32.STDIN_FD = bun.toFD(std.io.getStdIn().handle);
const peb = std.os.windows.peb();
const stdout = peb.ProcessParameters.hStdOutput;
const stderr = peb.ProcessParameters.hStdError;
const stdin = peb.ProcessParameters.hStdInput;
bun.Output.buffered_stdin.unbuffered_reader.context.handle = std.io.getStdIn().handle;
bun.win32.STDERR_FD = if (stderr != std.os.windows.INVALID_HANDLE_VALUE) bun.toFD(stderr) else bun.invalid_fd;
bun.win32.STDOUT_FD = if (stdout != std.os.windows.INVALID_HANDLE_VALUE) bun.toFD(stdout) else bun.invalid_fd;
bun.win32.STDIN_FD = if (stdin != std.os.windows.INVALID_HANDLE_VALUE) bun.toFD(stdin) else bun.invalid_fd;
bun.buffered_stdin.unbuffered_reader.context.handle = stdin;
const w = std.os.windows;
@@ -52,9 +57,8 @@ pub fn main() void {
_ = w.kernel32.SetConsoleOutputCP(CP_UTF8);
var mode: w.DWORD = undefined;
const stdoutHandle = w.peb().ProcessParameters.hStdOutput;
if (w.kernel32.GetConsoleMode(stdoutHandle, &mode) != 0) {
_ = SetConsoleMode(stdoutHandle, mode | w.ENABLE_VIRTUAL_TERMINAL_PROCESSING);
if (w.kernel32.GetConsoleMode(stdout, &mode) != 0) {
_ = SetConsoleMode(stdout, mode | w.ENABLE_VIRTUAL_TERMINAL_PROCESSING);
}
}
@@ -79,5 +83,5 @@ pub fn main() void {
);
}
bun.CLI.Cli.start(bun.default_allocator, stdout, stderr, MainPanicHandler);
bun.CLI.Cli.start(bun.default_allocator, MainPanicHandler);
}

View File

@@ -11,3 +11,15 @@ ws.onmessage = message => {
}, 300);
}
};
ws.onclose = () => {
console.log("Closed!");
};
ws.onerror = e => {
console.error(e);
};
ws.onopen = () => {
console.log("Connected!");
};

View File

@@ -1,7 +1,7 @@
import { describe, it, expect } from "bun:test";
import { bunExe, bunEnv, gc } from "harness";
import { readFileSync } from "fs";
import { join } from "path";
import { join, resolve } from "path";
import process from "process";
const TEST_WEBSOCKET_HOST = process.env.TEST_WEBSOCKET_HOST || "wss://ws.postman-echo.com/raw";
@@ -566,6 +566,7 @@ describe("websocket in subprocess", () => {
});
it("should exit after server stop and 0 messages", async () => {
const { promise, resolve } = Promise.withResolvers();
const server = Bun.serve({
port: 0,
fetch(req, server) {
@@ -576,7 +577,9 @@ describe("websocket in subprocess", () => {
return new Response("http response");
},
websocket: {
open(ws) {},
open(ws) {
resolve();
},
message(ws, message) {},
close(ws) {},
},
@@ -584,12 +587,12 @@ describe("websocket in subprocess", () => {
const subprocess = Bun.spawn({
cmd: [bunExe(), import.meta.dir + "/websocket-subprocess.ts", `http://${server.hostname}:${server.port}`],
stderr: "pipe",
stdin: "pipe",
stdout: "pipe",
stderr: "inherit",
stdin: "inherit",
stdout: "inherit",
env: bunEnv,
});
await promise;
server.stop(true);
expect(await subprocess.exited).toBe(0);
});