Skip to content

Commit

Permalink
Fixed race-condition in closing of DataSync subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Jul 19, 2022
1 parent 53df1bf commit 9fcb428
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions IHP/DataSync/ControllerImpl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ runDataSyncController ensureRLSEnabled installTableChangeTriggers receiveData se

subscriptionId <- UUID.nextRandom

-- Allocate the close handle as early as possible
-- to make DeleteDataSubscription calls succeed even when the DataSubscription is
-- not fully set up yet
close <- MVar.newEmptyMVar
modifyIORef' ?state (\state -> state |> modify #subscriptions (HashMap.insert subscriptionId close))

let (theQuery, theParams) = compileQuery query

result :: [[Field]] <- sqlQueryWithRLS theQuery theParams
Expand Down Expand Up @@ -132,9 +138,6 @@ runDataSyncController ensureRLSEnabled installTableChangeTriggers receiveData se
let unsubscribe subscription = PGListener.unsubscribe subscription pgListener
Exception.bracket subscribe unsubscribe \channelSubscription -> do
close <- MVar.newEmptyMVar
modifyIORef' ?state (\state -> state |> modify #subscriptions (HashMap.insert subscriptionId close))
sendJSON DidCreateDataSubscription { subscriptionId, requestId, result }
MVar.takeMVar close
Expand Down

0 comments on commit 9fcb428

Please sign in to comment.