Skip to content

Commit

Permalink
Fix: Run lightning-net-tokio on a different runtime
Browse files Browse the repository at this point in the history
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"
  • Loading branch information
mariocynicys committed Feb 5, 2023
1 parent b361508 commit b6889bd
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions teos/src/api/lightning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,19 @@ pub async fn serve(
"Couldn't bind the lightning server to {}",
lightning_bind
));
// A tokio runtime handle to run lightning net tokio on. This is to fix a deadlock issue similar
// to https://github.com/lightningdevkit/rust-lightning/issues/1367 which appears with too many
// concurrent requests to the server.
let ldk_handle = Box::leak(Box::new(tokio::runtime::Runtime::new().unwrap()))
.handle()
.clone();
loop {
let tcp_stream = listener.accept().await.unwrap().0;
if shutdown_signal.is_triggered() {
return;
}
let peer_manager = peer_manager.clone();
tokio::spawn(async move {
ldk_handle.spawn(async move {
lightning_net_tokio::setup_inbound(peer_manager, tcp_stream.into_std().unwrap()).await;
});
}
Expand Down Expand Up @@ -930,8 +936,7 @@ mod peer_manager_tests {

use teos_common::UserId;

// Needs to be "multi_thread" because we "block_in_place" without using "spawn_blocking".
#[tokio::test(flavor = "multi_thread")]
#[tokio::test]
async fn simple_test() {
let (tower_addr, tower_pk, _s) = run_lightning_tower().await;
let (client_peer_manager, client_messenger, client_pk) = get_test_client_peer_manager();
Expand Down

0 comments on commit b6889bd

Please sign in to comment.