Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
spikecurtis merged 1 commit intomainfromspike/internal-525-pubsub
Mar 25, 2025
Merged
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletionscoderd/database/pubsub/pubsub.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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 (
errCh = make(chan error)
dialer = logDialer{
logger: p.logger,
// pq.defaultDialer uses a zero net.Dialer as well.
Expand DownExpand Up@@ -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 {
Expand All@@ -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.
// Don't send if the errChannel has already been closed.
select {
case <-errCh:
// Only send the first error.
if sentErrCh {
return
default:
errCh <- err
close(errCh)
}
errCh <- err // won't block because we are buffered.
sentErrCh = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Do we need to protect this or is sequential execution guaranteed?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The 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:
case err = <-errCh:
if err != nil {
_ = p.pgListener.Close()
return xerrors.Errorf("create pq listener: %w", err)
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp