Skip to content

Commit

Permalink
using defer for releaseing lock
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmer committed Feb 12, 2025
1 parent 003e109 commit f36e14e
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions library/waku_thread/waku_thread.nim
Original file line number Diff line number Diff line change
Expand Up @@ -104,27 +104,25 @@ proc sendRequestToWakuThread*(
# Rearchitecting the signaling + migrating to a MP Channel will allow us to receive
# requests concurrently and spare us the need of locks
ctx.lock.acquire()
defer:
ctx.lock.release()
## Sending the request
let sentOk = ctx.reqChannel.trySend(req)
if not sentOk:
ctx.lock.release()
deallocShared(req)
return err("Couldn't send a request to the waku thread: " & $req[])

let fireSyncRes = ctx.reqSignal.fireSync()
if fireSyncRes.isErr():
ctx.lock.release()
deallocShared(req)
return err("failed fireSync: " & $fireSyncRes.error)

if fireSyncRes.get() == false:
ctx.lock.release()
deallocShared(req)
return err("Couldn't fireSync in time")

## wait until the Waku Thread properly received the request
let res = ctx.reqReceivedSignal.waitSync()
ctx.lock.release()

if res.isErr():
deallocShared(req)
Expand Down

0 comments on commit f36e14e

Please sign in to comment.