Skip to content

Commit fe8cc3b

Browse files
committed
perf(coderd/x/chatd): make chat stream reads lock-free
ChatMachine.ReadLock took a FOR SHARE row lock (GetChatByIDForShare) to give multi-statement readers a consistent snapshot. The stream loop runs this on every snapshot_version bump for every subscriber, so the read serialized against the FOR UPDATE writers and showed up as heavy lock wait under load. Run the readers in a read-only REPEATABLE READ transaction instead, as ChatMachine.ReadSnapshot. Snapshot isolation gives the same multi-statement consistency without a row lock, so reads no longer block or get blocked by writers. Ordering is still handled by the snapshot_version watermark, so an older snapshot triggers a later refetch rather than an inconsistent read. The lock statement also doubled as an existence check that returned ErrChatNotFound before the callback ran. Nothing needs it: every caller reads the chat as its first statement and already handles sql.ErrNoRows (the task loaders map it to errTaskExpectedExit, the HTTP handler maps it to 404), so ReadSnapshot opens the transaction and runs the callback, saving a round trip per stream sync. Two concurrency tests pin the guarantees: an open ReadSnapshot neither blocks a concurrent Update nor observes its commit (and the next read does), and a ReadSnapshot completes while another transaction holds the row's FOR UPDATE lock. Both fail against the FOR SHARE implementation.
1 parent c7da54c commit fe8cc3b

8 files changed

Lines changed: 173 additions & 51 deletions

File tree

‎coderd/x/chatd/chatstate/concurrency_test.go‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,136 @@ func TestConcurrentUpdatesSerializeOnChatRow(t *testing.T) {
218218
require.Equal(t, before.SnapshotVersion+int64(updates), after.SnapshotVersion,
219219
"snapshot_version advanced by exactly one per update")
220220
}
221+
222+
// TestReadSnapshotDoesNotBlockWriters verifies the guarantee that
223+
// replaced the FOR SHARE lock: an open ReadSnapshot neither blocks a
224+
// concurrent Update nor observes its commit. The Update completes while
225+
// the reader is still inside its callback, the reader keeps seeing the
226+
// state from before the commit for the rest of the snapshot, and the
227+
// commit is visible, and its chat:update published, for the next read.
228+
func TestReadSnapshotDoesNotBlockWriters(t *testing.T) {
229+
t.Parallel()
230+
f := newTestFixture(t)
231+
ctx := testutil.Context(t, testutil.WaitMedium)
232+
created := createTestChat(t, f)
233+
reader := chatstate.NewChatMachine(f.DB, f.Pub, created.Chat.ID)
234+
writer := chatstate.NewChatMachine(f.DB, f.Pub, created.Chat.ID)
235+
236+
readStarted := make(chan struct{})
237+
writerDone := make(chan struct{})
238+
t.Cleanup(func() {
239+
select {
240+
case <-writerDone:
241+
default:
242+
close(writerDone)
243+
}
244+
})
245+
246+
// Goroutine A: open a snapshot, read the chat, then hold the
247+
// snapshot open until the writer has committed and read again.
248+
var first, second database.Chat
249+
var readErr error
250+
var readWG sync.WaitGroup
251+
readWG.Go(func() {
252+
readErr = reader.ReadSnapshot(func(store database.Store) error {
253+
var err error
254+
first, err = store.GetChatByID(ctx, created.Chat.ID)
255+
if err != nil {
256+
return err
257+
}
258+
close(readStarted)
259+
if !waitForChan(ctx, writerDone) {
260+
return ctx.Err()
261+
}
262+
second, err = store.GetChatByID(ctx, created.Chat.ID)
263+
return err
264+
})
265+
})
266+
require.True(t, waitForChan(ctx, readStarted), "ReadSnapshot callback never started")
267+
268+
// Goroutine B: a transition on the same chat. It must commit while
269+
// A still holds its snapshot; with the former FOR SHARE lock it would
270+
// have waited for A to finish.
271+
publishedBefore := len(f.Pub.channels)
272+
var updateErr error
273+
var updateWG sync.WaitGroup
274+
updateWG.Go(func() {
275+
updateErr = writer.Update(ctx, func(tx *chatstate.Tx, _ database.Store) error {
276+
_, err := tx.FinishTurn(chatstate.FinishTurnInput{})
277+
return err
278+
})
279+
})
280+
require.True(t, waitForWaitGroup(ctx, &updateWG), "Update did not finish while the snapshot was open")
281+
require.NoError(t, updateErr)
282+
require.Greater(t, len(f.Pub.channels), publishedBefore, "the committed transition published its hint")
283+
284+
close(writerDone)
285+
require.True(t, waitForWaitGroup(ctx, &readWG), "ReadSnapshot did not finish")
286+
require.NoError(t, readErr)
287+
288+
// The snapshot is one consistent state: the commit is invisible to it.
289+
require.Equal(t, created.Chat.SnapshotVersion, first.SnapshotVersion)
290+
require.Equal(t, first.SnapshotVersion, second.SnapshotVersion, "a commit during the snapshot must not be visible to it")
291+
require.Equal(t, database.ChatStatusRunning, second.Status)
292+
293+
// The next read observes the commit, which is what the stream loop's
294+
// hint-driven resync relies on.
295+
after := f.readChat(ctx, t, created.Chat.ID)
296+
require.Equal(t, created.Chat.SnapshotVersion+1, after.SnapshotVersion)
297+
require.Equal(t, database.ChatStatusWaiting, after.Status)
298+
}
299+
300+
// TestReadSnapshotNotBlockedByRowLock verifies the other direction: a
301+
// ReadSnapshot completes while another transaction holds the chat row's
302+
// FOR UPDATE lock. The former FOR SHARE read would have queued behind it.
303+
func TestReadSnapshotNotBlockedByRowLock(t *testing.T) {
304+
t.Parallel()
305+
f := newTestFixture(t)
306+
ctx := testutil.Context(t, testutil.WaitMedium)
307+
created := createTestChat(t, f)
308+
locker := chatstate.NewChatMachine(f.DB, f.Pub, created.Chat.ID)
309+
reader := chatstate.NewChatMachine(f.DB, f.Pub, created.Chat.ID)
310+
311+
lockEntered := make(chan struct{})
312+
releaseLock := make(chan struct{})
313+
t.Cleanup(func() {
314+
select {
315+
case <-releaseLock:
316+
default:
317+
close(releaseLock)
318+
}
319+
})
320+
321+
// Goroutine A: hold the row lock and block.
322+
var lockErr error
323+
var lockWG sync.WaitGroup
324+
lockWG.Go(func() {
325+
lockErr = locker.Lock(ctx, func(_ database.Store) error {
326+
close(lockEntered)
327+
if !waitForChan(ctx, releaseLock) {
328+
return ctx.Err()
329+
}
330+
return nil
331+
})
332+
})
333+
require.True(t, waitForChan(ctx, lockEntered), "Lock callback never started")
334+
335+
// The read must complete without the lock being released.
336+
var read database.Chat
337+
var readErr error
338+
var readWG sync.WaitGroup
339+
readWG.Go(func() {
340+
readErr = reader.ReadSnapshot(func(store database.Store) error {
341+
var err error
342+
read, err = store.GetChatByID(ctx, created.Chat.ID)
343+
return err
344+
})
345+
})
346+
require.True(t, waitForWaitGroup(ctx, &readWG), "ReadSnapshot blocked behind the row lock")
347+
require.NoError(t, readErr)
348+
require.Equal(t, created.Chat.ID, read.ID)
349+
350+
close(releaseLock)
351+
require.True(t, waitForWaitGroup(ctx, &lockWG), "Lock did not finish")
352+
require.NoError(t, lockErr)
353+
}

‎coderd/x/chatd/chatstate/machine.go‎

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -267,41 +267,34 @@ func (m *ChatMachine) Lock(
267267
}, nil)
268268
}
269269

270-
// ReadLock takes a shared lock on the chat row with FOR SHARE and runs
271-
// fn in a transaction without advancing snapshot_version. It uses the
272-
// store captured by [NewChatMachine]. Use it when the caller needs a
273-
// consistent chat snapshot plus related rows such as messages or queued
274-
// messages but is NOT applying a transition and does NOT need to block
275-
// concurrent readers.
270+
// ReadSnapshot runs fn in a read-only REPEATABLE READ transaction without
271+
// advancing snapshot_version. It uses the store captured by
272+
// [NewChatMachine]. Use it when the caller needs a consistent chat
273+
// snapshot plus related rows such as messages or queued messages but is
274+
// NOT applying a transition.
276275
//
277-
// Unlike [ChatMachine.Lock], the FOR SHARE lock permits other shared
278-
// lockers to proceed concurrently while still blocking writers that take
279-
// FOR UPDATE (such as [ChatMachine.Update] and [ChatMachine.Lock]) until
280-
// the transaction commits.
276+
// Snapshot isolation gives fn a consistent multi-statement view without
277+
// taking any row lock, so it never blocks (or is blocked by) the FOR
278+
// UPDATE writers in [ChatMachine.Update] and [ChatMachine.Lock]. A
279+
// transition committing mid-read is simply not visible to this snapshot;
280+
// callers reconcile ordering via snapshot_version, so an older snapshot
281+
// triggers a later refetch rather than an inconsistent read.
282+
//
283+
// ReadSnapshot does not check that the chat exists; fn's own reads report
284+
// a missing chat as sql.ErrNoRows.
281285
//
282286
// Callers must not pass a store here; it belongs on the machine.
283287
//
284-
// ReadLock publishes nothing. Callback errors roll back the transaction
285-
// and propagate to the caller.
286-
func (m *ChatMachine) ReadLock(
287-
ctx context.Context,
288-
fn func(database.Store) error,
289-
) error {
288+
// ReadSnapshot publishes nothing. Callback errors roll back the
289+
// transaction and propagate to the caller.
290+
func (m *ChatMachine) ReadSnapshot(fn func(database.Store) error) error {
290291
if m.store == nil {
291292
return xerrors.New("chatstate: ChatMachine has nil store")
292293
}
293-
return m.store.InTx(func(store database.Store) error {
294-
// GetChatByIDForShare takes a shared lock on the row WITHOUT
295-
// bumping snapshot.
296-
_, err := store.GetChatByIDForShare(ctx, m.chatID)
297-
if err != nil {
298-
if errors.Is(err, sql.ErrNoRows) {
299-
return ErrChatNotFound
300-
}
301-
return xerrors.Errorf("read lock chat: %w", err)
302-
}
303-
return fn(store)
304-
}, nil)
294+
return m.store.InTx(fn, &database.TxOptions{
295+
Isolation: sql.LevelRepeatableRead,
296+
ReadOnly: true,
297+
})
305298
}
306299

307300
// ownershipStaleOrMissing reports whether the chat's current

‎coderd/x/chatd/chatstate/machine_test.go‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func TestChatMachine_Lock_DoesNotBumpSnapshot(t *testing.T) {
195195
require.Equal(t, publishedBefore, len(f.Pub.channels), "Lock must not publish")
196196
}
197197

198-
func TestChatMachine_ReadLock_DoesNotBumpSnapshot(t *testing.T) {
198+
func TestChatMachine_ReadSnapshot_DoesNotBumpSnapshot(t *testing.T) {
199199
t.Parallel()
200200
f := newTestFixture(t)
201201
ctx := testutil.Context(t, testutil.WaitShort)
@@ -206,26 +206,27 @@ func TestChatMachine_ReadLock_DoesNotBumpSnapshot(t *testing.T) {
206206
publishedBefore := len(f.Pub.channels)
207207

208208
var called bool
209-
require.NoError(t, m.ReadLock(ctx, func(_ database.Store) error {
209+
require.NoError(t, m.ReadSnapshot(func(_ database.Store) error {
210210
called = true
211211
return nil
212212
}))
213-
require.True(t, called, "ReadLock must invoke the callback")
213+
require.True(t, called, "ReadSnapshot must invoke the callback")
214214
after := f.readChat(ctx, t, created.Chat.ID)
215215
require.Equal(t, before.SnapshotVersion, after.SnapshotVersion)
216-
require.Equal(t, publishedBefore, len(f.Pub.channels), "ReadLock must not publish")
216+
require.Equal(t, publishedBefore, len(f.Pub.channels), "ReadSnapshot must not publish")
217217
}
218218

219-
func TestChatMachine_ReadLock_RejectsMissingChat(t *testing.T) {
219+
func TestChatMachine_ReadSnapshot_PropagatesMissingChat(t *testing.T) {
220220
t.Parallel()
221221
f := newTestFixture(t)
222222
ctx := testutil.Context(t, testutil.WaitShort)
223-
m := chatstate.NewChatMachine(f.DB, f.Pub, uuid.New())
224-
err := m.ReadLock(ctx, func(_ database.Store) error {
225-
t.Fatal("callback must not run when the chat is missing")
226-
return nil
223+
chatID := uuid.New()
224+
m := chatstate.NewChatMachine(f.DB, f.Pub, chatID)
225+
err := m.ReadSnapshot(func(store database.Store) error {
226+
_, err := store.GetChatByID(ctx, chatID)
227+
return err
227228
})
228-
require.ErrorIs(t, err, chatstate.ErrChatNotFound)
229+
require.ErrorIs(t, err, sql.ErrNoRows)
229230
require.Empty(t, f.Pub.channels)
230231
}
231232

‎coderd/x/chatd/generation.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ func (s *taskStarter) cancelDeletedToolCalls(
500500
params.PreviousUserMessageID = messages[previous].ID
501501
}
502502
var deleted []database.ChatMessage
503-
err := machine.ReadLock(ctx, func(store database.Store) error {
503+
err := machine.ReadSnapshot(func(store database.Store) error {
504504
if _, err := loadChatForTask(ctx, store, input, database.ChatStatusRunning, taskFenceOptions{requireHistory: true}); err != nil {
505505
return xerrors.Errorf("load chat for task: %w", err)
506506
}
@@ -670,7 +670,7 @@ func loadGenerationState(
670670
) (database.Chat, []database.ChatMessage, error) {
671671
var chat database.Chat
672672
var messages []database.ChatMessage
673-
err := machine.ReadLock(ctx, func(store database.Store) error {
673+
err := machine.ReadSnapshot(func(store database.Store) error {
674674
loadedChat, err := loadChatForTask(ctx, store, input, database.ChatStatusRunning, taskFenceOptions{requireHistory: true})
675675
if err != nil {
676676
return xerrors.Errorf("load chat for task: %w", err)
@@ -1620,7 +1620,7 @@ func (s *taskStarter) finishGenerationTurn(
16201620
}
16211621
var chat database.Chat
16221622
var messages []database.ChatMessage
1623-
err := machine.ReadLock(ctx, func(store database.Store) error {
1623+
err := machine.ReadSnapshot(func(store database.Store) error {
16241624
loadedChat, err := loadChatForGeneration(ctx, store, input, fence)
16251625
if err != nil {
16261626
return xerrors.Errorf("load chat for stop hook: %w", err)

‎coderd/x/chatd/hook_server.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func loadDynamicPostToolUseState(
126126
opts SubmitToolResultsOptions,
127127
) (dynamicPostToolUseState, error) {
128128
var state dynamicPostToolUseState
129-
err := machine.ReadLock(ctx, func(store database.Store) error {
129+
err := machine.ReadSnapshot(func(store database.Store) error {
130130
chat, err := store.GetChatByID(ctx, opts.ChatID)
131131
if err != nil {
132132
return xerrors.Errorf("load chat: %w", err)

‎coderd/x/chatd/stream_loop.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (l *streamLoop) shouldFetch(hint streamSyncHint) bool {
147147
func (l *streamLoop) loadDBSnapshot(ctx context.Context) (streamDBSnapshot, error) {
148148
var snapshot streamDBSnapshot
149149
machine := chatstate.NewChatMachine(l.db, nil, l.chatID)
150-
err := machine.ReadLock(ctx, func(tx database.Store) error {
150+
err := machine.ReadSnapshot(func(tx database.Store) error {
151151
chat, err := tx.GetChatByID(ctx, l.chatID)
152152
if err != nil {
153153
return xerrors.Errorf("get chat for stream: %w", err)

‎coderd/x/chatd/stream_loop_internal_test.go‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -421,14 +421,9 @@ func TestStreamLoopInitialSyncRecoversWithoutHint(t *testing.T) {
421421
loop.state.snapshotVersion = 1
422422
loop.state.status = database.ChatStatusRunning
423423

424-
db.EXPECT().InTx(gomock.Any(), nil).DoAndReturn(
424+
db.EXPECT().InTx(gomock.Any(), gomock.Any()).DoAndReturn(
425425
func(fn func(database.Store) error, _ *database.TxOptions) error { return fn(tx) },
426426
)
427-
tx.EXPECT().GetChatByIDForShare(gomock.Any(), chatID).Return(database.Chat{
428-
ID: chatID,
429-
Status: database.ChatStatusWaiting,
430-
SnapshotVersion: 2,
431-
}, nil)
432427
tx.EXPECT().GetChatByID(gomock.Any(), chatID).Return(database.Chat{
433428
ID: chatID,
434429
Status: database.ChatStatusWaiting,

‎coderd/x/chatd/tasks.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func (s *taskStarter) StartInterrupt(ctx context.Context, input chatWorkerTaskSt
265265
cancelable []fantasy.ToolCallContent
266266
toolCallIDs map[string]uuid.UUID
267267
)
268-
err := machine.ReadLock(ctx, func(store database.Store) error {
268+
err := machine.ReadSnapshot(func(store database.Store) error {
269269
loadedChat, err := loadChatForTask(ctx, store, input, database.ChatStatusInterrupting, taskFenceOptions{requireHistory: true})
270270
if err != nil {
271271
return xerrors.Errorf("load chat for task: %w", err)
@@ -425,7 +425,7 @@ func decideRequiresActionTimeout(
425425
input chatWorkerTaskStartInput,
426426
) (requiresActionTimeoutDecision, error) {
427427
var decision requiresActionTimeoutDecision
428-
err := machine.ReadLock(ctx, func(store database.Store) error {
428+
err := machine.ReadSnapshot(func(store database.Store) error {
429429
chat, err := loadChatForTask(ctx, store, input, database.ChatStatusRequiresAction, taskFenceOptions{requireHistory: true})
430430
if err != nil {
431431
return xerrors.Errorf("load chat for task: %w", err)

0 commit comments

Comments
 (0)