-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Copy pathweb_worker.zig
545 lines (466 loc) · 19.5 KB
/
web_worker.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
const bun = @import("root").bun;
const JSC = bun.JSC;
const Output = bun.Output;
const log = Output.scoped(.Worker, true);
const std = @import("std");
const JSValue = JSC.JSValue;
const Async = bun.Async;
const WTFStringImpl = @import("../string.zig").WTFStringImpl;
const Bool = std.atomic.Value(bool);
/// Shared implementation of Web and Node `Worker`
pub const WebWorker = struct {
/// null when haven't started yet
vm: ?*JSC.VirtualMachine = null,
status: std.atomic.Value(Status) = std.atomic.Value(Status).init(.start),
/// To prevent UAF, the `spin` function (aka the worker's event loop) will call deinit once this is set and properly exit the loop.
requested_terminate: Bool = Bool.init(false),
execution_context_id: u32 = 0,
parent_context_id: u32 = 0,
parent: *JSC.VirtualMachine,
/// Already resolved.
specifier: []const u8 = "",
preloads: [][]const u8 = &.{},
store_fd: bool = false,
arena: ?bun.MimallocArena = null,
name: [:0]const u8 = "Worker",
cpp_worker: *anyopaque,
mini: bool = false,
/// `user_keep_alive` is the state of the user's .ref()/.unref() calls
/// if false, then the parent poll will always be unref, otherwise the worker's event loop will keep the poll alive.
user_keep_alive: bool = false,
worker_event_loop_running: bool = true,
parent_poll_ref: Async.KeepAlive = .{},
argv: ?[]const WTFStringImpl,
execArgv: ?[]const WTFStringImpl,
pub const Status = enum(u8) {
start,
starting,
running,
terminated,
};
extern fn WebWorker__dispatchExit(?*JSC.JSGlobalObject, *anyopaque, i32) void;
extern fn WebWorker__dispatchOnline(this: *anyopaque, *JSC.JSGlobalObject) void;
extern fn WebWorker__dispatchError(*JSC.JSGlobalObject, *anyopaque, bun.String, JSValue) void;
export fn WebWorker__getParentWorker(vm: *JSC.VirtualMachine) ?*anyopaque {
const worker = vm.worker orelse return null;
return worker.cpp_worker;
}
pub fn hasRequestedTerminate(this: *const WebWorker) bool {
return this.requested_terminate.load(.monotonic);
}
pub fn setRequestedTerminate(this: *WebWorker) bool {
return this.requested_terminate.swap(true, .release);
}
export fn WebWorker__updatePtr(worker: *WebWorker, ptr: *anyopaque) bool {
worker.cpp_worker = ptr;
var thread = std.Thread.spawn(
.{ .stack_size = bun.default_thread_stack_size },
startWithErrorHandling,
.{worker},
) catch {
worker.deinit();
return false;
};
thread.detach();
return true;
}
fn resolveEntryPointSpecifier(
parent: *JSC.VirtualMachine,
str: []const u8,
error_message: *bun.String,
logger: *bun.logger.Log,
) ?[]const u8 {
if (parent.standalone_module_graph) |graph| {
if (graph.find(str) != null) {
return str;
}
// Since `bun build --compile` renames files to `.js` by
// default, we need to do the reverse of our file extension
// mapping.
//
// new Worker("./foo") -> new Worker("./foo.js")
// new Worker("./foo.ts") -> new Worker("./foo.js")
// new Worker("./foo.jsx") -> new Worker("./foo.js")
// new Worker("./foo.mjs") -> new Worker("./foo.js")
// new Worker("./foo.mts") -> new Worker("./foo.js")
// new Worker("./foo.cjs") -> new Worker("./foo.js")
// new Worker("./foo.cts") -> new Worker("./foo.js")
// new Worker("./foo.tsx") -> new Worker("./foo.js")
//
if (bun.strings.hasPrefixComptime(str, "./") or bun.strings.hasPrefixComptime(str, "../")) try_from_extension: {
var pathbuf: bun.PathBuffer = undefined;
var base = str;
base = bun.path.joinAbsStringBuf(bun.StandaloneModuleGraph.base_public_path_with_default_suffix, &pathbuf, &.{str}, .loose);
const extname = std.fs.path.extension(base);
// ./foo -> ./foo.js
if (extname.len == 0) {
pathbuf[base.len..][0..3].* = ".js".*;
if (graph.find(pathbuf[0 .. base.len + 3])) |js_file| {
return js_file.name;
}
break :try_from_extension;
}
// ./foo.ts -> ./foo.js
if (bun.strings.eqlComptime(extname, ".ts")) {
pathbuf[base.len - 3 .. base.len][0..3].* = ".js".*;
if (graph.find(pathbuf[0..base.len])) |js_file| {
return js_file.name;
}
break :try_from_extension;
}
if (extname.len == 4) {
inline for (.{ ".tsx", ".jsx", ".mjs", ".mts", ".cts", ".cjs" }) |ext| {
if (bun.strings.eqlComptime(extname, ext)) {
pathbuf[base.len - ext.len ..][0..".js".len].* = ".js".*;
const as_js = pathbuf[0 .. base.len - ext.len + ".js".len];
if (graph.find(as_js)) |js_file| {
return js_file.name;
}
break :try_from_extension;
}
}
}
}
}
if (JSC.WebCore.ObjectURLRegistry.isBlobURL(str)) {
if (JSC.WebCore.ObjectURLRegistry.singleton().has(str["blob:".len..])) {
return str;
} else {
error_message.* = bun.String.static("Blob URL is missing");
return null;
}
}
var resolved_entry_point: bun.resolver.Result = parent.transpiler.resolveEntryPoint(str) catch {
const out = logger.toJS(parent.global, bun.default_allocator, "Error resolving Worker entry point").toBunString(parent.global);
error_message.* = out;
return null;
};
const entry_path: *bun.fs.Path = resolved_entry_point.path() orelse {
error_message.* = bun.String.static("Worker entry point is missing");
return null;
};
return entry_path.text;
}
pub fn create(
cpp_worker: *void,
parent: *JSC.VirtualMachine,
name_str: bun.String,
specifier_str: bun.String,
error_message: *bun.String,
parent_context_id: u32,
this_context_id: u32,
mini: bool,
default_unref: bool,
argv_ptr: ?[*]WTFStringImpl,
argv_len: u32,
execArgv_ptr: ?[*]WTFStringImpl,
execArgv_len: u32,
preload_modules_ptr: ?[*]bun.String,
preload_modules_len: u32,
) callconv(.C) ?*WebWorker {
JSC.markBinding(@src());
log("[{d}] WebWorker.create", .{this_context_id});
var spec_slice = specifier_str.toUTF8(bun.default_allocator);
defer spec_slice.deinit();
const prev_log = parent.transpiler.log;
var temp_log = bun.logger.Log.init(bun.default_allocator);
parent.transpiler.setLog(&temp_log);
defer parent.transpiler.setLog(prev_log);
defer temp_log.deinit();
const preload_modules = if (preload_modules_ptr) |ptr|
ptr[0..preload_modules_len]
else
&.{};
const path = resolveEntryPointSpecifier(parent, spec_slice.slice(), error_message, &temp_log) orelse {
return null;
};
var preloads = std.ArrayList([]const u8).initCapacity(bun.default_allocator, preload_modules_len) catch bun.outOfMemory();
for (preload_modules) |module| {
const utf8_slice = module.toUTF8(bun.default_allocator);
defer utf8_slice.deinit();
if (resolveEntryPointSpecifier(parent, utf8_slice.slice(), error_message, &temp_log)) |preload| {
preloads.append(bun.default_allocator.dupe(u8, preload) catch bun.outOfMemory()) catch bun.outOfMemory();
}
if (!error_message.isEmpty()) {
for (preloads.items) |preload| {
bun.default_allocator.free(preload);
}
preloads.deinit();
return null;
}
}
var worker = bun.default_allocator.create(WebWorker) catch bun.outOfMemory();
worker.* = WebWorker{
.cpp_worker = cpp_worker,
.parent = parent,
.parent_context_id = parent_context_id,
.execution_context_id = this_context_id,
.mini = mini,
.specifier = bun.default_allocator.dupe(u8, path) catch bun.outOfMemory(),
.store_fd = parent.transpiler.resolver.store_fd,
.name = brk: {
if (!name_str.isEmpty()) {
break :brk std.fmt.allocPrintZ(bun.default_allocator, "{}", .{name_str}) catch bun.outOfMemory();
}
break :brk "";
},
.user_keep_alive = !default_unref,
.worker_event_loop_running = true,
.argv = if (argv_ptr) |ptr| ptr[0..argv_len] else null,
.execArgv = if (execArgv_ptr) |ptr| ptr[0..execArgv_len] else null,
.preloads = preloads.items,
};
worker.parent_poll_ref.ref(parent);
return worker;
}
pub fn startWithErrorHandling(
this: *WebWorker,
) void {
bun.Analytics.Features.workers_spawned += 1;
start(this) catch |err| {
Output.panic("An unhandled error occurred while starting a worker: {s}\n", .{@errorName(err)});
};
}
pub fn start(
this: *WebWorker,
) anyerror!void {
if (this.name.len > 0) {
Output.Source.configureNamedThread(this.name);
} else {
Output.Source.configureNamedThread("Worker");
}
if (this.hasRequestedTerminate()) {
this.exitAndDeinit();
return;
}
assert(this.status.load(.acquire) == .start);
assert(this.vm == null);
this.arena = try bun.MimallocArena.init();
var vm = try JSC.VirtualMachine.initWorker(this, .{
.allocator = this.arena.?.allocator(),
.args = this.parent.transpiler.options.transform_options,
.store_fd = this.store_fd,
.graph = this.parent.standalone_module_graph,
});
vm.allocator = this.arena.?.allocator();
vm.arena = &this.arena.?;
var b = &vm.transpiler;
b.configureDefines() catch {
this.flushLogs();
this.exitAndDeinit();
return;
};
// TODO: we may have to clone other parts of vm state. this will be more
// important when implementing vm.deinit()
const map = try vm.allocator.create(bun.DotEnv.Map);
map.* = try vm.transpiler.env.map.cloneWithAllocator(vm.allocator);
const loader = try vm.allocator.create(bun.DotEnv.Loader);
loader.* = bun.DotEnv.Loader.init(map, vm.allocator);
vm.transpiler.env = loader;
vm.loadExtraEnvAndSourceCodePrinter();
vm.is_main_thread = false;
JSC.VirtualMachine.is_main_thread_vm = false;
vm.onUnhandledRejection = onUnhandledRejection;
const callback = JSC.OpaqueWrap(WebWorker, WebWorker.spin);
this.vm = vm;
vm.global.vm().holdAPILock(this, callback);
}
/// Deinit will clean up vm and everything.
/// Early deinit may be called from caller thread, but full vm deinit will only be called within worker's thread.
fn deinit(this: *WebWorker) void {
log("[{d}] deinit", .{this.execution_context_id});
this.parent_poll_ref.unrefConcurrently(this.parent);
bun.default_allocator.free(this.specifier);
for (this.preloads) |preload| {
bun.default_allocator.free(preload);
}
bun.default_allocator.free(this.preloads);
bun.default_allocator.destroy(this);
}
fn flushLogs(this: *WebWorker) void {
JSC.markBinding(@src());
var vm = this.vm orelse return;
if (vm.log.msgs.items.len == 0) return;
const err = vm.log.toJS(vm.global, bun.default_allocator, "Error in worker");
const str = err.toBunString(vm.global);
defer str.deref();
WebWorker__dispatchError(vm.global, this.cpp_worker, str, err);
}
fn onUnhandledRejection(vm: *JSC.VirtualMachine, globalObject: *JSC.JSGlobalObject, error_instance_or_exception: JSC.JSValue) void {
// Prevent recursion
vm.onUnhandledRejection = &JSC.VirtualMachine.onQuietUnhandledRejectionHandlerCaptureValue;
var error_instance = error_instance_or_exception.toError() orelse error_instance_or_exception;
var array = bun.MutableString.init(bun.default_allocator, 0) catch unreachable;
defer array.deinit();
var buffered_writer_ = bun.MutableString.BufferedWriter{ .context = &array };
var buffered_writer = &buffered_writer_;
var worker = vm.worker orelse @panic("Assertion failure: no worker");
const writer = buffered_writer.writer();
const Writer = @TypeOf(writer);
// we buffer this because it'll almost always be < 4096
// when it's under 4096, we want to avoid the dynamic allocation
bun.JSC.ConsoleObject.format2(
.Debug,
globalObject,
&[_]JSC.JSValue{error_instance},
1,
Writer,
Writer,
writer,
.{
.enable_colors = false,
.add_newline = false,
.flush = false,
.max_depth = 32,
},
) catch |err| {
switch (err) {
error.JSError => {},
error.OutOfMemory => globalObject.throwOutOfMemory() catch {},
}
error_instance = globalObject.tryTakeException().?;
};
buffered_writer.flush() catch {
bun.outOfMemory();
};
JSC.markBinding(@src());
WebWorker__dispatchError(globalObject, worker.cpp_worker, bun.String.createUTF8(array.slice()), error_instance);
if (vm.worker) |worker_| {
_ = worker.setRequestedTerminate();
worker.parent_poll_ref.unrefConcurrently(worker.parent);
worker_.exitAndDeinit();
}
}
fn setStatus(this: *WebWorker, status: Status) void {
log("[{d}] status: {s}", .{ this.execution_context_id, @tagName(status) });
this.status.store(status, .release);
}
fn unhandledError(this: *WebWorker, _: anyerror) void {
this.flushLogs();
}
fn spin(this: *WebWorker) void {
log("[{d}] spin start", .{this.execution_context_id});
var vm = this.vm.?;
assert(this.status.load(.acquire) == .start);
this.setStatus(.starting);
vm.preload = this.preloads;
var promise = vm.loadEntryPointForWebWorker(this.specifier) catch {
this.flushLogs();
this.exitAndDeinit();
return;
};
if (promise.status(vm.global.vm()) == .rejected) {
const handled = vm.uncaughtException(vm.global, promise.result(vm.global.vm()), true);
if (!handled) {
vm.exit_handler.exit_code = 1;
this.exitAndDeinit();
return;
}
} else {
_ = promise.result(vm.global.vm());
}
this.flushLogs();
log("[{d}] event loop start", .{this.execution_context_id});
WebWorker__dispatchOnline(this.cpp_worker, vm.global);
this.setStatus(.running);
// don't run the GC if we don't actually need to
if (vm.isEventLoopAlive() or
vm.eventLoop().tickConcurrentWithCount() > 0)
{
vm.global.vm().releaseWeakRefs();
_ = vm.arena.gc(false);
_ = vm.global.vm().runGC(false);
}
// always doing a first tick so we call CppTask without delay after dispatchOnline
vm.tick();
while (vm.isEventLoopAlive()) {
vm.tick();
if (this.hasRequestedTerminate()) break;
vm.eventLoop().autoTickActive();
if (this.hasRequestedTerminate()) break;
}
log("[{d}] before exit {s}", .{ this.execution_context_id, if (this.hasRequestedTerminate()) "(terminated)" else "(event loop dead)" });
// Only call "beforeExit" if we weren't from a .terminate
if (!this.hasRequestedTerminate()) {
// TODO: is this able to allow the event loop to continue?
vm.onBeforeExit();
}
this.flushLogs();
this.exitAndDeinit();
log("[{d}] spin done", .{this.execution_context_id});
}
/// This is worker.ref()/.unref() from JS (Caller thread)
pub fn setRef(this: *WebWorker, value: bool) callconv(.C) void {
if (this.hasRequestedTerminate()) {
return;
}
this.setRefInternal(value);
}
pub fn setRefInternal(this: *WebWorker, value: bool) void {
if (value) {
this.parent_poll_ref.ref(this.parent);
} else {
this.parent_poll_ref.unref(this.parent);
}
}
/// Request a terminate (Called from main thread from worker.terminate(), or inside worker in process.exit())
/// The termination will actually happen after the next tick of the worker's loop.
pub fn requestTerminate(this: *WebWorker) callconv(.C) void {
if (this.status.load(.acquire) == .terminated) {
return;
}
if (this.setRequestedTerminate()) {
return;
}
log("[{d}] requestTerminate", .{this.execution_context_id});
if (this.vm) |vm| {
vm.eventLoop().wakeup();
}
this.setRefInternal(false);
}
/// This handles cleanup, emitting the "close" event, and deinit.
/// Only call after the VM is initialized AND on the same thread as the worker.
/// Otherwise, call `requestTerminate` to cause the event loop to safely terminate after the next tick.
pub fn exitAndDeinit(this: *WebWorker) noreturn {
JSC.markBinding(@src());
this.setStatus(.terminated);
bun.Analytics.Features.workers_terminated += 1;
log("[{d}] exitAndDeinit", .{this.execution_context_id});
const cpp_worker = this.cpp_worker;
var exit_code: i32 = 0;
var globalObject: ?*JSC.JSGlobalObject = null;
var vm_to_deinit: ?*JSC.VirtualMachine = null;
var loop: ?*bun.uws.Loop = null;
if (this.vm) |vm| {
loop = vm.uwsLoop();
this.vm = null;
vm.is_shutting_down = true;
vm.onExit();
exit_code = vm.exit_handler.exit_code;
globalObject = vm.global;
vm_to_deinit = vm;
}
var arena = this.arena;
WebWorker__dispatchExit(globalObject, cpp_worker, exit_code);
if (loop) |loop_| {
loop_.internal_loop_data.jsc_vm = null;
}
bun.uws.onThreadExit();
this.deinit();
if (vm_to_deinit) |vm| {
vm.deinit(); // NOTE: deinit here isn't implemented, so freeing workers will leak the vm.
}
bun.deleteAllPoolsForThreadExit();
if (arena) |*arena_| {
arena_.deinit();
}
bun.exitThread();
}
comptime {
@export(create, .{ .name = "WebWorker__create" });
@export(requestTerminate, .{ .name = "WebWorker__requestTerminate" });
@export(setRef, .{ .name = "WebWorker__setRef" });
_ = WebWorker__updatePtr;
}
};
const assert = bun.assert;