From 62191910f8abfca71fe17ef2ef1cb9d51da9b9cd Mon Sep 17 00:00:00 2001 From: Ethan <39577870+ethanndickson@users.noreply.github.com> Date: Thu, 16 Apr 2026 16:39:19 +1000 Subject: [PATCH] fix(coderd/x/chatd): hoist system prompt fetch out of chat creation transaction (#24369) (#24415) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Partial backport of #24369 to `release/2.32`. Only the `CreateChat` fix in `coderd/x/chatd/chatd.go` applies here — the second call site in `subagent.go` (`createChildSubagentChatWithOptions`) doesn't exist on this branch since the child-subagent-chat creation path was added after the branch cut. The fix hoists the `resolveDeploymentSystemPrompt` call out of the `InTx` closure so the transaction doesn't hold one DB connection while the helper tries to check out another via `p.db`. Under concurrent chat creation load this pattern can cause pool starvation. This is not urgent enough to warrant an immediate patch release — the bug only manifests under high concurrent chat creation load. It should be fine to ride along with the next scheduled 2.32.x patch. --- coderd/x/chatd/chatd.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/coderd/x/chatd/chatd.go b/coderd/x/chatd/chatd.go index 45a0fdd46b..f6b52e8201 100644 --- a/coderd/x/chatd/chatd.go +++ b/coderd/x/chatd/chatd.go @@ -869,6 +869,10 @@ func (p *Server) CreateChat(ctx context.Context, opts CreateOptions) (database.C if opts.Labels == nil { opts.Labels = database.StringMap{} } + // Resolve the deployment prompt before opening the transaction so + // chat creation does not hold one DB connection while waiting for + // another pool checkout. + deploymentPrompt := p.resolveDeploymentSystemPrompt(ctx) var chat database.Chat txErr := p.db.InTx(func(tx database.Store) error { @@ -904,7 +908,6 @@ func (p *Server) CreateChat(ctx context.Context, opts CreateOptions) (database.C return xerrors.Errorf("insert chat: %w", err) } - deploymentPrompt := p.resolveDeploymentSystemPrompt(ctx) userPrompt := SanitizePromptText(opts.SystemPrompt) var workspaceAwareness string if opts.WorkspaceID.Valid {