Skip to content

Commit 9871d40

Browse files
committed
f - Use a timeout with block_on to prevent deadlock
Test read_chunked_message_body sometimes hangs trying to read within block_on. AFAICT, the server reads an EOF and drops the stream so never writes anything for the client to read. Using a timeout triggers a retry which seems to resolve the problem.
1 parent 42c02a2 commit 9871d40

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

lightning-block-sync/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ rpc-client = [ "serde_json", "chunked_transfer" ]
1515
[dependencies]
1616
bitcoin = "0.24"
1717
lightning = { version = "0.0.12", path = "../lightning" }
18-
tokio = { version = ">=0.2.12", features = [ "tcp", "io-util", "dns" ], optional = true }
18+
tokio = { version = ">=0.2.12", features = [ "tcp", "io-util", "dns", "time" ], optional = true }
1919
serde_json = { version = "1", optional = true }
2020
chunked_transfer = { version = "1.3.0", optional = true }
2121
futures = { version = "0.3.8" }

lightning-block-sync/src/http.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,11 @@ struct ReadAdapter<'a, R: tokio::io::AsyncRead + std::marker::Unpin>(&'a mut R);
367367
#[cfg(feature = "tokio")]
368368
impl<'a, R: tokio::io::AsyncRead + std::marker::Unpin> std::io::Read for ReadAdapter<'a, R> {
369369
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
370-
futures::executor::block_on(self.0.read(buf))
370+
let read_with_timeout = tokio::time::timeout(Duration::from_secs(2), self.0.read(buf));
371+
match futures::executor::block_on(read_with_timeout) {
372+
Ok(result) => result,
373+
Err(e) => Err(std::io::Error::new(std::io::ErrorKind::TimedOut, e)),
374+
}
371375
}
372376
}
373377

0 commit comments

Comments
 (0)