From 7fa4dc4c15fb851c5323b941b81048af83ac8e42 Mon Sep 17 00:00:00 2001 From: zvolin Date: Fri, 18 Oct 2024 15:21:52 +0200 Subject: [PATCH] fix(node-wasm): workaround to clean extra message from worker --- node-wasm/src/client.rs | 4 ++++ node-wasm/src/ports.rs | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/node-wasm/src/client.rs b/node-wasm/src/client.rs index 6b7070b6..d4bad090 100644 --- a/node-wasm/src/client.rs +++ b/node-wasm/src/client.rs @@ -58,6 +58,7 @@ impl NodeClient { let worker = WorkerClient::new(port)?; + // todo: refactor when oneshots are implemented // keep pinging worker until it responds. // NOTE: there is a possibility that worker can take longer than a timeout // to send his response. Client will then send another ping and read previous @@ -71,6 +72,9 @@ impl NodeClient { break; } } + // wait another timeout to read potential extra response from worker that + // could be caused by the loop above + let _ = timeout(200, worker.recv()).await; debug!("Connected to worker"); diff --git a/node-wasm/src/ports.rs b/node-wasm/src/ports.rs index a8a77dd1..7942b679 100644 --- a/node-wasm/src/ports.rs +++ b/node-wasm/src/ports.rs @@ -219,6 +219,18 @@ impl WorkerClient { Ok(worker_response) } + + // todo: remove when oneshots are implemented, used in workaround for initial worker connection + pub(crate) async fn recv(&self) -> Result { + let mut response_channel = self.response_channel.lock().await; + let worker_response = response_channel + .recv() + .await + .expect("response channel should never drop") + .context("error receiving command")?; + + Ok(worker_response) + } } impl Drop for WorkerClient {