Skip to content

Commit 1f512e9

Browse files
committed
[server] fix leaking prebuild update listeners
- check for client be defined in gitpod-server-impl - add prebuild subscribers only if the thing is not disposed yet - this might have happened frequently on very short living workspacePageClose events
1 parent 604febe commit 1f512e9

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

components/server/src/workspace/gitpod-server-impl.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,13 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
308308
log.debug({ userId: this.userID }, "initializeClient");
309309

310310
this.listenForWorkspaceInstanceUpdates();
311-
312311
this.listenForPrebuildUpdates().catch((err) => log.error("error registering for prebuild updates", err));
313312
}
314313

315314
private async listenForPrebuildUpdates() {
315+
if (!this.client) {
316+
return;
317+
}
316318
// 'registering for prebuild updates for all projects this user has access to
317319
const projects = await this.getAccessibleProjects();
318320

@@ -328,8 +330,10 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
328330
ctx,
329331
);
330332

331-
for (const projectId of projects) {
332-
this.disposables.pushAll([this.subscriber.listenForPrebuildUpdates(projectId, handler)]);
333+
if (!this.disposables.disposed) {
334+
for (const projectId of projects) {
335+
this.disposables.push(this.subscriber.listenForPrebuildUpdates(projectId, handler));
336+
}
333337
}
334338

335339
// TODO(at) we need to keep the list of accessible project up to date
@@ -451,7 +455,8 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
451455
// Once we have those, we should remove this.
452456
//
453457
const ws = await this.workspaceDb.trace(ctx).findById(workspaceID);
454-
if (!!ws && !!wsi && ws.ownerId !== this.userID) {
458+
const relatedPrebuildFound = !!ws && !!wsi && ws.ownerId !== this.userID;
459+
if (relatedPrebuildFound && !this.disposables.disposed) {
455460
const resetListenerFromRedis = this.subscriber.listenForWorkspaceInstanceUpdates(
456461
ws.ownerId,
457462
(ctx, instance) => {
@@ -2968,19 +2973,23 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
29682973
}
29692974

29702975
const project = await this.projectsService.createProject(params, user);
2976+
29712977
// update client registration for the logged in user
2972-
const prebuildUpdateHandler = (ctx: TraceContext, update: PrebuildWithStatus) =>
2973-
TraceContext.withSpan(
2974-
"forwardPrebuildUpdateToClient",
2975-
(ctx) => {
2976-
traceClientMetadata(ctx, this.clientMetadata);
2977-
TraceContext.setJsonRPCMetadata(ctx, "onPrebuildUpdate");
2978+
if (this.client && !this.disposables.disposed) {
2979+
const prebuildUpdateHandler = (ctx: TraceContext, update: PrebuildWithStatus) =>
2980+
TraceContext.withSpan(
2981+
"forwardPrebuildUpdateToClient",
2982+
(ctx) => {
2983+
traceClientMetadata(ctx, this.clientMetadata);
2984+
TraceContext.setJsonRPCMetadata(ctx, "onPrebuildUpdate");
2985+
2986+
this.client?.onPrebuildUpdate(update);
2987+
},
2988+
ctx,
2989+
);
29782990

2979-
this.client?.onPrebuildUpdate(update);
2980-
},
2981-
ctx,
2982-
);
2983-
this.disposables.pushAll([this.subscriber.listenForPrebuildUpdates(project.id, prebuildUpdateHandler)]);
2991+
this.disposables.pushAll([this.subscriber.listenForPrebuildUpdates(project.id, prebuildUpdateHandler)]);
2992+
}
29842993

29852994
return project;
29862995
}

0 commit comments

Comments
 (0)