aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/bun.js/webcore/blob.zig23
1 files changed, 14 insertions, 9 deletions
diff --git a/src/bun.js/webcore/blob.zig b/src/bun.js/webcore/blob.zig
index f2332a764..3852b4439 100644
--- a/src/bun.js/webcore/blob.zig
+++ b/src/bun.js/webcore/blob.zig
@@ -3246,29 +3246,34 @@ pub const Blob = struct {
bloblog("doReadFile", .{});
const Handler = NewReadFileHandler(Function);
- var promise = JSPromise.create(global);
-
- var handler = Handler{
+ var handler = bun.default_allocator.create(Handler) catch unreachable;
+ handler.* = Handler{
.context = this.*,
.globalThis = global,
};
- const promise_value = promise.asValue(global);
- promise_value.ensureStillAlive();
- handler.promise.strong.set(global, promise_value);
- var ptr = bun.default_allocator.create(Handler) catch unreachable;
- ptr.* = handler;
var file_read = Store.ReadFile.create(
bun.default_allocator,
this.store.?,
this.offset,
this.size,
*Handler,
- ptr,
+ handler,
Handler.run,
) catch unreachable;
var read_file_task = Store.ReadFile.ReadFileTask.createOnJSThread(bun.default_allocator, global, file_read) catch unreachable;
+
+ // Create the Promise only after the store has been ref()'d.
+ // The garbage collector runs on memory allocations
+ // The JSPromise is the next GC'd memory allocation.
+ // This shouldn't really fix anything, but it's a little safer.
+ var promise = JSPromise.create(global);
+ const promise_value = promise.asValue(global);
+ promise_value.ensureStillAlive();
+ handler.promise.strong.set(global, promise_value);
+
read_file_task.schedule();
+
bloblog("doReadFile: read_file_task scheduled", .{});
return promise_value;
}