From 75acec0aea3eb39afe9240d7952cc6106363c8de Mon Sep 17 00:00:00 2001 From: Aapo Alasuutari Date: Sat, 15 Oct 2022 16:49:46 +0300 Subject: fix(ext/ffi): Fix UnsafeCallback ref'ing making Deno enter a live-loop (#16216) Fixes #15136 Currently `UnsafeCallback` class' `ref()` and `unref()` methods rely on the `event_loop_middleware` implementation in core. If even a single `UnsafeCallback` is ref'ed, then the FFI event loop middleware will always return `true` to signify that there may still be more work for the event loop to do. The middleware handling in core does not wait a moment to check again, but will instead synchronously directly re-poll the event loop and middlewares for more work. This becomes a live-loop. This PR introduces a `Future` implementation for the `CallbackInfo` struct that acts as the intermediary data storage between an `UnsafeCallback` and the `libffi` C callback. Ref'ing a callback now means calling an async op that binds to the `CallbackInfo` Future and only resolves once the callback is unref'ed. The `libffi` C callback will call the waker of this Future when it fires to make sure that the main thread wakes up to receive the callback. --- test_ffi/tests/test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'test_ffi/tests/test.js') diff --git a/test_ffi/tests/test.js b/test_ffi/tests/test.js index 6bf3c47f8..0af94b906 100644 --- a/test_ffi/tests/test.js +++ b/test_ffi/tests/test.js @@ -515,6 +515,7 @@ logCallback.unref(); console.log("Thread safe call counter:", counter); returnU8Callback.ref(); await dylib.symbols.call_fn_ptr_return_u8_thread_safe(returnU8Callback.pointer); +// Purposefully do not unref returnU8Callback: Instead use it to test close() unrefing. // Test statics console.log("Static u32:", dylib.symbols.static_u32); @@ -585,7 +586,7 @@ After: ${postStr}`, })(); function assertIsOptimized(fn) { - const status = % GetOptimizationStatus(fn); + const status = %GetOptimizationStatus(fn); assert(status & (1 << 4), `expected ${fn.name} to be optimized, but wasn't`); } -- cgit v1.2.3