Skip to content

Commit 185c2f2

Browse files
authored
Candidate: Fix wh model leak (microsoft#230735)
* fix leak -- dispose model ref * dispose in finally * import
1 parent fb1b5c6 commit 185c2f2

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

src/vs/editor/contrib/wordHighlighter/browser/wordHighlighter.ts

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { URI } from '../../../../base/common/uri.js';
1717
import { IContextKey, IContextKeyService, RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';
1818
import { ServicesAccessor } from '../../../../platform/instantiation/common/instantiation.js';
1919
import { KeybindingWeight } from '../../../../platform/keybinding/common/keybindingsRegistry.js';
20+
import { ILogService } from '../../../../platform/log/common/log.js';
2021
import { IActiveCodeEditor, ICodeEditor, isDiffEditor } from '../../../browser/editorBrowser.js';
2122
import { EditorAction, EditorContributionInstantiation, IActionOptions, registerEditorAction, registerEditorContribution, registerModelAndPositionCommand } from '../../../browser/editorExtensions.js';
2223
import { ICodeEditorService } from '../../../browser/services/codeEditorService.js';
@@ -211,6 +212,7 @@ class WordHighlighter {
211212

212213
private readonly textModelService: ITextModelService;
213214
private readonly codeEditorService: ICodeEditorService;
215+
private readonly logService: ILogService;
214216

215217
private occurrencesHighlight: string;
216218

@@ -237,13 +239,15 @@ class WordHighlighter {
237239
contextKeyService: IContextKeyService,
238240
@ITextModelService textModelService: ITextModelService,
239241
@ICodeEditorService codeEditorService: ICodeEditorService,
242+
@ILogService logService: ILogService,
240243
) {
241244
this.editor = editor;
242245
this.providers = providers;
243246
this.multiDocumentProviders = multiProviders;
244247

245248
this.codeEditorService = codeEditorService;
246249
this.textModelService = textModelService;
250+
this.logService = logService;
247251

248252
this._hasWordHighlights = ctxHasWordHighlights.bindTo(contextKeyService);
249253
this._ignorePositionChangeEvent = false;
@@ -685,17 +689,23 @@ class WordHighlighter {
685689
if (!WordHighlighter.query || !WordHighlighter.query.modelInfo) {
686690
return;
687691
}
692+
688693
const queryModelRef = await this.textModelService.createModelReference(WordHighlighter.query.modelInfo.modelURI);
689-
const queryModel = queryModelRef.object.textEditorModel;
690-
this.workerRequest = this.computeWithModel(queryModel, WordHighlighter.query.modelInfo.selection, otherModelsToHighlight);
691-
692-
this.workerRequest?.result.then(data => {
693-
if (myRequestId === this.workerRequestTokenId) {
694-
this.workerRequestCompleted = true;
695-
this.workerRequestValue = data || [];
696-
this._beginRenderDecorations();
697-
}
698-
}, onUnexpectedError);
694+
try {
695+
this.workerRequest = this.computeWithModel(queryModelRef.object.textEditorModel, WordHighlighter.query.modelInfo.selection, otherModelsToHighlight);
696+
this.workerRequest?.result.then(data => {
697+
if (myRequestId === this.workerRequestTokenId) {
698+
this.workerRequestCompleted = true;
699+
this.workerRequestValue = data || [];
700+
this._beginRenderDecorations();
701+
}
702+
}, onUnexpectedError);
703+
} catch (e) {
704+
this.logService.error('Unexpected error during occurrence request. Log: ', e);
705+
} finally {
706+
queryModelRef.dispose();
707+
}
708+
699709
} else if (this.model.uri.scheme === Schemas.vscodeNotebookCell) {
700710
// new wordHighlighter coming from a different model, NOT the query model, need to create a textModel ref
701711

@@ -709,16 +719,20 @@ class WordHighlighter {
709719
}
710720

711721
const queryModelRef = await this.textModelService.createModelReference(WordHighlighter.query.modelInfo.modelURI);
712-
const queryModel = queryModelRef.object.textEditorModel;
713-
this.workerRequest = this.computeWithModel(queryModel, WordHighlighter.query.modelInfo.selection, [this.model]);
714-
715-
this.workerRequest?.result.then(data => {
716-
if (myRequestId === this.workerRequestTokenId) {
717-
this.workerRequestCompleted = true;
718-
this.workerRequestValue = data || [];
719-
this._beginRenderDecorations(noDelay);
720-
}
721-
}, onUnexpectedError);
722+
try {
723+
this.workerRequest = this.computeWithModel(queryModelRef.object.textEditorModel, WordHighlighter.query.modelInfo.selection, [this.model]);
724+
this.workerRequest?.result.then(data => {
725+
if (myRequestId === this.workerRequestTokenId) {
726+
this.workerRequestCompleted = true;
727+
this.workerRequestValue = data || [];
728+
this._beginRenderDecorations(noDelay);
729+
}
730+
}, onUnexpectedError);
731+
} catch (e) {
732+
this.logService.error('Unexpected error during occurrence request. Log: ', e);
733+
} finally {
734+
queryModelRef.dispose();
735+
}
722736
}
723737
}
724738

@@ -814,12 +828,13 @@ export class WordHighlighterContribution extends Disposable implements IEditorCo
814828
@ILanguageFeaturesService languageFeaturesService: ILanguageFeaturesService,
815829
@ICodeEditorService codeEditorService: ICodeEditorService,
816830
@ITextModelService textModelService: ITextModelService,
831+
@ILogService logService: ILogService,
817832
) {
818833
super();
819834
this._wordHighlighter = null;
820835
const createWordHighlighterIfPossible = () => {
821836
if (editor.hasModel() && !editor.getModel().isTooLargeForTokenization()) {
822-
this._wordHighlighter = new WordHighlighter(editor, languageFeaturesService.documentHighlightProvider, languageFeaturesService.multiDocumentHighlightProvider, contextKeyService, textModelService, codeEditorService);
837+
this._wordHighlighter = new WordHighlighter(editor, languageFeaturesService.documentHighlightProvider, languageFeaturesService.multiDocumentHighlightProvider, contextKeyService, textModelService, codeEditorService, logService);
823838
}
824839
};
825840
this._register(editor.onDidChangeModel((e) => {

0 commit comments

Comments
 (0)