Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
fix(providerquerymanager): confirm cancel via channel
Browse files Browse the repository at this point in the history
instead of reading from a closed channel to prevent blocks after cancel, wait for a confirm message
on cancel
  • Loading branch information
hannahhoward committed Feb 4, 2019
1 parent 92717db commit 23f9b87
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions providerquerymanager/providerquerymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type newProvideQueryMessage struct {
}

type cancelRequestMessage struct {
cancelConfirm chan<- struct{}
incomingProviders chan peer.ID
k cid.Cid
}
Expand Down Expand Up @@ -159,12 +160,15 @@ func (pqm *ProviderQueryManager) receiveProviders(sessionCtx context.Context, k
for len(receivedProviders) > 0 || incomingProviders != nil {
select {
case <-sessionCtx.Done():
cancelConfirm := make(chan struct{})
pqm.providerQueryMessages <- &cancelRequestMessage{
cancelConfirm: cancelConfirm,
incomingProviders: incomingProviders,
k: k,
}
// clear out any remaining providers
for range incomingProviders {
select {
case <-pqm.ctx.Done():
case <-cancelConfirm:
}
return
case provider, ok := <-incomingProviders:
Expand Down Expand Up @@ -364,13 +368,17 @@ func (crm *cancelRequestMessage) handle(pqm *ProviderQueryManager) {
requestStatus, ok := pqm.inProgressRequestStatuses[crm.k]
if !ok {
log.Errorf("Attempt to cancel request for cid (%s) not in progress", crm.k.String())
return
} else {
_, ok = requestStatus.listeners[crm.incomingProviders]
if !ok {
log.Errorf("Attempt to cancel request for for cid (%s) this is not a listener", crm.k.String())
} else {
delete(requestStatus.listeners, crm.incomingProviders)
}
close(crm.incomingProviders)
}
listener := crm.incomingProviders
if !ok {
log.Errorf("Attempt to cancel request for for cid (%s) this is not a listener", crm.k.String())
return
select {
case <-pqm.ctx.Done():
case crm.cancelConfirm <- struct{}{}:
}
close(listener)
delete(requestStatus.listeners, listener)
}

0 comments on commit 23f9b87

Please sign in to comment.