- Notifications
You must be signed in to change notification settings - Fork909
fix: fix race condition in pubsub startup#17088
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -492,7 +492,6 @@ func (p *PGPubsub) startListener(ctx context.Context, connectURL string) error { | ||
p.connected.Set(0) | ||
// Creates a new listener using pq. | ||
var ( | ||
dialer = logDialer{ | ||
logger: p.logger, | ||
// pq.defaultDialer uses a zero net.Dialer as well. | ||
@@ -525,6 +524,10 @@ func (p *PGPubsub) startListener(ctx context.Context, connectURL string) error { | ||
dc.Dialer(dialer) | ||
} | ||
var ( | ||
errCh = make(chan error, 1) | ||
sentErrCh = false | ||
) | ||
p.pgListener = pqListenerShim{ | ||
Listener: pq.NewConnectorListener(connector, connectURL, time.Second, time.Minute, func(t pq.ListenerEventType, err error) { | ||
switch t { | ||
@@ -541,18 +544,16 @@ func (p *PGPubsub) startListener(ctx context.Context, connectURL string) error { | ||
p.logger.Error(ctx, "pubsub failed to connect to postgres", slog.Error(err)) | ||
} | ||
// This callback gets events whenever the connection state changes. | ||
// Only send the first error. | ||
if sentErrCh { | ||
return | ||
} | ||
errCh <- err // won't block because we are buffered. | ||
sentErrCh = true | ||
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. Do we need to protect this or is sequential execution guaranteed? 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. It executes sequentially, at least by inspection. There is always a chance they could change the implementation and call-back concurrently, but it didn't seem worth while defending for that. | ||
}), | ||
} | ||
select { | ||
case err = <-errCh: | ||
if err != nil { | ||
_ = p.pgListener.Close() | ||
return xerrors.Errorf("create pq listener: %w", err) | ||
Uh oh!
There was an error while loading.Please reload this page.