From 1deed7deeb1012f9f2040ac302258e1705f06c16 Mon Sep 17 00:00:00 2001 From: Serhii Tatarintsev Date: Mon, 15 Jan 2024 11:33:21 +0100 Subject: [PATCH] qe: Fix timeout errors (#4641) * qe: Fix timeout errors Problem: we awaited spawned future immediately, which kind of makes spawn useless, effecctively serializing batch requests instead of executing them in parallel. Affects only the batches that could not be compated into a single query. I struggled to reproduce the error in engine's test suite. I suggest we test it in client instead. Fix prisma/prisma#22610 * Fix wasm * Use spawn_local for WASM branch * Address more review comments --- libs/crosstarget-utils/src/native/spawn.rs | 5 +++-- libs/crosstarget-utils/src/wasm/spawn.rs | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/libs/crosstarget-utils/src/native/spawn.rs b/libs/crosstarget-utils/src/native/spawn.rs index cd1d5246d123..70e4c3708f22 100644 --- a/libs/crosstarget-utils/src/native/spawn.rs +++ b/libs/crosstarget-utils/src/native/spawn.rs @@ -1,11 +1,12 @@ +use futures::TryFutureExt; use std::future::Future; use crate::common::SpawnError; -pub async fn spawn_if_possible(future: F) -> Result +pub fn spawn_if_possible(future: F) -> impl Future> where F: Future + 'static + Send, F::Output: Send + 'static, { - tokio::spawn(future).await.map_err(|_| SpawnError) + tokio::spawn(future).map_err(|_| SpawnError) } diff --git a/libs/crosstarget-utils/src/wasm/spawn.rs b/libs/crosstarget-utils/src/wasm/spawn.rs index 33ed1d21b3b7..e27104c3b941 100644 --- a/libs/crosstarget-utils/src/wasm/spawn.rs +++ b/libs/crosstarget-utils/src/wasm/spawn.rs @@ -1,10 +1,20 @@ use std::future::Future; +use futures::TryFutureExt; +use tokio::sync::oneshot; +use wasm_bindgen_futures::spawn_local; + use crate::common::SpawnError; -pub async fn spawn_if_possible(future: F) -> Result +pub fn spawn_if_possible(future: F) -> impl Future> where F: Future + 'static, { - Ok(future.await) + let (sx, rx) = oneshot::channel(); + spawn_local(async move { + let result = future.await; + _ = sx.send(result); + }); + + rx.map_err(|_| SpawnError) }