Skip to content

Commit 23f42de

Browse files
abort-mergeeasonLiangWorldedtech
authored andcommitted
fix(u5): stop a recovery from rolling back an edit it no longer owns
#1930 row 4505100232 (src/integrations/editor/DiffViewProvider.ts:1182 at 22969c6). attemptRollbackRecovery() installs the captured edit, awaits the buffer's re-acquisition, and then runs revertChanges(). The install is not a reservation: while that acquisition is awaited, the retry the refused rollback is waiting for can open a different target on the same provider, and open() overwrites relPath, editType, createdDirs and originalContent. revertChanges() reads this class fields rather than the record it was handed, so a recovery that finished its await unchecked restored and saved the other edit's content into the other edit's target and tore a live edit down - the recovery never stops on its own, it just stops being about the edit it started from. The recovery is now bound to the edit it started from. Ownership - this edit's target still installed as this edit's type - is re-checked when the acquisition settles, and a recovery that lost the provider returns the same false the caller already keeps refusing on, leaves the live edit's state alone rather than rolling it back to what this recovery found, and does not install the editor it managed to acquire into that edit's slot. The baseline is not part of ownership: a later flow replacing originalContent is the diff-side question the acquisition's own capture at :1182 answers, so the guard names target and edit type and nothing else. Two cases in DiffViewProvider.spec.ts: a retry taking the provider for another target mid-acquisition (recovery reports false, nothing is restored or saved for either path, and the live edit keeps its own target, baseline and editor slot), and the provider being cleared again mid-acquisition (recovery reports false and drops the editor it acquired rather than installing it). With the component reverted to 22969c6 those two are the only failures - the first resolves true because the rollback ran on the other edit's state, the second leaves the acquired editor installed - and the other 97 pass; the component was put back byte-for-byte afterwards. Measured on the pinned runtime v22.23.4: 99 passed (99) in the focused spec, 107 passed across integrations/editor, the misc shard 2120 passed / 13 skipped and the core shard 4294 passed / 9 skipped both exiting 0 with an empty FAIL-set, tsc --noEmit -p tsconfig.json clean, eslint --max-warnings=0 clean on both touched files, prettier clean, eslint-suppressions unchanged at 265 files / 2870 entries and that file not dirty. NO local Stryker preflight ran for this commit.
1 parent 22969c6 commit 23f42de

2 files changed

Lines changed: 111 additions & 1 deletion

File tree

‎src/integrations/editor/DiffViewProvider.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,12 @@ export class DiffViewProvider {
597597
*
598598
* A live edit is not hijacked. If an edit is in progress for another target this recovery cannot
599599
* run without rolling back somebody else work, so it reports false and the caller keeps refusing.
600+
*
601+
* The same bound holds across the await this recovery performs. It re-acquires the buffer, and while
602+
* that awaits another flow can take the provider for a different edit; ownership is re-checked when
603+
* the acquisition settles, and a recovery that no longer owns the edit it started from stops there
604+
* with the live edit's state untouched, because the rollback below reads this class fields rather
605+
* than the record it was handed.
600606
*/
601607
async attemptRollbackRecovery(context: RollbackRecoveryContext): Promise<boolean> {
602608
if (this.isEditing) {
@@ -629,16 +635,41 @@ export class DiffViewProvider {
629635
// If the file is gone by now the acquisition rejects, and the caller treats a rejection exactly
630636
// like a refused restore - it keeps the refusal instead of clearing it on a rollback that did not
631637
// happen.
638+
let acquiredEditor: vscode.TextEditor
632639
try {
633-
this.activeDiffEditor = await this.openDiffEditor()
640+
acquiredEditor = await this.openDiffEditor()
634641
} catch (acquisitionError: unknown) {
642+
// Captured above: a failed acquisition puts the provider back exactly as it was found.
635643
this.isEditing = wasEditing
636644
this.relPath = previousRelPath
637645
this.editType = previousEditType
638646
this.createdDirs = previousCreatedDirs
639647
this.originalContent = previousOriginalContent
640648
throw acquisitionError
641649
}
650+
// The acquisition is where this recovery spends its time, and it awaits editor work the rest of the
651+
// extension keeps running through: while this one waits, a retry can start another edit on this
652+
// very provider, and open() overwrites relPath, editType, createdDirs and originalContent.
653+
// revertChanges() works from whatever this class holds the moment it runs, not from the record this
654+
// recovery installed, so finishing here unchecked would restore and save the other edit's content
655+
// into the other edit's target and tear that live edit down. Recovery is bound to the edit that
656+
// started it: once this provider answers to a different edit it stops, reports the same false the
657+
// caller already keeps refusing on, and leaves the live edit's state alone rather than rolling it
658+
// back to something this recovery found. The baseline is not part of ownership - a later flow
659+
// replacing originalContent is the diff-side question the acquisition's own capture answers, not a
660+
// claim on the provider.
661+
if (
662+
!this.isEditing ||
663+
!this.relPath ||
664+
!arePathsEqual(path.resolve(this.cwd, this.relPath), context.absolutePath) ||
665+
this.editType !== context.editType
666+
) {
667+
// Somebody else owns this provider now, and its editor is theirs to install: the acquired editor
668+
// is dropped without being written into activeDiffEditor, because a recovery that no longer owns
669+
// the edit must not steer the live one either.
670+
return false
671+
}
672+
this.activeDiffEditor = acquiredEditor
642673
return this.revertChanges()
643674
}
644675

‎src/integrations/editor/__tests__/DiffViewProvider.spec.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,6 +2606,85 @@ describe("DiffViewProvider", () => {
26062606
expect(restorePreStreamBuffer).toHaveBeenCalledWith(absolutePath, editor.document, "on-disk original")
26072607
expect(saveBufferClean).toHaveBeenCalledWith(absolutePath, editor.document)
26082608
})
2609+
2610+
it("stops when a retry takes the provider for another edit while the buffer is being acquired", async () => {
2611+
const relPath = "mock-recovery-target.ts"
2612+
const otherRelPath = "another-target.ts"
2613+
const editor = simulateReset()
2614+
const restorePreStreamBuffer = vi.fn().mockResolvedValue(undefined)
2615+
const saveBufferClean = vi.fn().mockResolvedValue(undefined)
2616+
const keepOrCloseEditedFile = vi.fn().mockResolvedValue(undefined)
2617+
diffViewProvider["restorePreStreamBuffer"] = restorePreStreamBuffer
2618+
diffViewProvider["saveBufferClean"] = saveBufferClean
2619+
diffViewProvider["keepOrCloseEditedFile"] = keepOrCloseEditedFile
2620+
// The acquisition is the await this recovery performs, and the next attempt does not wait for it:
2621+
// a refused rollback reports, the tool keeps the captured context across the reset, and the
2622+
// retry opens a different target on this same provider while this recovery is still waiting.
2623+
// open() overwrites exactly the fields revertChanges() works from, so a recovery that finished
2624+
// here unchecked would restore and save the other edit's content into the other edit's target.
2625+
diffViewProvider["openDiffEditor"] = vi.fn(async () => {
2626+
Object.assign(diffViewProvider, {
2627+
isEditing: true,
2628+
relPath: otherRelPath,
2629+
editType: "modify",
2630+
createdDirs: [],
2631+
originalContent: "the other file's own original",
2632+
})
2633+
return editor
2634+
})
2635+
2636+
// The caller keeps its refusal on a false return, which is the only honest outcome here:
2637+
// no rollback of this edit ran.
2638+
await expect(diffViewProvider.attemptRollbackRecovery(recoveryContextFor(relPath, "modify"))).resolves.toBe(
2639+
false,
2640+
)
2641+
2642+
// Nothing was restored and nothing was saved for either path.
2643+
expect(restorePreStreamBuffer).not.toHaveBeenCalled()
2644+
expect(saveBufferClean).not.toHaveBeenCalled()
2645+
expect(keepOrCloseEditedFile).not.toHaveBeenCalled()
2646+
// The live edit keeps the provider it just took: its own target, its own baseline, and the
2647+
// editor slot left for its own flow to fill.
2648+
expect(diffViewProvider["isEditing"]).toBe(true)
2649+
expect(diffViewProvider["relPath"]).toBe(otherRelPath)
2650+
expect(diffViewProvider["editType"]).toBe("modify")
2651+
expect(diffViewProvider["originalContent"]).toBe("the other file's own original")
2652+
expect(diffViewProvider["activeDiffEditor"]).toBeUndefined()
2653+
})
2654+
2655+
it("stops when the provider is cleared again while its editor is being acquired", async () => {
2656+
const relPath = "mock-recovery-target.ts"
2657+
const editor = simulateReset()
2658+
const restorePreStreamBuffer = vi.fn().mockResolvedValue(undefined)
2659+
const saveBufferClean = vi.fn().mockResolvedValue(undefined)
2660+
diffViewProvider["restorePreStreamBuffer"] = restorePreStreamBuffer
2661+
diffViewProvider["saveBufferClean"] = saveBufferClean
2662+
diffViewProvider["keepOrCloseEditedFile"] = vi.fn().mockResolvedValue(undefined)
2663+
// The other way ownership goes away: something resets the provider out from under the recovery,
2664+
// so the install this recovery made is gone by the time the acquisition settles and the rollback
2665+
// would read a relPath and a baseline that belong to no edit at all.
2666+
diffViewProvider["openDiffEditor"] = vi.fn(async () => {
2667+
Object.assign(diffViewProvider, {
2668+
isEditing: false,
2669+
relPath: undefined,
2670+
editType: undefined,
2671+
createdDirs: [],
2672+
originalContent: undefined,
2673+
})
2674+
return editor
2675+
})
2676+
2677+
await expect(diffViewProvider.attemptRollbackRecovery(recoveryContextFor(relPath, "modify"))).resolves.toBe(
2678+
false,
2679+
)
2680+
2681+
expect(restorePreStreamBuffer).not.toHaveBeenCalled()
2682+
expect(saveBufferClean).not.toHaveBeenCalled()
2683+
expect(diffViewProvider["isEditing"]).toBe(false)
2684+
// The editor this recovery managed to acquire is dropped rather than installed: a recovery that
2685+
// no longer owns the edit does not steer the provider with it either.
2686+
expect(diffViewProvider["activeDiffEditor"]).toBeUndefined()
2687+
})
26092688
})
26102689
// The acquisition inside openDiffEditor() races a ten-second timer against a showTextDocument it cannot
26112690
// cancel. Both cases below are about what that leaves behind: a diff nobody is waiting for, and a diff

0 commit comments

Comments
 (0)