-
Notifications
You must be signed in to change notification settings - Fork 3.5k
fix(mothership): Use heartbeat mechanism for chat locks #4286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
|
|
||
| import { redisConfigMock, redisConfigMockFns } from '@sim/testing' | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { mockHasAbortMarker, mockClearAbortMarker, mockWriteAbortMarker } = vi.hoisted(() => ({ | ||
| mockHasAbortMarker: vi.fn().mockResolvedValue(false), | ||
| mockClearAbortMarker: vi.fn().mockResolvedValue(undefined), | ||
| mockWriteAbortMarker: vi.fn().mockResolvedValue(undefined), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/core/config/redis', () => redisConfigMock) | ||
| vi.mock('@/lib/copilot/request/session/buffer', () => ({ | ||
| hasAbortMarker: mockHasAbortMarker, | ||
| clearAbortMarker: mockClearAbortMarker, | ||
| writeAbortMarker: mockWriteAbortMarker, | ||
| })) | ||
| vi.mock('@/lib/copilot/request/otel', () => ({ | ||
| withCopilotSpan: (_span: unknown, _attrs: unknown, fn: (span: unknown) => unknown) => | ||
| fn({ setAttribute: vi.fn() }), | ||
| })) | ||
|
|
||
| import { startAbortPoller } from '@/lib/copilot/request/session/abort' | ||
|
|
||
| describe('startAbortPoller heartbeat', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| vi.useFakeTimers() | ||
| mockHasAbortMarker.mockResolvedValue(false) | ||
| redisConfigMockFns.mockExtendLock.mockResolvedValue(true) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers() | ||
| }) | ||
|
|
||
| it('extends the chat stream lock approximately every heartbeat interval', async () => { | ||
| const controller = new AbortController() | ||
| const streamId = 'stream-heartbeat-1' | ||
| const chatId = 'chat-heartbeat-1' | ||
|
|
||
| const interval = startAbortPoller(streamId, controller, { chatId }) | ||
|
|
||
| try { | ||
| await vi.advanceTimersByTimeAsync(15_000) | ||
| expect(redisConfigMockFns.mockExtendLock).not.toHaveBeenCalled() | ||
|
|
||
| await vi.advanceTimersByTimeAsync(6_000) | ||
|
|
||
| expect(redisConfigMockFns.mockExtendLock).toHaveBeenCalledTimes(1) | ||
| expect(redisConfigMockFns.mockExtendLock).toHaveBeenLastCalledWith( | ||
| `copilot:chat-stream-lock:${chatId}`, | ||
| streamId, | ||
| 60 | ||
| ) | ||
|
|
||
| await vi.advanceTimersByTimeAsync(20_000) | ||
| expect(redisConfigMockFns.mockExtendLock).toHaveBeenCalledTimes(2) | ||
|
|
||
| await vi.advanceTimersByTimeAsync(20_000) | ||
| expect(redisConfigMockFns.mockExtendLock).toHaveBeenCalledTimes(3) | ||
| } finally { | ||
| clearInterval(interval) | ||
| } | ||
| }) | ||
|
|
||
| it('does not extend the lock when no chatId is passed (backward compat)', async () => { | ||
| const controller = new AbortController() | ||
| const interval = startAbortPoller('stream-no-chat', controller, {}) | ||
|
|
||
| try { | ||
| await vi.advanceTimersByTimeAsync(90_000) | ||
| expect(redisConfigMockFns.mockExtendLock).not.toHaveBeenCalled() | ||
| } finally { | ||
| clearInterval(interval) | ||
| } | ||
| }) | ||
|
|
||
| it('retries on the next tick when extendLock throws (no 20s backoff)', async () => { | ||
| const controller = new AbortController() | ||
| const streamId = 'stream-retry' | ||
| const chatId = 'chat-retry' | ||
|
|
||
| redisConfigMockFns.mockExtendLock.mockRejectedValueOnce(new Error('redis down')) | ||
|
|
||
| const interval = startAbortPoller(streamId, controller, { chatId }) | ||
|
|
||
| try { | ||
| await vi.advanceTimersByTimeAsync(20_000) | ||
| expect(redisConfigMockFns.mockExtendLock).toHaveBeenCalledTimes(1) | ||
|
|
||
| await vi.advanceTimersByTimeAsync(1_000) | ||
| expect(redisConfigMockFns.mockExtendLock).toHaveBeenCalledTimes(2) | ||
| } finally { | ||
| clearInterval(interval) | ||
| } | ||
| }) | ||
|
|
||
| it('stops heartbeating after ownership is lost', async () => { | ||
| const controller = new AbortController() | ||
| const streamId = 'stream-lost' | ||
| const chatId = 'chat-lost' | ||
|
|
||
| redisConfigMockFns.mockExtendLock.mockResolvedValueOnce(false) | ||
|
|
||
| const interval = startAbortPoller(streamId, controller, { chatId }) | ||
|
|
||
| try { | ||
| await vi.advanceTimersByTimeAsync(21_000) | ||
| expect(redisConfigMockFns.mockExtendLock).toHaveBeenCalledTimes(1) | ||
|
|
||
| await vi.advanceTimersByTimeAsync(60_000) | ||
| expect(redisConfigMockFns.mockExtendLock).toHaveBeenCalledTimes(1) | ||
| } finally { | ||
| clearInterval(interval) | ||
| } | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extendLockis slowThe heartbeat section executes after
pollingStreams.delete(streamId)in thefinallyblock, so by the timeextendLockis awaited the next interval tick (1 s) can enter, pass thepollingStreamsguard, finish its abort poll, delete again, and reach the time check whilelastHeartbeatAtstill hasn't been updated by the in-flight call. If Redis latency exceeds the 1 s poll interval — e.g. during the 5 scommandTimeout— several concurrentEXPIREcalls can pile up.EXPIREis idempotent so correctness is unaffected, but under degraded-Redis conditions this amplifies the load exactly when Redis is already struggling. UpdatinglastHeartbeatAtoptimistically before the await would eliminate the duplicate calls:This preserves the fast-retry-on-error behaviour the test checks for while capping concurrent calls to one.