Skip to content

Commit

Permalink
fix: abbreviation rewriter disposal race condition (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhuisi authored Jul 1, 2024
1 parent a6bd9cf commit 3a4e4bb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 8 additions & 4 deletions vscode-lean4/src/abbreviation/AbbreviationRewriterFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ export class AbbreviationRewriterFeature {
}

private async disposeActiveAbbreviationRewriter() {
if (this.activeAbbreviationRewriter === undefined) {
// This is necessary to prevent `disposeActiveAbbreviationRewriter` from racing with
// other assignments to `this.activeAbbreviationRewriter`.
const abbreviationRewriterToDispose = this.activeAbbreviationRewriter
this.activeAbbreviationRewriter = undefined
if (abbreviationRewriterToDispose === undefined) {
return
}
await this.activeAbbreviationRewriter.replaceAllTrackedAbbreviations()
this.activeAbbreviationRewriter.dispose()
this.activeAbbreviationRewriter = undefined

await abbreviationRewriterToDispose.replaceAllTrackedAbbreviations()
abbreviationRewriterToDispose.dispose()
}

private async changedActiveTextEditor(activeTextEditor: TextEditor | undefined) {
Expand Down
6 changes: 5 additions & 1 deletion vscode-lean4/src/abbreviation/VSCodeAbbreviationRewriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ export class VSCodeAbbreviationRewriter implements AbbreviationTextSource {
retries++
}
} catch (e) {
this.writeError('Error while replacing abbreviation: ' + e)
// The 'not possible on closed editors' error naturally occurs when we attempt to replace abbreviations as the user
// is switching away from the active tab.
if (!(e instanceof Error) || e.message !== 'TextEditor#edit not possible on closed editors') {
this.writeError('Error while replacing abbreviation: ' + e)
}
}
return ok
}
Expand Down

0 comments on commit 3a4e4bb

Please sign in to comment.