Skip to content

Commit 8448029

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 29ace49 commit 8448029

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) => {
@@ -2994,19 +2999,23 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
29942999
}
29953000

29963001
const project = await this.projectsService.createProject(params, user);
3002+
29973003
// update client registration for the logged in user
2998-
const prebuildUpdateHandler = (ctx: TraceContext, update: PrebuildWithStatus) =>
2999-
TraceContext.withSpan(
3000-
"forwardPrebuildUpdateToClient",
3001-
(ctx) => {
3002-
traceClientMetadata(ctx, this.clientMetadata);
3003-
TraceContext.setJsonRPCMetadata(ctx, "onPrebuildUpdate");
3004+
if (this.client && !this.disposables.disposed) {
3005+
const prebuildUpdateHandler = (ctx: TraceContext, update: PrebuildWithStatus) =>
3006+
TraceContext.withSpan(
3007+
"forwardPrebuildUpdateToClient",
3008+
(ctx) => {
3009+
traceClientMetadata(ctx, this.clientMetadata);
3010+
TraceContext.setJsonRPCMetadata(ctx, "onPrebuildUpdate");
3011+
3012+
this.client?.onPrebuildUpdate(update);
3013+
},
3014+
ctx,
3015+
);
30043016

3005-
this.client?.onPrebuildUpdate(update);
3006-
},
3007-
ctx,
3008-
);
3009-
this.disposables.pushAll([this.subscriber.listenForPrebuildUpdates(project.id, prebuildUpdateHandler)]);
3017+
this.disposables.pushAll([this.subscriber.listenForPrebuildUpdates(project.id, prebuildUpdateHandler)]);
3018+
}
30103019

30113020
return project;
30123021
}

0 commit comments

Comments
 (0)