aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-07-22 19:56:39 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-07-22 19:56:39 -0700
commit53eb126898cf88f579cabc2baf8dafa06f031094 (patch)
tree168886747d857f016d5eba0a717d31541837cfe6
parent6809d08a90e7f8b658b847a0877b49ac47b0acc9 (diff)
downloadbun-53eb126898cf88f579cabc2baf8dafa06f031094.tar.gz
bun-53eb126898cf88f579cabc2baf8dafa06f031094.tar.zst
bun-53eb126898cf88f579cabc2baf8dafa06f031094.zip
ref() before creating the JSPromise
-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;
}