Use await keyword as soon as possible to yield to the event event loop

This commit is contained in:
Arnaud 2025-09-12 12:17:27 +02:00
parent 56dc449096
commit 4848e6f600
No known key found for this signature in database
GPG Key ID: B8FBC178F10CA7AE

View File

@ -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()