@@ -780,20 +780,21 @@ proc push*[T](
780780 # # Push successful result to queue ``sq``.
781781 mixin updateScore, updateStats, getStats
782782
783+ template findPosition (sq, sr: untyped ): SyncPosition =
784+ sq.find (sr).valueOr:
785+ debug " Request is no more relevant" ,
786+ request = sr, sync_ident = sq.ident, topics = " syncman"
787+ # Request is not in queue anymore, probably reset happened.
788+ return
789+
783790 # This is backpressure handling algorithm, this algorithm is blocking
784791 # all pending `push` requests if `request` is not in range.
785792 var
786793 position =
787794 block :
788795 var pos: SyncPosition
789796 while true :
790- pos = sq.find (sr).valueOr:
791- debug " Request is no more relevant" ,
792- request = sr,
793- sync_ident = sq.ident,
794- topics = " syncman"
795- # Request is not in queue anymore, probably reset happened.
796- return
797+ pos = sq.findPosition (sr)
797798
798799 if pos.qindex == 0 :
799800 # Exiting loop when request is first in queue.
@@ -816,20 +817,18 @@ proc push*[T](
816817
817818 await sq.lock.acquire ()
818819 try :
819- block :
820- position = sq.find (sr).valueOr:
821- # Queue has advanced, the request is no longer relevant.
822- debug " Request is no more relevant" ,
823- request = sr,
824- sync_ident = sq.ident,
825- topics = " syncman"
826- return
820+ position = sq.findPosition (sr)
827821
828822 if not (isNil (processingCb)):
829823 processingCb ()
830824
831825 let pres = await sq.process (sr, data, blobs, maybeFinalized)
832826
827+ # We need to update position, because while we waiting for `process()` to
828+ # complete - clearAndWakeup() could be invoked which could clean whole the
829+ # queue (invalidating all the positions).
830+ position = sq.findPosition (sr)
831+
833832 case pres.code
834833 of SyncProcessError .Empty :
835834 # Empty responses does not affect failures count
0 commit comments