Skip to content

Commit

Permalink
dbconn: better protection against possible double future ending and s…
Browse files Browse the repository at this point in the history
…implify
  • Loading branch information
Ivansete-status committed Sep 30, 2024
1 parent 500ca09 commit a13c68f
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions waku/common/databases/db_postgres/dbconn.nim
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,11 @@ proc waitQueryToFinish(
## The 'rowCallback' param is != nil when the underlying query wants to retrieve results (SELECT.)
## For other queries, like "INSERT", 'rowCallback' should be nil.

var triggered = false ## to control the "data available" signal is only triggered once

let futDataAvailable = newFuture[void]("futDataAvailable")

proc onDataAvailable(udata: pointer) {.gcsafe, raises: [].} =
if not triggered:
if not futDataAvailable.completed():
futDataAvailable.complete()
triggered = true

let asyncFd = cast[asyncengine.AsyncFD](pqsocket(dbConnWrapper.dbConn))

Expand All @@ -198,7 +195,8 @@ proc waitQueryToFinish(

if pqResult == nil:
dbConnWrapper.dbConn.check().isOkOr:
dbConnWrapper.futBecomeFree.fail(newException(ValueError, $error))
if not dbConnWrapper.futBecomeFree.failed():
dbConnWrapper.futBecomeFree.fail(newException(ValueError, $error))
return err("error in query: " & $error)

dbConnWrapper.futBecomeFree.complete()
Expand Down

0 comments on commit a13c68f

Please sign in to comment.