Skip to content

Commit 34ca550

Browse files
Backport PR #19144 on branch 4.6.x (Fix stale document load after rename during initialization) (#19163)
Backport PR #19144: Fix stale document load after rename during initialization Co-authored-by: ANUJ KUMAR SINGH <[email protected]>
1 parent 172a7f6 commit 34ca550

2 files changed

Lines changed: 100 additions & 1 deletion

File tree

‎packages/docregistry/src/context.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,9 @@ export class Context<
693693
if (this.isDisposed) {
694694
return;
695695
}
696+
if (path !== this._path) {
697+
return this._revert(initializeModel);
698+
}
696699
if (contents.content) {
697700
if (contents.format === 'json') {
698701
model.fromJSON(contents.content);
@@ -719,6 +722,9 @@ export class Context<
719722
}
720723
})
721724
.catch(async err => {
725+
if (!this.isDisposed && path !== this._path) {
726+
return this._revert(initializeModel);
727+
}
722728
const localPath = this._manager.contents.localPath(this._path);
723729
const name = PathExt.basename(localPath);
724730
void this._handleError(

‎packages/docregistry/test/context.spec.ts‎

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
waitForDialog
1515
} from '@jupyterlab/testing';
1616
import { ServiceManagerMock } from '@jupyterlab/services/lib/testutils';
17-
import { UUID } from '@lumino/coreutils';
17+
import { PromiseDelegate, UUID } from '@lumino/coreutils';
1818
import { Widget } from '@lumino/widgets';
1919

2020
describe('docregistry/context', () => {
@@ -295,6 +295,99 @@ describe('docregistry/context', () => {
295295
await context.initialize(false);
296296
await expect(context.ready).resolves.not.toThrow();
297297
});
298+
299+
it('should resolve if the file is renamed while it is loading', async () => {
300+
const oldPath = context.path;
301+
const newPath = UUID.uuid4() + '.txt';
302+
await manager.contents.save(oldPath, {
303+
type: factory.contentType,
304+
format: factory.fileFormat,
305+
content: 'foo'
306+
});
307+
308+
const get = manager.contents.get as jest.MockedFunction<
309+
typeof manager.contents.get
310+
>;
311+
const getImplementation = get.getMockImplementation();
312+
if (!getImplementation) {
313+
throw new Error('Missing contents get mock implementation');
314+
}
315+
316+
const started = new PromiseDelegate<void>();
317+
const delayedGet = new PromiseDelegate<Contents.IModel>();
318+
let isFirstGet = true;
319+
get.mockImplementation((path, options) => {
320+
if (path === oldPath && isFirstGet) {
321+
isFirstGet = false;
322+
started.resolve();
323+
return delayedGet.promise;
324+
}
325+
return getImplementation(path, options);
326+
});
327+
328+
try {
329+
const initialized = context.initialize(false);
330+
await started.promise;
331+
await manager.contents.rename(oldPath, newPath);
332+
delayedGet.reject(new Error('Stale path'));
333+
334+
await expect(initialized).resolves.not.toThrow();
335+
await expect(context.ready).resolves.not.toThrow();
336+
expect(context.path).toBe(newPath);
337+
expect(context.contentsModel!.path).toBe(newPath);
338+
expect(context.model.toString()).toBe('foo');
339+
expect(
340+
document.body.getElementsByClassName('jp-Dialog')
341+
).toHaveLength(0);
342+
} finally {
343+
get.mockImplementation(getImplementation);
344+
}
345+
});
346+
347+
it('should ignore stale contents if the file is renamed while it is loading', async () => {
348+
const oldPath = context.path;
349+
const newPath = UUID.uuid4() + '.txt';
350+
const staleContents = await manager.contents.save(oldPath, {
351+
type: factory.contentType,
352+
format: factory.fileFormat,
353+
content: 'foo'
354+
});
355+
356+
const get = manager.contents.get as jest.MockedFunction<
357+
typeof manager.contents.get
358+
>;
359+
const getImplementation = get.getMockImplementation();
360+
if (!getImplementation) {
361+
throw new Error('Missing contents get mock implementation');
362+
}
363+
364+
const started = new PromiseDelegate<void>();
365+
const delayedGet = new PromiseDelegate<Contents.IModel>();
366+
let isFirstGet = true;
367+
get.mockImplementation((path, options) => {
368+
if (path === oldPath && isFirstGet) {
369+
isFirstGet = false;
370+
started.resolve();
371+
return delayedGet.promise;
372+
}
373+
return getImplementation(path, options);
374+
});
375+
376+
try {
377+
const initialized = context.initialize(false);
378+
await started.promise;
379+
await manager.contents.rename(oldPath, newPath);
380+
delayedGet.resolve(staleContents);
381+
382+
await expect(initialized).resolves.not.toThrow();
383+
await expect(context.ready).resolves.not.toThrow();
384+
expect(context.path).toBe(newPath);
385+
expect(context.contentsModel!.path).toBe(newPath);
386+
expect(context.model.toString()).toBe('foo');
387+
} finally {
388+
get.mockImplementation(getImplementation);
389+
}
390+
});
298391
});
299392

300393
describe('#disposed', () => {

0 commit comments

Comments
 (0)