Skip to content

Commit 6be4ae4

Browse files
committed
This reworks the previous restoration race fix to make the logic more natural
Instead of just an initial state restore, this makes it so that we never ask for a kernel restore when another kernel restore is already in progress. A subcase of this is the initial restore race logic from the previous commit.
1 parent e2b7ecb commit 6be4ae4

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

packages/jupyterlab-manager/src/manager.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ export abstract class LabWidgetManager extends ManagerBase<Widget>
444444

445445
protected _restored = new Signal<this, void>(this);
446446
protected _restoredStatus = false;
447-
protected _initialRestoredStatus = false;
447+
protected _kernelRestoreInProgress = false;
448448

449449
private _isDisposed = false;
450450
private _registry: SemVerCache<ExportData> = new SemVerCache<ExportData>();
@@ -487,9 +487,9 @@ export class KernelWidgetManager extends LabWidgetManager {
487487

488488
_handleKernelConnectionStatusChange(status: Kernel.ConnectionStatus): void {
489489
if (status === 'connected') {
490-
// Only restore if our initial restore at construction is finished
491-
if (this._initialRestoredStatus) {
492-
// We only want to restore widgets from the kernel, not ones saved in the notebook.
490+
// Only restore if we aren't currently trying to restore from the kernel
491+
// (for example, in our initial restore from the constructor).
492+
if (!this._kernelRestoreInProgress) {
493493
this.restoreWidgets();
494494
}
495495
}
@@ -506,13 +506,14 @@ export class KernelWidgetManager extends LabWidgetManager {
506506
*/
507507
async restoreWidgets(): Promise<void> {
508508
try {
509+
this._kernelRestoreInProgress = true;
509510
await this._loadFromKernel();
510511
this._restoredStatus = true;
511-
this._initialRestoredStatus = true;
512512
this._restored.emit();
513513
} catch (err) {
514514
// Do nothing
515515
}
516+
this._kernelRestoreInProgress = false;
516517
}
517518

518519
/**
@@ -588,8 +589,9 @@ export class WidgetManager extends LabWidgetManager {
588589

589590
_handleKernelConnectionStatusChange(status: Kernel.ConnectionStatus): void {
590591
if (status === 'connected') {
591-
// Only restore if our initial restore at construction is finished
592-
if (this._initialRestoredStatus) {
592+
// Only restore if we aren't currently trying to restore from the kernel
593+
// (for example, in our initial restore from the constructor).
594+
if (!this._kernelRestoreInProgress) {
593595
// We only want to restore widgets from the kernel, not ones saved in the notebook.
594596
this.restoreWidgets(this._context!.model, {
595597
loadKernel: true,
@@ -614,7 +616,12 @@ export class WidgetManager extends LabWidgetManager {
614616
): Promise<void> {
615617
try {
616618
if (loadKernel) {
617-
await this._loadFromKernel();
619+
try {
620+
this._kernelRestoreInProgress = true;
621+
await this._loadFromKernel();
622+
} finally {
623+
this._kernelRestoreInProgress = false;
624+
}
618625
}
619626
if (loadNotebook) {
620627
await this._loadFromNotebook(notebook);
@@ -626,9 +633,6 @@ export class WidgetManager extends LabWidgetManager {
626633
} catch (err) {
627634
// Do nothing if the restore did not work.
628635
}
629-
// We *always* claim that the initial restore happened, so when we connect
630-
// to a kernel, we will initiate another restore. Maybe this should really be a "restore in progress" variable?
631-
this._initialRestoredStatus = true;
632636
}
633637

634638
/**

0 commit comments

Comments
 (0)