Skip to content
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

DO NOT MERGE Tests for async-deadlock fix #89

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ serde = { version = "1", features = ["derive"] }
uniffi = "=0.28.0"
uniffi_bindgen = "=0.28.0"
uniffi_meta = "=0.28.0"
matrix-sdk-ffi = { rev = "8fbf1101c232bcd69583eaaa4605542653213822", git = "https://github.com/vimeiro-co/matrix-rust-sdk-mirror" }
matrix-sdk = { rev = "8fbf1101c232bcd69583eaaa4605542653213822", git = "https://github.com/vimeiro-co/matrix-rust-sdk-mirror" }

[patch.crates-io]
# async-compat = { git = "https://github.com/jplatte/async-compat", rev = "16dc8597ec09a6102d58d4e7b67714a35dd0ecb8" }
# const_panic = { git = "https://github.com/jplatte/const_panic", rev = "9024a4cb3eac45c1d2d980f17aaee287b17be498" }
38 changes: 26 additions & 12 deletions cpp/includes/UniffiCallInvoker.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
*/
#pragma once
#include <ReactCommon/CallInvoker.h>
#include <future>
#include <condition_variable>
#include <jsi/jsi.h>
#include <memory>
#include <mutex>
#include <thread>

namespace uniffi_runtime {
Expand Down Expand Up @@ -57,24 +58,37 @@ class UniffiCallInvoker {
if (std::this_thread::get_id() == threadId_) {
func(rt);
} else {
std::promise<void> promise;
auto future = promise.get_future();
std::mutex mtx;
std::condition_variable cv;
bool done = false;
// The runtime argument was added to CallFunc in
// https://github.com/facebook/react-native/pull/43375
//
// Once that is released, there will be a deprecation period.
//
// Any time during the deprecation period, we can switch `&rt`
// from being a captured variable to being an argument, i.e.
// commenting out one line, and uncommenting the other.
std::function<void()> wrapper = [&func, &promise, &rt]() {
// react::CallFunc wrapper = [&func, &promise](jsi::Runtime &rt) {
// This can be changed once that change is released.
// react::CallFunc wrapper = [&func, &mtx, &cv, &done](jsi::Runtime &rt) {
std::function<void()> wrapper = [&func, &rt, &mtx, &cv, &done]() {
func(rt);
promise.set_value();
{
std::lock_guard<std::mutex> lock(mtx);
done = true;
}
cv.notify_one();
};
callInvoker_->invokeAsync(std::move(wrapper));
future.wait();

std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [&done] { return done; });
}
}

/**
* Invokes the given function on the JS thread, by adding to
* the event queue.
*/
void invokeNonBlocking(jsi::Runtime &rt, UniffiCallFunc func) {
// react::CallFunc wrapper = [func](jsi::Runtime &rt) {
std::function<void()> wrapper = [func, &rt]() { func(rt); };
callInvoker_->invokeAsync(std::move(wrapper));
}
};
} // namespace uniffi_runtime
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ namespace {{ ns }} {
};
// We'll then call that lambda from the callInvoker which will
// look after calling it on the correct thread.
{% if callback.is_blocking() -%}
callInvoker->invokeBlocking(rt, jsLambda);
{%- else %}
callInvoker->invokeNonBlocking(rt, jsLambda);
{%- endif %}
};
return callback;
}
Expand Down
4 changes: 4 additions & 0 deletions crates/ubrn_bindgen/src/bindings/react_native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,10 @@ impl FfiCallbackFunction {
.find(|a| a.is_return() && !a.type_().is_void());
arg.map(|a| a.type_())
}

fn is_blocking(&self) -> bool {
self.name() != "RustFutureContinuationCallback"
}
}

fn is_future(nm: &str) -> bool {
Expand Down
28 changes: 28 additions & 0 deletions fixtures/async-deadlock/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "regression-async-deadlock"
edition = "2021"
version = "0.0.1"
authors = ["Filament Team <team@filament.im>"]
license = "MPL-2.0"
publish = false

[lib]
crate-type = ["lib", "staticlib", "cdylib"]
name = "regression_async_deadlock"

[patch.crates-io]
async-compat = { git = "https://github.com/jplatte/async-compat", rev = "16dc8597ec09a6102d58d4e7b67714a35dd0ecb8" }
const_panic = { git = "https://github.com/jplatte/const_panic", rev = "9024a4cb3eac45c1d2d980f17aaee287b17be498" }

[dependencies]
uniffi = { workspace = true }
matrix-sdk-ffi = { workspace = true }
matrix-sdk = { workspace = true }
thiserror = "1.0"


[build-dependencies]
uniffi = { workspace = true, features = ["build"] }

[dev-dependencies]
uniffi = { workspace = true, features = ["bindgen-tests"] }
16 changes: 16 additions & 0 deletions fixtures/async-deadlock/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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;

use matrix_sdk_ffi::client_builder::ClientBuilder;

#[uniffi::export]
pub fn get_matrix_client_builder() -> Arc<ClientBuilder> {
ClientBuilder::new()
}

uniffi::setup_scaffolding!();
Loading