diff --git a/library/storage_context.nim b/library/storage_context.nim index df352bdf..3f412ab3 100644 --- a/library/storage_context.nim +++ b/library/storage_context.nim @@ -95,7 +95,7 @@ proc sendRequestToStorageThread*( let sentOk = ctx.reqChannel.trySend(req) if not sentOk: let reqDesc = $req[] - deallocShared(req) + req.destroy() return err("Failed to send request to the Logos Storage thread: " & reqDesc) # trySend has succeeded: req is published in the channel and diff --git a/library/storage_thread_requests/requests/node_debug_request.nim b/library/storage_thread_requests/requests/node_debug_request.nim index 8bf3106c..6c8f95aa 100644 --- a/library/storage_thread_requests/requests/node_debug_request.nim +++ b/library/storage_thread_requests/requests/node_debug_request.nim @@ -41,7 +41,7 @@ proc createShared*( ret[].logLevel = logLevel.alloc() return ret -proc destroyShared(self: ptr NodeDebugRequest) = +proc destroyShared*(self: ptr NodeDebugRequest) = deallocShared(self[].peerId) deallocShared(self[].logLevel) deallocShared(self) diff --git a/library/storage_thread_requests/requests/node_download_request.nim b/library/storage_thread_requests/requests/node_download_request.nim index f0297bbe..3f1938f5 100644 --- a/library/storage_thread_requests/requests/node_download_request.nim +++ b/library/storage_thread_requests/requests/node_download_request.nim @@ -72,7 +72,7 @@ proc createShared*( return ret -proc destroyShared(self: ptr NodeDownloadRequest) = +proc destroyShared*(self: ptr NodeDownloadRequest) = deallocShared(self[].cid) deallocShared(self[].filepath) deallocShared(self) diff --git a/library/storage_thread_requests/requests/node_info_request.nim b/library/storage_thread_requests/requests/node_info_request.nim index 7e755a3a..a07ffc32 100644 --- a/library/storage_thread_requests/requests/node_info_request.nim +++ b/library/storage_thread_requests/requests/node_info_request.nim @@ -27,7 +27,7 @@ proc createShared*(T: type NodeInfoRequest, op: NodeInfoMsgType): ptr type T = ret[].operation = op return ret -proc destroyShared(self: ptr NodeInfoRequest) = +proc destroyShared*(self: ptr NodeInfoRequest) = deallocShared(self) proc getRepo( diff --git a/library/storage_thread_requests/requests/node_lifecycle_request.nim b/library/storage_thread_requests/requests/node_lifecycle_request.nim index cccfd3fb..0f0125ac 100644 --- a/library/storage_thread_requests/requests/node_lifecycle_request.nim +++ b/library/storage_thread_requests/requests/node_lifecycle_request.nim @@ -90,7 +90,7 @@ proc createShared*( ret[].configJson = configJson.alloc() return ret -proc destroyShared(self: ptr NodeLifecycleRequest) = +proc destroyShared*(self: ptr NodeLifecycleRequest) = deallocShared(self[].configJson) deallocShared(self) diff --git a/library/storage_thread_requests/requests/node_p2p_request.nim b/library/storage_thread_requests/requests/node_p2p_request.nim index bfc29b88..ef9737bb 100644 --- a/library/storage_thread_requests/requests/node_p2p_request.nim +++ b/library/storage_thread_requests/requests/node_p2p_request.nim @@ -35,7 +35,7 @@ proc createShared*( ret[].peerAddresses = peerAddresses return ret -proc destroyShared(self: ptr NodeP2PRequest) = +proc destroyShared*(self: ptr NodeP2PRequest) = deallocShared(self[].peerId) deallocShared(self) diff --git a/library/storage_thread_requests/requests/node_storage_request.nim b/library/storage_thread_requests/requests/node_storage_request.nim index b2343041..8c44f172 100644 --- a/library/storage_thread_requests/requests/node_storage_request.nim +++ b/library/storage_thread_requests/requests/node_storage_request.nim @@ -50,7 +50,7 @@ proc createShared*( return ret -proc destroyShared(self: ptr NodeStorageRequest) = +proc destroyShared*(self: ptr NodeStorageRequest) = deallocShared(self[].cid) deallocShared(self) diff --git a/library/storage_thread_requests/requests/node_upload_request.nim b/library/storage_thread_requests/requests/node_upload_request.nim index 064439b2..a10ada32 100644 --- a/library/storage_thread_requests/requests/node_upload_request.nim +++ b/library/storage_thread_requests/requests/node_upload_request.nim @@ -80,7 +80,7 @@ proc createShared*( return ret -proc destroyShared(self: ptr NodeUploadRequest) = +proc destroyShared*(self: ptr NodeUploadRequest) = deallocShared(self[].filepath) deallocShared(self[].sessionId) deallocShared(self) diff --git a/library/storage_thread_requests/storage_thread_request.nim b/library/storage_thread_requests/storage_thread_request.nim index eaa0bba4..a262356e 100644 --- a/library/storage_thread_requests/storage_thread_request.nim +++ b/library/storage_thread_requests/storage_thread_request.nim @@ -52,6 +52,27 @@ proc createShared*( ret[].userData = userData return ret +proc destroy*(request: ptr StorageThreadRequest) = + ## Frees the payload (reqContent) and the wrapper on error paths, + ## when the request is not handed to the storage thread. + ## On success paths, the storage thread frees the payload and the wrapper. + case request[].reqType + of LIFECYCLE: + cast[ptr NodeLifecycleRequest](request[].reqContent).destroyShared() + of INFO: + cast[ptr NodeInfoRequest](request[].reqContent).destroyShared() + of RequestType.DEBUG: + cast[ptr NodeDebugRequest](request[].reqContent).destroyShared() + of P2P: + cast[ptr NodeP2PRequest](request[].reqContent).destroyShared() + of UPLOAD: + cast[ptr NodeUploadRequest](request[].reqContent).destroyShared() + of DOWNLOAD: + cast[ptr NodeDownloadRequest](request[].reqContent).destroyShared() + of STORAGE: + cast[ptr NodeStorageRequest](request[].reqContent).destroyShared() + deallocShared(request) + # NOTE: User callbacks are executed on the working thread. # They must be fast and non-blocking; otherwise this thread will be blocked # and no further requests can be processed.