-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixup GC'ing objects with callbacks intermittent crasher #208
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,22 @@ | ||
[package] | ||
name = "gc-callbacks-crasher" | ||
edition = "2021" | ||
version = "0.22.0" | ||
license = "MPL-2.0" | ||
publish = false | ||
|
||
|
||
[lib] | ||
crate-type = ["cdylib", "staticlib"] | ||
|
||
[dependencies] | ||
async-std = "1.12.0" | ||
async-trait = "0.1.83" | ||
thiserror = "1.0" | ||
uniffi = { workspace = true, features = ["tokio"] } | ||
|
||
[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,79 @@ | ||
/* | ||
* 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; | ||
|
||
#[uniffi::export(callback_interface)] | ||
#[async_trait::async_trait] | ||
pub trait AsyncDelegate: Sync + Send { | ||
async fn method(&self, user_id: String) -> String; | ||
} | ||
|
||
#[uniffi::export(callback_interface)] | ||
pub trait BasicDelegate: Sync + Send { | ||
fn method(&self, user_id: String) -> String; | ||
} | ||
|
||
#[derive(Clone, uniffi::Object)] | ||
pub struct Builder { | ||
async_delegate: Option<Arc<dyn AsyncDelegate>>, | ||
basic_delegate: Option<Arc<dyn BasicDelegate>>, | ||
} | ||
|
||
pub(crate) fn unwrap_or_clone_arc<T: Clone>(arc: Arc<T>) -> T { | ||
Arc::try_unwrap(arc).unwrap_or_else(|x| (*x).clone()) | ||
} | ||
|
||
#[uniffi::export] | ||
impl Builder { | ||
pub fn echo(self: Arc<Self>) -> String { | ||
"echo".to_string() | ||
} | ||
pub fn set_async_delegate(self: Arc<Self>, delegate: Box<dyn AsyncDelegate>) -> Arc<Self> { | ||
let mut builder = unwrap_or_clone_arc(self); | ||
builder.async_delegate = Some(delegate.into()); | ||
Arc::new(builder) | ||
} | ||
pub fn set_basic_delegate(self: Arc<Self>, delegate: Box<dyn BasicDelegate>) -> Arc<Self> { | ||
let mut builder = unwrap_or_clone_arc(self); | ||
builder.basic_delegate = Some(delegate.into()); | ||
Arc::new(builder) | ||
} | ||
} | ||
|
||
impl Builder { | ||
pub fn set_basic_delegate_simplistic(&mut self, delegate: Box<dyn BasicDelegate>) { | ||
self.basic_delegate = Some(delegate.into()); | ||
} | ||
} | ||
|
||
#[uniffi::export] | ||
pub fn get_builder() -> Arc<Builder> { | ||
Arc::new(new_builder()) | ||
} | ||
|
||
pub fn new_builder() -> Builder { | ||
Builder { | ||
async_delegate: None, | ||
basic_delegate: None, | ||
} | ||
} | ||
|
||
#[uniffi::export] | ||
pub fn create_arc_dropping_builder(delegate: Box<dyn BasicDelegate>) { | ||
let builder = get_builder(); | ||
builder.set_basic_delegate(delegate); | ||
} | ||
|
||
// Not crashing | ||
#[uniffi::export] | ||
pub fn create_dropping_builder(delegate: Box<dyn BasicDelegate>) { | ||
let mut builder = new_builder(); | ||
builder.set_basic_delegate_simplistic(delegate); | ||
drop(builder); | ||
} | ||
|
||
uniffi::setup_scaffolding!(); |
101 changes: 101 additions & 0 deletions
101
fixtures/gc-callbacks-crasher/tests/bindings/test_gc_callbacks_crasher.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,101 @@ | ||
/* | ||
* 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/ | ||
*/ | ||
import { asyncTest } from "@/asserts"; | ||
import theModule, { | ||
type BasicDelegate, | ||
Builder, | ||
createArcDroppingBuilder, | ||
createDroppingBuilder, | ||
getBuilder, | ||
} from "../../generated/gc_callbacks_crasher"; | ||
|
||
theModule.initialize(); | ||
|
||
function delayPromise(delayMs: number): Promise<void> { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, delayMs); | ||
}); | ||
} | ||
|
||
const basicDelegate: BasicDelegate = { | ||
method: (userId: string) => { | ||
return "foo"; | ||
}, | ||
}; | ||
|
||
(async () => { | ||
await asyncTest("using no callback", async (t) => { | ||
const makeClientBuilder = async () => { | ||
try { | ||
const builder = getBuilder(); | ||
(builder as Builder).uniffiDestroy(); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
for (let i = 0; i < 1000; i++) { | ||
await makeClientBuilder(); | ||
} | ||
await delayPromise(1000); | ||
t.end(); | ||
}); | ||
await asyncTest("using uniffiDestroy", async (t) => { | ||
const makeClientBuilder = async () => { | ||
try { | ||
const builder = getBuilder(); | ||
builder.setBasicDelegate(basicDelegate); | ||
(builder as Builder).uniffiDestroy(); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
for (let i = 0; i < 1000; i++) { | ||
await makeClientBuilder(); | ||
} | ||
await delayPromise(1000); | ||
t.end(); | ||
}); | ||
|
||
await asyncTest("Dropping in Rust", async (t) => { | ||
try { | ||
for (let i = 0; i < 1000; i++) { | ||
createDroppingBuilder(basicDelegate); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
await delayPromise(1000); | ||
t.end(); | ||
}); | ||
|
||
await asyncTest("Dropping Arc in Rust", async (t) => { | ||
try { | ||
for (let i = 0; i < 1000; i++) { | ||
createArcDroppingBuilder(basicDelegate); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
await delayPromise(1000); | ||
t.end(); | ||
}); | ||
|
||
await asyncTest("using C++ destructors", async (t) => { | ||
const makeClientBuilder = async () => { | ||
try { | ||
const builder = getBuilder(); | ||
builder.setBasicDelegate(basicDelegate); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
for (let i = 0; i < 1000; i++) { | ||
await makeClientBuilder(); | ||
} | ||
await delayPromise(1000); | ||
t.end(); | ||
}); | ||
})(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As suspected, it's a one line fix.