Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-9501: fix(daemon): stop claim delivery from deadlocking concurrent chat sends (#10828)

Jiayuan Zhang84ab6ff02543
diff --git a/server/internal/handler/chat_send_claim_deadlock_test.go b/server/internal/handler/chat_send_claim_deadlock_test.go
new file mode 100644
index 0000000000..55f24d0601
--- /dev/null
+++ b/server/internal/handler/chat_send_claim_deadlock_test.go
@@ -0,0 +1,262 @@
+package handler
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "net/http"
+ "strings"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/jackc/pgx/v5"
+ "github.com/jackc/pgx/v5/pgconn"
+
+ "github.com/multica-ai/multica/server/internal/testutil"
+ db "github.com/multica-ai/multica/server/pkg/db/generated"
+)
+
+// isDeadlock reports whether err carries PostgreSQL's deadlock_detected.
+func isDeadlock(err error) bool {
+ var pgErr *pgconn.PgError
+ return errors.As(err, &pgErr) && pgErr.Code == "40P01"
+}
+
+// waitForWaiterPIDBlockedBy is waitForWaiterBlockedBy that also returns the
+// waiter's backend pid, so a test can chain "A waits on B, C waits on A".
+func waitForWaiterPIDBlockedBy(t *testing.T, holderPID int, timeout time.Duration) int {
+ t.Helper()
+ deadline := time.Now().Add(timeout)
+ for {
+ var pid int
+ err := testPool.QueryRow(context.Background(), `
+ SELECT pid FROM pg_stat_activity
+ WHERE datname = current_database()
+ AND state = 'active'
+ AND wait_event_type = 'Lock'
+ AND wait_event IN ('transactionid', 'tuple')
+ AND $1::int = ANY(pg_blocking_pids(pid))
+ LIMIT 1
+ `, holderPID).Scan(&pid)
+ if err == nil {
+ return pid
+ }
+ if !errors.Is(err, pgx.ErrNoRows) {
+ t.Fatalf("probe pg_stat_activity for waiters blocked by pid %d: %v", holderPID, err)
+ }
+ if time.Now().After(deadline) {
+ return 0
+ }
+ time.Sleep(20 * time.Millisecond)
+ }
+}
+
+// A direct chat send holds the agent row (FOR UPDATE) when CreateChatTask's
+// owner fence asks for FOR KEY SHARE on the agent's runtime row. The claim
+// delivery gate locks that runtime row first and then the agent row. When the
+// gate held the runtime FOR UPDATE, the two waited on each other and Postgres
+// killed one with 40P01 (MUL-9501). The gate now holds the runtime in a mode
+// that lets the fence's KEY SHARE through.
+//
+// The interleaving is forced: a third transaction holds the workspace row so
+// the send parks inside the fence after taking the agent lock, the gate is
+// started and parks on that agent lock, and only then is the workspace
+// released so the send continues to the runtime row.
+func TestDirectChatSendDoesNotDeadlockWithClaimDeliveryGate(t *testing.T) {
+ if testHandler == nil || testPool == nil {
+ t.Skip("database not available")
+ }
+ ctx := context.Background()
+ agentID, claimedSessionID, runtimeID, _ := setupDirectChatSession(t, ctx, "claim gate deadlock: claimed")
+ sendSessionID := dbfx.ChatSession(t, agentID, testutil.Cols{
+ "title": "claim gate deadlock: send",
+ "explicitly_created_at": testutil.Raw("now()"),
+ })
+
+ claimedTaskID := sendDirectChat(t, ctx, agentID, claimedSessionID, "claimed turn")
+ claimed, err := testHandler.TaskService.ClaimTaskForRuntime(ctx, parseUUID(runtimeID))
+ if err != nil || claimed == nil || uuidToString(claimed.ID) != claimedTaskID {
+ t.Fatalf("claim task: task=%+v err=%v, want %s", claimed, err, claimedTaskID)
+ }
+ runtimeSnapshot, err := testHandler.Queries.GetAgentRuntimeForWorkspace(ctx, db.GetAgentRuntimeForWorkspaceParams{
+ ID: parseUUID(runtimeID),
+ WorkspaceID: parseUUID(testWorkspaceID),
+ })
+ if err != nil {
+ t.Fatalf("load claim-time runtime: %v", err)
+ }
+ sendSession, err := testHandler.Queries.GetChatSession(ctx, parseUUID(sendSessionID))
+ if err != nil {
+ t.Fatalf("load send session: %v", err)
+ }
+ agent, err := testHandler.Queries.GetAgent(ctx, parseUUID(agentID))
+ if err != nil {
+ t.Fatalf("load agent: %v", err)
+ }
+
+ workspaceHolder, err := testPool.Begin(ctx)
+ if err != nil {
+ t.Fatalf("begin workspace holder: %v", err)
+ }
+ defer workspaceHolder.Rollback(context.Background())
+ var holderPID int
+ if err := workspaceHolder.QueryRow(ctx, `SELECT pg_backend_pid()`).Scan(&holderPID); err != nil {
+ t.Fatalf("read holder pid: %v", err)
+ }
+ if _, err := workspaceHolder.Exec(ctx, `SELECT id FROM workspace WHERE id = $1 FOR UPDATE`, testWorkspaceID); err != nil {
+ t.Fatalf("hold workspace row: %v", err)
+ }
+
+ sendDone := make(chan error, 1)
+ go func() {
+ _, err := testHandler.TaskService.SendDirectChatMessage(context.Background(), sendSession, agent,
+ parseUUID(testUserID), "concurrent turn", nil, "member", parseUUID(testUserID))
+ sendDone <- err
+ }()
+ sendPID := waitForWaiterPIDBlockedBy(t, holderPID, 5*time.Second)
+ if sendPID == 0 {
+ t.Fatal("send never reached the owner fence's workspace lock")
+ }
+
+ gateDone := make(chan error, 1)
+ go func() {
+ req := newOwnerDaemonRequest(http.MethodPost, "/api/daemon/runtimes/"+runtimeID+"/tasks/claim", nil)
+ _, _, _, _, failure, err := testHandler.finalizeClaimDeliveryForTestWithRuntime(
+ req, claimed, runtimeSnapshot, runtimeID, testWorkspaceID,
+ )
+ if err == nil && failure != nil {
+ err = fmt.Errorf("delivery failure: %+v", failure)
+ }
+ gateDone <- err
+ }()
+ if !waitForWaiterBlockedBy(t, sendPID, 5*time.Second) {
+ t.Fatal("claim delivery gate never queued behind the send's agent row lock")
+ }
+
+ if err := workspaceHolder.Rollback(ctx); err != nil {
+ t.Fatalf("release workspace row: %v", err)
+ }
+
+ for name, done := range map[string]chan error{"send": sendDone, "claim delivery gate": gateDone} {
+ select {
+ case err := <-done:
+ if isDeadlock(err) {
+ t.Errorf("%s hit a deadlock: %v", name, err)
+ } else if err != nil {
+ t.Errorf("%s: %v", name, err)
+ }
+ case <-time.After(15 * time.Second):
+ t.Fatalf("%s did not finish", name)
+ }
+ }
+}
+
+// Many sessions of one agent sending at once while the daemon claims and
+// delivers that agent's earlier turns: every send must land its task and user
+// message, and every claim must deliver (MUL-9501). Each session starts with one
+// queued turn so the claimers run the delivery gate while the sends are in
+// flight, not after they have all committed.
+func TestDirectChatConcurrentSendsAcrossSessionsWhileDaemonClaims(t *testing.T) {
+ if testHandler == nil || testPool == nil {
+ t.Skip("database not available")
+ }
+ const (
+ sessions = 16
+ rounds = 10
+ claimers = 8
+ )
+ ctx := context.Background()
+ agentID, firstSessionID, runtimeID, _ := setupDirectChatSession(t, ctx, "concurrent sends 0")
+ dbfx.Exec(t, `UPDATE agent SET max_concurrent_tasks = $1 WHERE id = $2`, (rounds+1)*sessions, agentID)
+ t.Cleanup(func() {
+ testPool.Exec(context.Background(), `DELETE FROM task_token WHERE agent_id = $1`, agentID)
+ testPool.Exec(context.Background(), `DELETE FROM agent_task_queue WHERE agent_id = $1`, agentID)
+ })
+ sessionIDs := []string{firstSessionID}
+ for i := 1; i < sessions; i++ {
+ sessionIDs = append(sessionIDs, dbfx.ChatSession(t, agentID, testutil.Cols{
+ "title": fmt.Sprintf("concurrent sends %d", i),
+ "explicitly_created_at": testutil.Raw("now()"),
+ }))
+ }
+ chatSessions := make([]db.ChatSession, len(sessionIDs))
+ for i, id := range sessionIDs {
+ sendDirectChat(t, ctx, agentID, id, fmt.Sprintf("queued %d", i))
+ var err error
+ if chatSessions[i], err = testHandler.Queries.GetChatSession(ctx, parseUUID(id)); err != nil {
+ t.Fatalf("load session %s: %v", id, err)
+ }
+ }
+ agent, err := testHandler.Queries.GetAgent(ctx, parseUUID(agentID))
+ if err != nil {
+ t.Fatalf("load agent: %v", err)
+ }
+
+ start := make(chan struct{})
+ sendErrs := make([]error, len(chatSessions))
+ var sends sync.WaitGroup
+ for i := range chatSessions {
+ sends.Add(1)
+ go func(i int) {
+ defer sends.Done()
+ <-start
+ for r := 0; r < rounds && sendErrs[i] == nil; r++ {
+ _, sendErrs[i] = testHandler.TaskService.SendDirectChatMessage(context.Background(), chatSessions[i], agent,
+ parseUUID(testUserID), fmt.Sprintf("concurrent %d.%d", i, r), nil, "member", parseUUID(testUserID))
+ }
+ }(i)
+ }
+
+ sendsDone := make(chan struct{})
+ claimErrs := make(chan error, claimers)
+ var claims sync.WaitGroup
+ for c := 0; c < claimers; c++ {
+ claims.Add(1)
+ go func() {
+ defer claims.Done()
+ <-start
+ for {
+ req := testutil.WithURLParams(
+ newOwnerDaemonRequest(http.MethodPost, "/api/daemon/runtimes/"+runtimeID+"/tasks/claim", nil),
+ "runtimeId", runtimeID,
+ )
+ w := testutil.Call(t, testHandler.ClaimTaskByRuntime, req)
+ if w.Code != http.StatusOK {
+ claimErrs <- fmt.Errorf("claim returned %d: %s", w.Code, w.Body.String())
+ return
+ }
+ select {
+ case <-sendsDone:
+ if strings.TrimSpace(w.Body.String()) == `{"task":null}` {
+ return
+ }
+ default:
+ }
+ }
+ }()
+ }
+
+ close(start)
+ sends.Wait()
+ close(sendsDone)
+ claims.Wait()
+ close(claimErrs)
+
+ for err := range claimErrs {
+ t.Errorf("daemon claim: %v", err)
+ }
+ for i, err := range sendErrs {
+ if err != nil {
+ t.Errorf("send to session %d: %v (deadlock=%v)", i, err, isDeadlock(err))
+ }
+ }
+ for i, id := range sessionIDs {
+ var tasks, userMessages int
+ dbfx.QueryRow(t, `SELECT count(*) FROM agent_task_queue WHERE chat_session_id = $1`, id).Scan(&tasks)
+ dbfx.QueryRow(t, `SELECT count(*) FROM chat_message WHERE chat_session_id = $1 AND role = 'user'`, id).Scan(&userMessages)
+ if tasks != rounds+1 || userMessages != rounds+1 {
+ t.Errorf("session %d: tasks=%d user messages=%d, want %d of each", i, tasks, userMessages, rounds+1)
+ }
+ }
+}
diff --git a/server/internal/handler/daemon.go b/server/internal/handler/daemon.go
index 0227a59102..16ca2ef5c8 100644
--- a/server/internal/handler/daemon.go
+++ b/server/internal/handler/daemon.go
@@ -2502,8 +2502,8 @@ func claimPollHintDelay(now, fireAt time.Time) time.Duration {
// finalizeClaimDelivery is the shared final delivery gate for the singular and
// batch claim paths. It re-verifies the current agent/runtime authorization
-// INSIDE the FinalizeTaskClaim transaction, under a FOR UPDATE row lock on the
-// runtime row, so the decision and the task-token mint commit atomically:
+// INSIDE the FinalizeTaskClaim transaction, under a FOR NO KEY UPDATE row lock
+// on the runtime row, so the decision and the task-token mint commit atomically:
//
// - runtime.owner_id is re-read authoritatively (no stale claim-time
// snapshot), and a concurrent re-registration that would change owner_id
@@ -2533,11 +2533,11 @@ func (h *Handler) finalizeClaimDelivery(
) (receipt []pgtype.UUID, deliveryFailure *claimBuildFailure, err error) {
var agentOwnerID pgtype.UUID
authorize := func(qtx *db.Queries, tokenParams *db.CreateTaskTokenParams) error {
- // FOR UPDATE on the runtime row: any concurrent
- // UpsertAgentRuntime/visibility flip that would change owner_id or
- // visibility blocks until this transaction commits, so the values read
- // here are the ones delivery is authorized against.
- locked, lerr := qtx.LockAgentRuntime(ctx, runtime.ID)
+ // Any concurrent UpsertAgentRuntime/visibility flip that would change
+ // owner_id or visibility blocks until this transaction commits, so the
+ // values read here are the ones delivery is authorized against. The
+ // lock must not be FOR UPDATE: see LockAgentRuntimeForClaimDelivery.
+ locked, lerr := qtx.LockAgentRuntimeForClaimDelivery(ctx, runtime.ID)
if lerr != nil {
return fmt.Errorf("lock runtime for delivery authorization: %w", lerr)
}
diff --git a/server/internal/service/task.go b/server/internal/service/task.go
index a1f2199cee..61a8fcee10 100644
--- a/server/internal/service/task.go
+++ b/server/internal/service/task.go
@@ -4093,11 +4093,12 @@ func (e *ClaimDeliveryAuthzError) Error() string {
// no snapshot leaves the following comment-triggered run with no baseline.
//
// The optional authorize closure runs INSIDE the same transaction, after the
-// gate has re-read the current runtime row under a FOR UPDATE row lock. That
-// makes the authorization decision and the task-token/daemon-token writes one
-// atomic unit: a concurrent runtime re-registration that would change owner_id
-// blocks until the gate commits, so the owner the gate authorized against is
-// the owner the tokens were minted for — no stale-snapshot delivery window.
+// gate has re-read the current runtime row under a FOR NO KEY UPDATE row
+// lock. That makes the authorization decision and the task-token/daemon-token
+// writes one atomic unit: a concurrent runtime re-registration that would
+// change owner_id blocks until the gate commits, so the owner the gate
+// authorized against is the owner the tokens were minted for — no
+// stale-snapshot delivery window.
// The closure receives the in-transaction token params so it can normalize
// identity fields from the locked rows before the token is inserted. It
// returns a *ClaimDeliveryAuthzError to reject delivery (every other error
diff --git a/server/pkg/db/generated/runtime.sql.go b/server/pkg/db/generated/runtime.sql.go
index de8288d573..fd58db20da 100644
--- a/server/pkg/db/generated/runtime.sql.go
+++ b/server/pkg/db/generated/runtime.sql.go
@@ -1431,6 +1431,46 @@ func (q *Queries) LockAgentRuntimeForAccessChange(ctx context.Context, id pgtype
return i, err
}
+const lockAgentRuntimeForClaimDelivery = `-- name: LockAgentRuntimeForClaimDelivery :one
+SELECT id, workspace_id, daemon_id, name, runtime_mode, provider, status, device_info, metadata, last_seen_at, created_at, updated_at, owner_id, legacy_daemon_id, visibility, profile_id, custom_name, shared_with, max_concurrent_tasks FROM agent_runtime
+WHERE id = $1
+FOR NO KEY UPDATE
+`
+
+// The claim delivery gate's lock on the runtime it re-reads owner_id and
+// visibility from. FOR NO KEY UPDATE still blocks every UPDATE and DELETE of
+// the row, so a re-registration or visibility flip waits for the gate to
+// commit. Unlike FOR UPDATE it does not block FOR KEY SHARE, which every task
+// insert takes on its runtime (lock_task_owner_rows and the foreign key) while
+// already holding the agent row. The gate takes the agent row after this one,
+// so FOR UPDATE here deadlocked a chat send racing a delivery (MUL-9501).
+func (q *Queries) LockAgentRuntimeForClaimDelivery(ctx context.Context, id pgtype.UUID) (AgentRuntime, error) {
+ row := q.db.QueryRow(ctx, lockAgentRuntimeForClaimDelivery, id)
+ var i AgentRuntime
+ err := row.Scan(
+ &i.ID,
+ &i.WorkspaceID,
+ &i.DaemonID,
+ &i.Name,
+ &i.RuntimeMode,
+ &i.Provider,
+ &i.Status,
+ &i.DeviceInfo,
+ &i.Metadata,
+ &i.LastSeenAt,
+ &i.CreatedAt,
+ &i.UpdatedAt,
+ &i.OwnerID,
+ &i.LegacyDaemonID,
+ &i.Visibility,
+ &i.ProfileID,
+ &i.CustomName,
+ &i.SharedWith,
+ &i.MaxConcurrentTasks,
+ )
+ return i, err
+}
+
const lockRuntimeClaimCapacity = `-- name: LockRuntimeClaimCapacity :exec
SELECT pg_advisory_xact_lock(hashtextextended($1::uuid::text || ':runtime_claim_capacity', 0))
`
diff --git a/server/pkg/db/queries/runtime.sql b/server/pkg/db/queries/runtime.sql
index 5688f5d89d..9f41c0a7ff 100644
--- a/server/pkg/db/queries/runtime.sql
+++ b/server/pkg/db/queries/runtime.sql
@@ -59,6 +59,18 @@ SELECT * FROM agent_runtime
WHERE id = $1
FOR UPDATE;
+-- name: LockAgentRuntimeForClaimDelivery :one
+-- The claim delivery gate's lock on the runtime it re-reads owner_id and
+-- visibility from. FOR NO KEY UPDATE still blocks every UPDATE and DELETE of
+-- the row, so a re-registration or visibility flip waits for the gate to
+-- commit. Unlike FOR UPDATE it does not block FOR KEY SHARE, which every task
+-- insert takes on its runtime (lock_task_owner_rows and the foreign key) while
+-- already holding the agent row. The gate takes the agent row after this one,
+-- so FOR UPDATE here deadlocked a chat send racing a delivery (MUL-9501).
+SELECT * FROM agent_runtime
+WHERE id = $1
+FOR NO KEY UPDATE;
+
-- name: GetAgentRuntimeForWorkspace :one
SELECT * FROM agent_runtime
WHERE id = $1 AND workspace_id = $2;