Skip to content

Commit 1a2840b

Browse files
committed
Fix: Run lightning-net-tokio on a different runtime
This fixes a deadlock issue that appears with too many concurrent requtests. This solution was taken from devrandom's issue: lightningdevkit/rust-lightning #1367 Another solution that would probably work is that lightning net tokio wraps our sync calls with `tokio::task::spawn_blocking(|| sync_call()).await.unwarp()`, but not sure how would this affect performace of other users with no async code requirements. Note that peer_manager_tests now don't need to be anotated with `flavor = "multi_thread"`, that's because the runtime we block_on (inside lightning net tokio) is the artificial runtime created in `api::lightning::server` which is already "multi_thread"
1 parent 2e73d14 commit 1a2840b

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

teos/src/api/lightning.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,19 @@ pub async fn serve(
275275
"Couldn't bind the lightning server to {}",
276276
lightning_bind
277277
));
278+
// A tokio runtime handle to run lightning net tokio on. This is to fix a deadlock issue similar
279+
// to https://github.com/lightningdevkit/rust-lightning/issues/1367 which appears with too many
280+
// concurrent requests to the server.
281+
let ldk_handle = Box::leak(Box::new(tokio::runtime::Runtime::new().unwrap()))
282+
.handle()
283+
.clone();
278284
loop {
279285
let tcp_stream = listener.accept().await.unwrap().0;
280286
if shutdown_signal.is_triggered() {
281287
return;
282288
}
283289
let peer_manager = peer_manager.clone();
284-
tokio::spawn(async move {
290+
ldk_handle.spawn(async move {
285291
lightning_net_tokio::setup_inbound(peer_manager, tcp_stream.into_std().unwrap()).await;
286292
});
287293
}
@@ -930,8 +936,7 @@ mod peer_manager_tests {
930936

931937
use teos_common::UserId;
932938

933-
// Needs to be "multi_thread" because we "block_in_place" without using "spawn_blocking".
934-
#[tokio::test(flavor = "multi_thread")]
939+
#[tokio::test]
935940
async fn simple_test() {
936941
let (tower_addr, tower_pk, _s) = run_lightning_tower().await;
937942
let (client_peer_manager, client_messenger, client_pk) = get_test_client_peer_manager();

0 commit comments

Comments
 (0)