Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all 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
16 changes: 16 additions & 0 deletions coderd/x/chatd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,27 @@ func (r *runner) run() {
}

func (r *runner) bootstrap() bool {
// Pubsub can deliver chat:update messages that were already queued by
// Postgres before this runner subscribed. Hold those hints until the
// runner initializes its local state with the current database snapshot.
// Otherwise a stale chat:update that shows the chat has no owner could
// cause the runner to exit before it starts work.
bootstrapReady := make(chan struct{})
bootstrapReadyClosed := false
defer func() {
if !bootstrapReadyClosed {
close(bootstrapReady)
}
}()

channel := coderdpubsub.ChatStateUpdateChannel(r.rec.key.ChatID)
unsubscribe, err := r.opts.Pubsub.SubscribeWithErr(channel, coderdpubsub.HandleChatStateUpdate(
func(ctx context.Context, payload coderdpubsub.ChatStateUpdateMessage, err error) {
if err != nil {
r.opts.Logger.Warn(ctx, "chatworker state update decode failed", slogError(err))
return
}
<-bootstrapReady

@mafredri mafredri Jun 17, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we select on r.ctx here as well (early exit)?

@hugodutka hugodutka Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

not necessary since bootstrapReady is guaranteed to be closed when the outer function exits

r.mgr.RouteStateHint(ctx, stateUpdateFromPubsub(r.rec.key.ChatID, payload))
},
))
Expand All @@ -114,6 +128,8 @@ func (r *runner) bootstrap() bool {
return false
}
r.mgr.RouteStateHint(r.ctx, stateUpdateFromChat(chat))
close(bootstrapReady)
bootstrapReadyClosed = true
return true
}

Expand Down
Loading