-
Notifications
You must be signed in to change notification settings - Fork 199
fix(task-persistence): add cross-instance lock for concurrent task history updates (#920) #926
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
Draft
easonLiangWorldedtech
wants to merge
4
commits into
Zoo-Code-Org:main
Choose a base branch
from
easonLiangWorldedtech:fix/920-concurrent-task-history-cross-instance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ac5c2e0
fix(core): serialize task history updates across parallel tabs
easonliang28 370e9c6
fix(task-persistence): guard concurrent history mutations
easonliang28 0d004f7
fix(task-history): serialize store mutations with shared lock
easonliang28 381c183
fix(task-history): avoid fs open in lock path setup
easonliang28 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
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,91 @@ | ||
| import * as fs from "fs/promises" | ||
| import * as path from "path" | ||
| import * as lockfile from "proper-lockfile" | ||
|
|
||
| import { GlobalFileNames } from "../../shared/globalFileNames" | ||
| import { getStorageBasePath } from "../../utils/storage" | ||
|
|
||
| /** | ||
| * Cross-process lock for task history mutations. | ||
| * | ||
| * Multiple `ClineProvider` instances may live in separate extension-host | ||
| * processes (for example VS Code windows) while sharing the same task history | ||
| * storage. Each process has its own `TaskHistoryStore`, so an in-memory mutex is | ||
| * not sufficient. This lock serializes mutations by taking an exclusive advisory | ||
| * lock on the shared `tasks/_history.lock` file. | ||
| */ | ||
| export class TaskHistoryLock { | ||
| private queue: Promise<unknown> = Promise.resolve() | ||
|
|
||
| /** | ||
| * Acquires the shared task-history lock and executes `fn` while holding it. | ||
| * | ||
| * The lock file is scoped to the effective storage root (including custom | ||
| * storage path resolution) so all windows/processes targeting the same history | ||
| * store contend on the same file. | ||
| */ | ||
| async withLock<T>(globalStoragePath: string, fn: () => Promise<T>): Promise<T> { | ||
| const result = this.queue.then( | ||
| async () => { | ||
| const lockFilePath = await this.getLockFilePath(globalStoragePath) | ||
| return this.runWithFileLock(lockFilePath, fn) | ||
| }, | ||
| async () => { | ||
| const lockFilePath = await this.getLockFilePath(globalStoragePath) | ||
| return this.runWithFileLock(lockFilePath, fn) | ||
| }, | ||
| ) | ||
|
|
||
| this.queue = result.then( | ||
| () => undefined, | ||
| () => undefined, | ||
| ) | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| /** | ||
| * Clears in-process queues. File locks held by other processes are not affected. | ||
| */ | ||
| reset(): void { | ||
| this.queue = Promise.resolve() | ||
| } | ||
|
|
||
| async getLockFilePath(globalStoragePath: string): Promise<string> { | ||
| const basePath = await getStorageBasePath(globalStoragePath) | ||
| const tasksDir = path.join(basePath, "tasks") | ||
| await fs.mkdir(tasksDir, { recursive: true }) | ||
| return path.join(tasksDir, GlobalFileNames.historyLock) | ||
| } | ||
|
|
||
| private async runWithFileLock<T>(lockFilePath: string, fn: () => Promise<T>): Promise<T> { | ||
| let releaseLock: (() => Promise<void>) | undefined | ||
|
|
||
| try { | ||
| releaseLock = await lockfile.lock(lockFilePath, { | ||
| stale: 31000, | ||
| update: 10000, | ||
| realpath: false, | ||
| retries: { | ||
| retries: 36, | ||
| factor: 1, | ||
| minTimeout: 1000, | ||
| maxTimeout: 1000, | ||
| }, | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| onCompromised: (err) => { | ||
| console.error(`[TaskHistoryLock] Lock at ${lockFilePath} was compromised:`, err) | ||
| throw err | ||
| }, | ||
| }) | ||
|
|
||
| return await fn() | ||
| } finally { | ||
| if (releaseLock) { | ||
| await releaseLock() | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Singleton instance shared across all ClineProvider instances in this process. | ||
| export const taskHistoryLock = new TaskHistoryLock() | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.