Skip to content

Commit

Permalink
Implement napi_ref_threadsafe_function (oven-sh#4156)
Browse files Browse the repository at this point in the history
* Implement napi_ref_threadsafe_function

* work on this

* i hate event loops

* little better

* clean
  • Loading branch information
paperclover authored Aug 21, 2023
1 parent 397182b commit 6641198
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"${workspaceFolder}/src/bun.js/modules/*",
"${workspaceFolder}/src/deps",
"${workspaceFolder}/src/deps/boringssl/include/",
"${workspaceFolder}/src/deps/uws/uSockets/src"
"${workspaceFolder}/src/deps/uws/uSockets/src",
"${workspaceFolder}/src/napi"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ".vscode/cppdb"
Expand Down
4 changes: 2 additions & 2 deletions .vscode/launch.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/bun.js/base.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,14 @@ pub const PollRef = struct {
this.status = .active;
vm.uws_event_loop.?.refConcurrently();
}

pub fn refConcurrentlyFromEventLoop(this: *PollRef, loop: *JSC.EventLoop) void {
this.refConcurrently(loop.virtual_machine);
}

pub fn unrefConcurrentlyFromEventLoop(this: *PollRef, loop: *JSC.EventLoop) void {
this.unrefConcurrently(loop.virtual_machine);
}
};

const KQueueGenerationNumber = if (Environment.isMac and Environment.allow_assert) usize else u0;
Expand Down
13 changes: 12 additions & 1 deletion src/bun.js/bindings/napi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
#include "JavaScriptCore/JSSourceCode.h"
#include "JavaScriptCore/JSNativeStdFunction.h"
#include "JavaScriptCore/BigIntObject.h"
#include "ScriptExecutionContext.h"
#include "Strong.h"

#include "../modules/ObjectModule.h"

Expand Down Expand Up @@ -1763,7 +1765,16 @@ extern "C" napi_status napi_create_external(napi_env env, void* data,

auto* structure = Bun::NapiExternal::createStructure(vm, globalObject, globalObject->objectPrototype());
JSValue value = JSValue(Bun::NapiExternal::create(vm, structure, data, finalize_hint, finalize_cb));
JSC::EnsureStillAliveScope ensureStillAlive(value);

// With `fsevents`, their napi_create_external seems to get immediatly garbage
// collected for some unknown reason.
// See https://github.com/oven-sh/bun/issues/3978 and `fsevents.test.ts`
JSC::Strong<Unknown>* strong = new JSC::Strong<Unknown>(vm, value);
globalObject->scriptExecutionContext()->postTask([strong](auto& context) -> void {
strong->clear();
delete strong;
});

*result = toNapi(value);
return napi_ok;
}
Expand Down
7 changes: 4 additions & 3 deletions src/napi/napi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ pub const ThreadSafeFunction = struct {
/// Neither does napi_unref_threadsafe_function mark the thread-safe
/// functions as able to be destroyed nor does napi_ref_threadsafe_function
/// prevent it from being destroyed.
ref_for_process_exit: bool = false,
poll_ref: JSC.PollRef,

owning_threads: std.AutoArrayHashMapUnmanaged(u64, void) = .{},
owning_thread_lock: Lock = Lock.init(),
Expand Down Expand Up @@ -1345,11 +1345,11 @@ pub const ThreadSafeFunction = struct {
}

pub fn ref(this: *ThreadSafeFunction) void {
this.ref_for_process_exit = true;
this.poll_ref.refConcurrentlyFromEventLoop(this.event_loop);
}

pub fn unref(this: *ThreadSafeFunction) void {
this.ref_for_process_exit = false;
this.poll_ref.unrefConcurrentlyFromEventLoop(this.event_loop);
}

pub fn acquire(this: *ThreadSafeFunction) !void {
Expand Down Expand Up @@ -1415,6 +1415,7 @@ pub export fn napi_create_threadsafe_function(
.ctx = context,
.channel = ThreadSafeFunction.Queue.init(max_queue_size, bun.default_allocator),
.owning_threads = .{},
.poll_ref = JSC.PollRef.init(),
};
function.owning_threads.ensureTotalCapacity(bun.default_allocator, initial_thread_count) catch return genericFailure();
function.finalizer = .{ .ctx = thread_finalize_data, .fun = thread_finalize_cb };
Expand Down
Binary file modified test/bun.lockb
Binary file not shown.
10 changes: 10 additions & 0 deletions test/js/third_party/fsevents/fsevents-event-loop.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import fsevents from "fsevents";

if (process.argv.length < 3) {
console.log("Usage: bun fsevents-event-loop.ts <directory>");
process.exit(1);
}
fsevents.watch(process.argv[2], () => {
console.log("it works!");
process.exit(0);
});
20 changes: 20 additions & 0 deletions test/js/third_party/fsevents/fsevents.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { bunEnv, bunExe } from "harness";
import path from "path";
import fs from "fs";
import os from "os";

test("fsevents works (napi_ref_threadsafe_function keeps event loop alive)", async () => {
const tempFile = fs.mkdtempSync(path.join(os.tmpdir(), "fsevents-test-"));
const spawned = Bun.spawn({
cmd: [bunExe(), "run", path.join(import.meta.dir, "fsevents-event-loop.mjs"), tempFile],
env: bunEnv,
stdio: ["pipe", "pipe", "pipe"],
});
await Bun.sleep(50);
if (spawned.killed) {
throw new Error("event loop died, test failed");
}
await Bun.write(tempFile + "/hello.txt", "test");
expect(await spawned.exited).toBe(0);
expect(await new Response(spawned.stdout).text()).toBe("it works!\n");
});
6 changes: 6 additions & 0 deletions test/js/third_party/fsevents/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "fsevents-test",
"dependencies": {
"fsevents": "2.3.2"
}
}
1 change: 1 addition & 0 deletions test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"es-module-lexer": "1.3.0",
"esbuild": "0.18.6",
"express": "4.18.2",
"fsevents": "2.3.2",
"iconv-lite": "0.6.3",
"jest-extended": "4.0.0",
"lodash": "4.17.21",
Expand Down

0 comments on commit 6641198

Please sign in to comment.