diff --git a/src/vs/workbench/services/textfile/common/textFileEditorModel.ts b/src/vs/workbench/services/textfile/common/textFileEditorModel.ts index 8106f9cf4837d1af9c2d120f313505b8d401b5ed..a625dbdba080f632dc7bcb03189fe99f2b816171 100644 --- a/src/vs/workbench/services/textfile/common/textFileEditorModel.ts +++ b/src/vs/workbench/services/textfile/common/textFileEditorModel.ts @@ -693,6 +693,16 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil // mark the save participant as current pending save operation return this.saveSequentializer.setPending(versionId, saveParticipantPromise.then(newVersionId => { + // We have to protect against being disposed at this point. It could be that the save() operation + // was triggerd followed by a dispose() operation right after without waiting. Typically we cannot + // be disposed if we are dirty, but if we are not dirty, save() and dispose() can still be triggered + // one after the other without waiting for the save() to complete. If we are disposed(), we risk + // saving contents to disk that are stale (see https://github.com/Microsoft/vscode/issues/50942). + // To fix this issue, we will not store the contents to disk when we got disposed. + if (this.disposed) { + return void 0; + } + // Under certain conditions we do a short-cut of flushing contents to disk when we can assume that // the file has not changed and as such was not dirty before. // The conditions are all of: