From 4848e6f600bb7031c46ed43f6d43eb038fcc0ca9 Mon Sep 17 00:00:00 2001 From: Arnaud Date: Fri, 12 Sep 2025 12:17:27 +0200 Subject: [PATCH] Use await keyword as soon as possible to yield to the event event loop --- library/codex_context.nim | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/library/codex_context.nim b/library/codex_context.nim index c0451e9b..fb69aad5 100644 --- a/library/codex_context.nim +++ b/library/codex_context.nim @@ -137,8 +137,14 @@ proc runCodex(ctx: ptr CodexContext) {.async: (raises: []).} = error "codex thread could not receive a request" continue - # Dispatch the request to be processed asynchronously - asyncSpawn CodexThreadRequest.process(request, addr codex) + # yield immediately to the event loop + # with asyncSpawn only, the code will be executed + # synchronously until the first await + asyncSpawn ( + proc() {.async.} = + await sleepAsync(0) + await CodexThreadRequest.process(request, addr codex) + )() # Notify the main thread that we picked up the request let fireRes = ctx.reqReceivedSignal.fireSync()