Skip to content

Commit

Permalink
fix(notebooks): fix race condition in view registration (#13115)
Browse files Browse the repository at this point in the history
Fixes a race condition that occurred when event handlers for `onDidRegisterNotebookSerializer`
were registered *after* said event was fired.

Signed-off-by: Beniamino Ventura <benia@protonmail.com>
  • Loading branch information
bvenreply committed Nov 29, 2023
1 parent eeb1988 commit 1943c80
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/notebook/src/browser/service/notebook-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,31 @@ export class NotebookService implements Disposable {
if (this.notebookProviders.has(type)) {
return this.notebookProviders.get(type);
}
await Promise.all(this.willUseNotebookSerializerEmitter.fire(type));
const deferred = new Deferred<NotebookProviderInfo | undefined>();
// 20 seconds of timeout
const timeoutDuration = 20_000;
const disposable = this.onDidRegisterNotebookSerializer(viewType => {

// Must declare these variables where they can be captured by the closure
let disposable: Disposable;
// eslint-disable-next-line
let timeout: ReturnType<typeof setTimeout>;

// eslint-disable-next-line
disposable = this.onDidRegisterNotebookSerializer(viewType => {
if (viewType === type) {
clearTimeout(timeout);
disposable.dispose();
deferred.resolve(this.notebookProviders.get(type));
}
});
const timeout = setTimeout(() => {
timeout = setTimeout(() => {
clearTimeout(timeout);
disposable.dispose();
deferred.reject();
deferred.reject(new Error(`Timed out while waiting for notebook serializer for type ${type} to be registered`));
}, timeoutDuration);

await Promise.all(this.willUseNotebookSerializerEmitter.fire(type));

return deferred.promise;
}

Expand Down

0 comments on commit 1943c80

Please sign in to comment.