-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixup callbacks deadlock when calling callback very frequently (#158)
Fixes #157 Minimal test: 146cd39 The change here is in `UniffiCallInvoker`, reverting the implementation of `invokeBlocking` back to the Promise based implementation introduced in PR #88. The deadlock rate for the test is now 0/50. /cc @tayrevor Adding any more to the implementation (e.g. uncommenting the `console.log`) causes the same error as before, but it is completely deterministic: ```sh -- Going to 1024, now at: 264 -- Going to 1024, now at: 265 Assertion failed: (usedDbg_ && "Stats not submitted."), function ~CollectionStats, file HadesGC.cpp, line 259. ``` On my machine it _always_ stops after 265/1024. I am still unsure if this is a problem with production uniff-bindgen-react-native code, an artifact of the test-harness or something weirder. I'm going to suggest that this is addressed in a separate issue.
- Loading branch information
Showing
8 changed files
with
132 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "uniffi-example-callbacks" | ||
edition = "2021" | ||
version = "0.22.0" | ||
license = "MPL-2.0" | ||
publish = false | ||
|
||
[lib] | ||
crate-type = ["lib", "cdylib"] | ||
name = "uniffi_callbacks" | ||
|
||
[dependencies] | ||
uniffi = { workspace = true } | ||
thiserror = "1.0" | ||
|
||
[build-dependencies] | ||
uniffi = { workspace = true, features = ["build"] } | ||
|
||
[dev-dependencies] | ||
uniffi = { workspace = true, features = ["bindgen-tests"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
This is an example of UniFFI "callbacks". | ||
|
||
Callbacks are the ability to implement Rust traits in foreign languages. These are defined by | ||
[normal UniFFI traits](../../docs/manual/src/foreign_traits.md) or via ["callback" definitions](../../docs/manual/src/udl/callback_interfaces.md). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
*/ | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
*/ | ||
|
||
use std::{sync::Arc, thread::spawn}; | ||
|
||
uniffi::setup_scaffolding!(); | ||
|
||
#[uniffi::export(with_foreign)] | ||
pub trait EventListener: Send + Sync { | ||
fn on_event(&self, message: String, number: i32); | ||
} | ||
|
||
#[derive(uniffi::Object)] | ||
struct EventSource { | ||
listener: Arc<dyn EventListener>, | ||
} | ||
|
||
#[uniffi::export] | ||
impl EventSource { | ||
#[uniffi::constructor] | ||
fn new(listener: Arc<dyn EventListener>) -> Arc<Self> { | ||
Arc::new(Self { listener }) | ||
} | ||
fn start(self: Arc<Self>, message: String, until: i32) { | ||
let listener = self.listener.clone(); | ||
spawn(move || { | ||
for i in 0..until { | ||
listener.on_event(message.clone(), i); | ||
} | ||
}); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
fixtures/callbacks-regression/tests/bindings/test_callbacks_regression.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
*/ | ||
|
||
/* | ||
fixture=fixtures/callbacks-regression | ||
cargo xtask run \ | ||
--crate $fixture \ | ||
--ts-dir $fixture/generated \ | ||
--cpp-dir $fixture/generated/cpp \ | ||
$fixture/tests/bindings/test_callbacks_regression.ts | ||
*/ | ||
|
||
import generated, { | ||
EventSource, | ||
EventListener, | ||
} from "../../generated/uniffi_callbacks"; | ||
import { asyncTest, AsyncAsserts } from "@/asserts"; | ||
import { console } from "@/hermes"; | ||
|
||
// This is only needed in tests. | ||
generated.initialize(); | ||
|
||
class EventListenerImpl implements EventListener { | ||
constructor( | ||
private t: AsyncAsserts, | ||
private max: number, | ||
) {} | ||
onEvent(message: string, number: number): void { | ||
// console.log("--", message, number); | ||
if (number === this.max - 1) { | ||
console.log("-- Done!", this.max); | ||
this.t.end(); | ||
} | ||
} | ||
} | ||
|
||
async function testToMax(max: number, t: AsyncAsserts) { | ||
const listener = new EventListenerImpl(t, max); | ||
const source = new EventSource(listener); | ||
source.start(`Going to ${max}, now at:`, max); | ||
} | ||
|
||
(async () => { | ||
for (let i = 1; i <= 1024; i *= 2) { | ||
await asyncTest( | ||
`Full tilt test up to ${i}`, | ||
async (t) => await testToMax(i, t), | ||
); | ||
} | ||
})(); |
5 changes: 5 additions & 0 deletions
5
fixtures/callbacks-regression/tests/test_generated_bindings.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
*/ |
Empty file.