From acf7ef3d61dddc502c01902667bb4afb7575b8cb Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 1 Mar 2022 18:02:12 -0800 Subject: [PATCH] Avoid retaining executor when using Connection::in_memory --- crates/rpc/src/conn.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/crates/rpc/src/conn.rs b/crates/rpc/src/conn.rs index 70c59d013b43d8bbeb9bce93c6a10ba59ee9139b..fb91b72d9f3898ec42b35b0387dc9f27951fc8f8 100644 --- a/crates/rpc/src/conn.rs +++ b/crates/rpc/src/conn.rs @@ -59,18 +59,21 @@ impl Connection { ) { use futures::channel::mpsc; use io::{Error, ErrorKind}; + use std::sync::Arc; let (tx, rx) = mpsc::unbounded::(); let tx = tx .sink_map_err(|e| WebSocketError::from(Error::new(ErrorKind::Other, e))) .with({ + let executor = Arc::downgrade(&executor); let kill_rx = kill_rx.clone(); - let executor = executor.clone(); move |msg| { let kill_rx = kill_rx.clone(); let executor = executor.clone(); Box::pin(async move { - executor.simulate_random_delay().await; + if let Some(executor) = executor.upgrade() { + executor.simulate_random_delay().await; + } if kill_rx.borrow().is_none() { Ok(msg) } else { @@ -80,9 +83,11 @@ impl Connection { } }); let rx = rx.then(move |msg| { - let executor = executor.clone(); + let executor = Arc::downgrade(&executor); Box::pin(async move { - executor.simulate_random_delay().await; + if let Some(executor) = executor.upgrade() { + executor.simulate_random_delay().await; + } msg }) });