diff --git a/library/codex_thread_requests/requests/node_lifecycle_request.nim b/library/codex_thread_requests/requests/node_lifecycle_request.nim index 5f5efbaf..0e462aa0 100644 --- a/library/codex_thread_requests/requests/node_lifecycle_request.nim +++ b/library/codex_thread_requests/requests/node_lifecycle_request.nim @@ -31,7 +31,7 @@ type NodeLifecycleMsgType* = enum CREATE_NODE START_NODE STOP_NODE - DESTROY_NODE + CLOSE_NODE proc readValue*[T: InputFile | InputDir | OutPath | OutDir | OutFile]( r: var JsonReader, val: var T @@ -82,18 +82,13 @@ proc readValue*(r: var JsonReader, val: var EthAddress) = type NodeLifecycleRequest* = object operation: NodeLifecycleMsgType configJson: cstring - onDestroy: proc() {.gcsafe.} proc createShared*( - T: type NodeLifecycleRequest, - op: NodeLifecycleMsgType, - configJson: cstring = "", - onDestroy: proc() {.gcsafe.} = nil, + T: type NodeLifecycleRequest, op: NodeLifecycleMsgType, configJson: cstring = "" ): ptr type T = var ret = createShared(T) ret[].operation = op ret[].configJson = configJson.alloc() - ret[].onDestroy = onDestroy return ret proc destroyShared(self: ptr NodeLifecycleRequest) = @@ -184,10 +179,9 @@ proc process*( except Exception as e: error "Failed to STOP_NODE.", error = e.msg return err(e.msg) - of DESTROY_NODE: + of CLOSE_NODE: try: await codex[].close() - self.onDestroy() except Exception as e: error "Failed to STOP_NODE.", error = e.msg return err(e.msg) diff --git a/library/libcodex.h b/library/libcodex.h index fa91f67a..e63c2ad1 100644 --- a/library/libcodex.h +++ b/library/libcodex.h @@ -180,6 +180,10 @@ int codex_stop(void* ctx, CodexCallback callback, void* userData); +int codex_close(void* ctx, + CodexCallback callback, + void* userData); + // Destroys an instance of a codex node created with codex_new int codex_destroy(void* ctx, CodexCallback callback, diff --git a/library/libcodex.nim b/library/libcodex.nim index da17e81c..839137ba 100644 --- a/library/libcodex.nim +++ b/library/libcodex.nim @@ -246,30 +246,28 @@ proc codex_peer_debug( return callback.okOrError(res, userData) -proc codex_destroy( +proc codex_close( ctx: ptr CodexContext, callback: CodexCallback, userData: pointer ): cint {.dynlib, exportc.} = initializeLibrary() checkLibcodexParams(ctx, callback, userData) - let destroySignal = ThreadSignalPtr.new().valueOr: - return callback.error("failed to create destroy signal", userData) - - proc onDestroy() {.gcsafe.} = - discard destroySignal.fireSync() - - let reqContent = NodeLifecycleRequest.createShared( - NodeLifecycleMsgType.DESTROY_NODE, onDestroy = onDestroy - ) + let reqContent = NodeLifecycleRequest.createShared(NodeLifecycleMsgType.CLOSE_NODE) var res = codex_context.sendRequestToCodexThread( ctx, RequestType.LIFECYCLE, reqContent, callback, userData ) if res.isErr: return callback.error(res.error, userData) - discard destroySignal.waitSync() + return callback.okOrError(res, userData) - res = codex_context.destroyCodexContext(ctx) +proc codex_destroy( + ctx: ptr CodexContext, callback: CodexCallback, userData: pointer +): cint {.dynlib, exportc.} = + initializeLibrary() + checkLibcodexParams(ctx, callback, userData) + + let res = codex_context.destroyCodexContext(ctx) if res.isErr: return callback.error(res.error, userData)