-
Notifications
You must be signed in to change notification settings - Fork 924
fix: fix Listen/Unlisten race on Pubsub #15315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ import ( | |
"sync/atomic" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/lib/pq" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"golang.org/x/xerrors" | ||
|
@@ -188,6 +187,19 @@ func (l pqListenerShim) NotifyChan() <-chan *pq.Notification { | |
return l.Notify | ||
} | ||
|
||
type queueSet struct { | ||
m map[*msgQueue]struct{} | ||
// unlistenInProgress will be non-nil if another goroutine is unlistening for the event this | ||
// queueSet corresponds to. If non-nil, that goroutine will close the channel when it is done. | ||
unlistenInProgress chan struct{} | ||
} | ||
|
||
func newQueueSet() *queueSet { | ||
return &queueSet{ | ||
m: make(map[*msgQueue]struct{}), | ||
} | ||
} | ||
|
||
// PGPubsub is a pubsub implementation using PostgreSQL. | ||
type PGPubsub struct { | ||
logger slog.Logger | ||
|
@@ -196,7 +208,7 @@ type PGPubsub struct { | |
db *sql.DB | ||
|
||
qMu sync.Mutex | ||
queues map[string]map[uuid.UUID]*msgQueue | ||
queues map[string]*queueSet | ||
|
||
// making the close state its own mutex domain simplifies closing logic so | ||
// that we don't have to hold the qMu --- which could block processing | ||
|
@@ -243,6 +255,48 @@ func (p *PGPubsub) subscribeQueue(event string, newQ *msgQueue) (cancel func(), | |
} | ||
}() | ||
|
||
var ( | ||
unlistenInProgress <-chan struct{} | ||
// MUST hold the p.qMu lock to manipulate this! | ||
qs *queueSet | ||
) | ||
func() { | ||
p.qMu.Lock() | ||
defer p.qMu.Unlock() | ||
|
||
var ok bool | ||
if qs, ok = p.queues[event]; !ok { | ||
qs = newQueueSet() | ||
p.queues[event] = qs | ||
} | ||
qs.m[newQ] = struct{}{} | ||
unlistenInProgress = qs.unlistenInProgress | ||
}() | ||
// NOTE there cannot be any `return` statements between here and the next +-+, otherwise the | ||
// assumptions the defer makes could be violated | ||
if unlistenInProgress != nil { | ||
// We have to wait here because we don't want our `Listen` call to happen before the other | ||
// goroutine calls `Unlisten`. That would result in this subscription not getting any | ||
// events. c.f. https://github.com/coder/coder/issues/15312 | ||
p.logger.Debug(context.Background(), "waiting for Unlisten in progress", slog.F("event", event)) | ||
<-unlistenInProgress | ||
p.logger.Debug(context.Background(), "unlistening complete", slog.F("event", event)) | ||
} | ||
// +-+ (see above) | ||
defer func() { | ||
if err != nil { | ||
p.qMu.Lock() | ||
defer p.qMu.Unlock() | ||
delete(qs.m, newQ) | ||
if len(qs.m) == 0 { | ||
// we know that newQ was in the queueSet since we last unlocked, so there cannot | ||
// have been any _new_ goroutines trying to Unlisten(). Therefore, if the queueSet | ||
// is now empty, it's safe to delete. | ||
delete(p.queues, event) | ||
} | ||
} | ||
}() | ||
|
||
// The pgListener waits for the response to `LISTEN` on a mainloop that also dispatches | ||
// notifies. We need to avoid holding the mutex while this happens, since holding the mutex | ||
// blocks reading notifications and can deadlock the pgListener. | ||
|
@@ -258,32 +312,40 @@ func (p *PGPubsub) subscribeQueue(event string, newQ *msgQueue) (cancel func(), | |
if err != nil { | ||
return nil, xerrors.Errorf("listen: %w", err) | ||
} | ||
p.qMu.Lock() | ||
defer p.qMu.Unlock() | ||
|
||
var eventQs map[uuid.UUID]*msgQueue | ||
var ok bool | ||
if eventQs, ok = p.queues[event]; !ok { | ||
eventQs = make(map[uuid.UUID]*msgQueue) | ||
p.queues[event] = eventQs | ||
} | ||
id := uuid.New() | ||
eventQs[id] = newQ | ||
return func() { | ||
p.qMu.Lock() | ||
listeners := p.queues[event] | ||
q := listeners[id] | ||
q.close() | ||
delete(listeners, id) | ||
if len(listeners) == 0 { | ||
delete(p.queues, event) | ||
} | ||
listenerCount := len(listeners) | ||
p.qMu.Unlock() | ||
// as above, we must not hold the lock while calling into pgListener | ||
var unlistening chan struct{} | ||
func() { | ||
p.qMu.Lock() | ||
defer p.qMu.Unlock() | ||
newQ.close() | ||
qSet, ok := p.queues[event] | ||
if !ok { | ||
p.logger.Critical(context.Background(), "event was removed before cancel", slog.F("event", event)) | ||
return | ||
} | ||
delete(qSet.m, newQ) | ||
if len(qSet.m) == 0 { | ||
unlistening = make(chan struct{}) | ||
qSet.unlistenInProgress = unlistening | ||
} | ||
}() | ||
|
||
if listenerCount == 0 { | ||
// as above, we must not hold the lock while calling into pgListener | ||
if unlistening != nil { | ||
uErr := p.pgListener.Unlisten(event) | ||
close(unlistening) | ||
// we can now delete the queueSet if it is empty. | ||
func() { | ||
p.qMu.Lock() | ||
defer p.qMu.Unlock() | ||
qSet, ok := p.queues[event] | ||
if ok && len(qSet.m) == 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we wanted to be really careful here we could check that the channel on qSet is still ours. It'll work without the check though, just the delete could (very) theoretically be done by another routine, which would still only be a semantic difference. |
||
p.logger.Debug(context.Background(), "removing queueSet", slog.F("event", event)) | ||
delete(p.queues, event) | ||
} | ||
}() | ||
|
||
p.closeMu.Lock() | ||
defer p.closeMu.Unlock() | ||
if uErr != nil && !p.closedListener { | ||
|
@@ -361,21 +423,21 @@ func (p *PGPubsub) listenReceive(notif *pq.Notification) { | |
|
||
p.qMu.Lock() | ||
defer p.qMu.Unlock() | ||
queues, ok := p.queues[notif.Channel] | ||
qSet, ok := p.queues[notif.Channel] | ||
if !ok { | ||
return | ||
} | ||
extra := []byte(notif.Extra) | ||
for _, q := range queues { | ||
for q := range qSet.m { | ||
q.enqueue(extra) | ||
} | ||
} | ||
|
||
func (p *PGPubsub) recordReconnect() { | ||
p.qMu.Lock() | ||
defer p.qMu.Unlock() | ||
for _, listeners := range p.queues { | ||
for _, q := range listeners { | ||
for _, qSet := range p.queues { | ||
for q := range qSet.m { | ||
q.dropped() | ||
} | ||
} | ||
|
@@ -590,8 +652,8 @@ func (p *PGPubsub) Collect(metrics chan<- prometheus.Metric) { | |
p.qMu.Lock() | ||
events := len(p.queues) | ||
subs := 0 | ||
for _, subscriberMap := range p.queues { | ||
subs += len(subscriberMap) | ||
for _, qSet := range p.queues { | ||
subs += len(qSet.m) | ||
} | ||
p.qMu.Unlock() | ||
metrics <- prometheus.MustNewConstMetric(currentSubscribersDesc, prometheus.GaugeValue, float64(subs)) | ||
|
@@ -629,7 +691,7 @@ func newWithoutListener(logger slog.Logger, db *sql.DB) *PGPubsub { | |
logger: logger, | ||
listenDone: make(chan struct{}), | ||
db: db, | ||
queues: make(map[string]map[uuid.UUID]*msgQueue), | ||
queues: make(map[string]*queueSet), | ||
latencyMeasurer: NewLatencyMeasurer(logger.Named("latency-measurer")), | ||
|
||
publishesTotal: prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: m => q/queues. I feel this would explain better what one is looking at when viewing the code.