Skip to content

Commit

Permalink
fix a bundler crash (#12864)
Browse files Browse the repository at this point in the history
Co-authored-by: paperdave <[email protected]>
  • Loading branch information
paperclover and paperdave authored Jul 27, 2024
1 parent 32d9bb3 commit d547d8a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 18 deletions.
25 changes: 15 additions & 10 deletions src/bundler/bundle_v2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1656,17 +1656,11 @@ pub const BundleV2 = struct {

bundler.resolver.opts = bundler.options;

var this = try BundleV2.init(bundler, allocator, JSC.AnyEventLoop.init(allocator), false, JSC.WorkPool.get(), heap);
const this = try BundleV2.init(bundler, allocator, JSC.AnyEventLoop.init(allocator), false, JSC.WorkPool.get(), heap);
this.plugins = completion.plugins;
this.completion = completion;
completion.bundler = this;

errdefer {
var out_log = Logger.Log.init(bun.default_allocator);
this.bundler.log.appendToWithRecycled(&out_log, true) catch bun.outOfMemory();
completion.log = out_log;
}

defer {
if (this.graph.pool.pool.threadpool_context == @as(?*anyopaque, @ptrCast(this.graph.pool))) {
this.graph.pool.pool.threadpool_context = null;
Expand All @@ -1676,6 +1670,16 @@ pub const BundleV2 = struct {
this.deinit();
}

errdefer {
// Wait for wait groups to finish. There still may be
this.linker.source_maps.line_offset_wait_group.wait();
this.linker.source_maps.quoted_contents_wait_group.wait();

var out_log = Logger.Log.init(bun.default_allocator);
this.bundler.log.appendToWithRecycled(&out_log, true) catch bun.outOfMemory();
completion.log = out_log;
}

completion.result = .{
.value = .{
.output_files = try this.runFromJSInNewThread(config),
Expand Down Expand Up @@ -3826,7 +3830,7 @@ pub const LinkerContext = struct {

options: LinkerOptions = .{},

wait_group: ThreadPoolLib.WaitGroup = undefined,
wait_group: ThreadPoolLib.WaitGroup = .{},

ambiguous_result_pool: std.ArrayList(MatchImport) = undefined,

Expand Down Expand Up @@ -3864,10 +3868,10 @@ pub const LinkerContext = struct {
};

pub const SourceMapData = struct {
line_offset_wait_group: sync.WaitGroup = undefined,
line_offset_wait_group: sync.WaitGroup = .{},
line_offset_tasks: []Task = &.{},

quoted_contents_wait_group: sync.WaitGroup = undefined,
quoted_contents_wait_group: sync.WaitGroup = .{},
quoted_contents_tasks: []Task = &.{},

pub const Task = struct {
Expand Down Expand Up @@ -9113,6 +9117,7 @@ pub const LinkerContext = struct {
wait_group.deinit();
c.allocator.destroy(wait_group);
}
errdefer wait_group.wait();
{
var total_count: usize = 0;
for (chunks, chunk_contexts) |*chunk, *chunk_ctx| {
Expand Down
8 changes: 2 additions & 6 deletions src/thread_pool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,10 @@ pub const Batch = struct {
pub const WaitGroup = struct {
mutex: std.Thread.Mutex = .{},
counter: u32 = 0,
event: std.Thread.ResetEvent,
event: std.Thread.ResetEvent = .{},

pub fn init(self: *WaitGroup) void {
self.* = .{
.mutex = .{},
.counter = 0,
.event = undefined,
};
self.* = .{};
}

pub fn deinit(self: *WaitGroup) void {
Expand Down
14 changes: 12 additions & 2 deletions src/work_pool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@ pub fn NewWorkPool(comptime max_threads: ?usize) type {
@setCold(true);

pool = ThreadPool.init(.{
.max_threads = max_threads orelse @max(@as(u32, @truncate(std.Thread.getCpuCount() catch 0)), 2),
.max_threads = max_threads orelse @max(2, max_threads: {
if (bun.getenvZ("GOMAXPROCS")) |max_procs| try_override: {
break :max_threads std.fmt.parseInt(u32, max_procs, 10) catch
break :try_override;
}

break :max_threads @as(u32, @truncate(std.Thread.getCpuCount() catch 0));
}),
.stack_size = ThreadPool.default_thread_stack_size,
});
return &pool;
}

/// Initialization of WorkPool is not thread-safe, as it is
/// assumed a single main thread sets everything up. Calling
/// this afterwards is thread-safe.
pub inline fn get() *ThreadPool {
// lil racy
if (loaded) return &pool;
loaded = true;

Expand Down
37 changes: 37 additions & 0 deletions test/bundler/bun-build-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,43 @@ describe("Bun.build", () => {
throw new Error("should have thrown");
});

// https://github.com/oven-sh/bun/issues/12818
test("sourcemap + build error crash case", async () => {
const dir = tempDirWithFiles("build", {
"/src/file1.ts": `
import { A } from './dir';
console.log(A);
`,
"/src/dir/index.ts": `
import { B } from "./file3";
export const A = [B]
`,
"/src/dir/file3.ts": `
import { C } from "../file1"; // error
export const B = C;
`,
"/src/package.json": `
{ "type": "module" }
`,
"/src/tsconfig.json": `
{
"extends": "../tsconfig.json",
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"types": []
}
}
`,
});
const y = await Bun.build({
entrypoints: [join(dir, "src/file1.ts")],
outdir: join(dir, "out"),
sourcemap: "external",
external: ["@minecraft"],
});
});

test("invalid options throws", async () => {
expect(() => Bun.build({} as any)).toThrow();
expect(() =>
Expand Down

0 comments on commit d547d8a

Please sign in to comment.