Avoid raising errors in the spawn threads

This commit is contained in:
Arnaud 2025-09-11 12:30:54 +02:00 committed by Eric
parent 1a499a7858
commit 64a4da3549
No known key found for this signature in database
4 changed files with 29 additions and 19 deletions

View File

@ -1,4 +1,3 @@
{.push raises: [].}
import std/times

View File

@ -65,7 +65,7 @@ template callEventCallback(ctx: ptr CodexContext, eventName: string, body: untyp
cast[CodexCallback](ctx[].eventCallback)(
RET_OK, unsafeAddr event[0], cast[csize_t](len(event)), ctx[].eventUserData
)
except Exception, CatchableError:
except CatchableError:
let msg =
"Exception " & eventName & " when calling 'eventCallBack': " &
getCurrentExceptionMsg()
@ -114,12 +114,16 @@ proc sendRequestToCodexThread*(
## process proc. See the 'codex_thread_request.nim' module for more details.
ok()
proc runCodex(ctx: ptr CodexContext) {.async.} =
proc runCodex(ctx: ptr CodexContext) {.async: (raises: []).} =
var codex: CodexServer
while true:
# Wait until a request is available
await ctx.reqSignal.wait()
try:
# Wait until a request is available
await ctx.reqSignal.wait()
except Exception as e:
error "codex thread error while waiting for reqSignal", error = e.msg
continue
# If codex_destroy was called, exit the loop
if ctx.running.load == false:

View File

@ -67,7 +67,7 @@ proc handleRes[T: string | void](
proc process*(
T: type CodexThreadRequest, request: ptr CodexThreadRequest, codex: ptr CodexServer
) {.async.} =
) {.async: (raises: []).} =
## Processes the request in the Codex thread.
## Dispatch to the appropriate request handler based on reqType.
let retFut =

View File

@ -87,18 +87,25 @@ proc destroyShared(self: ptr NodeLifecycleRequest) =
deallocShared(self[].configJson)
deallocShared(self)
proc createCodex(configJson: cstring): Future[Result[CodexServer, string]] {.async.} =
var conf = CodexConf.load(
version = codexFullVersion,
envVarsPrefix = "codex",
cmdLine = @[],
secondarySources = proc(
config: CodexConf, sources: auto
) {.gcsafe, raises: [ConfigurationError].} =
if configJson.len > 0:
sources.addConfigFileContent(Json, $(configJson))
,
)
proc createCodex(
configJson: cstring
): Future[Result[CodexServer, string]] {.async: (raises: []).} =
var conf: CodexConf
try:
conf = CodexConf.load(
version = codexFullVersion,
envVarsPrefix = "codex",
cmdLine = @[],
secondarySources = proc(
config: CodexConf, sources: auto
) {.gcsafe, raises: [ConfigurationError].} =
if configJson.len > 0:
sources.addConfigFileContent(Json, $(configJson))
,
)
except ConfigurationError as e:
return err("Failed to load configuration: " & e.msg)
conf.setupLogging()
conf.setupMetrics()
@ -136,7 +143,7 @@ proc createCodex(configJson: cstring): Future[Result[CodexServer, string]] {.asy
proc process*(
self: ptr NodeLifecycleRequest, codex: ptr CodexServer
): Future[Result[string, string]] {.async.} =
): Future[Result[string, string]] {.async: (raises: []).} =
defer:
destroyShared(self)