mirror of
https://github.com/coder/coder.git
synced 2026-10-28 02:42:54 +08:00
fix(coderd/x/chatd): resolve inflight race (#26460)
Using `WaitGroup.Go` must be synchronized with `WaitGroup.Wait` according to [go docs](https://pkg.go.dev/sync#WaitGroup.Go): > If the WaitGroup is empty, Go must happen before a [WaitGroup.Wait](https://pkg.go.dev/sync#WaitGroup.Wait). There were a couple of places in chatd that violated this principle. This was caught as a data race in https://github.com/coder/internal/issues/1599. This PR ensures that all functions that spawn inflight goroutines synchronize with each other. I also noticed that inflight goroutines may be spawned after the server is closed, which was surprising and looked like a bug. This PR therefore also introduces a mechanism that disallows spawning inflight goroutines after the server is closed, and ensures that any code that tries doing it logs an error. Closes https://github.com/coder/internal/issues/1599.
This commit is contained in:
@@ -169,7 +169,7 @@ func (p *Server) GenerateChatTitleAsync(ctx context.Context, chat database.Chat)
|
||||
// Detach from the request lifetime so title generation can finish
|
||||
// even after the create response is written.
|
||||
titleCtx := context.WithoutCancel(ctx)
|
||||
p.inflight.Go(func() {
|
||||
if err := p.goInflight(func() {
|
||||
modelOpts := modelBuildOptionsFromMessages(messages)
|
||||
titleCtx = withActiveTurnAPIKeyID(titleCtx, modelOpts)
|
||||
model, modelConfig, keys, route, _, _, _, err := p.resolveChatModel(titleCtx, chat, modelOpts)
|
||||
@@ -193,7 +193,13 @@ func (p *Server) GenerateChatTitleAsync(ctx context.Context, chat database.Chat)
|
||||
logger,
|
||||
p.existingDebugService(),
|
||||
)
|
||||
})
|
||||
}); err != nil {
|
||||
logger.Error(titleCtx, "failed to schedule automatic chat title generation",
|
||||
slog.F("chat_id", chat.ID),
|
||||
slog.F("owner_id", chat.OwnerID),
|
||||
slog.Error(err),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// maybeGenerateChatTitle generates an AI title for the chat when
|
||||
|
||||
Reference in New Issue
Block a user