Skip to content

Commit a3733e5

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 72d59cb commit a3733e5

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
@@ -310,11 +310,13 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
310310
log.debug({ userId: this.userID }, "initializeClient");
311311

312312
this.listenForWorkspaceInstanceUpdates();
313-
314313
this.listenForPrebuildUpdates().catch((err) => log.error("error registering for prebuild updates", err));
315314
}
316315

317316
private async listenForPrebuildUpdates() {
317+
if (!this.client) {
318+
return;
319+
}
318320
// 'registering for prebuild updates for all projects this user has access to
319321
const projects = await this.getAccessibleProjects();
320322

@@ -330,8 +332,10 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
330332
ctx,
331333
);
332334

333-
for (const projectId of projects) {
334-
this.disposables.pushAll([this.subscriber.listenForPrebuildUpdates(projectId, handler)]);
335+
if (!this.disposables.disposed) {
336+
for (const projectId of projects) {
337+
this.disposables.push(this.subscriber.listenForPrebuildUpdates(projectId, handler));
338+
}
335339
}
336340

337341
// TODO(at) we need to keep the list of accessible project up to date
@@ -453,7 +457,8 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
453457
// Once we have those, we should remove this.
454458
//
455459
const ws = await this.workspaceDb.trace(ctx).findById(workspaceID);
456-
if (!!ws && !!wsi && ws.ownerId !== this.userID) {
460+
const relatedPrebuildFound = !!ws && !!wsi && ws.ownerId !== this.userID;
461+
if (relatedPrebuildFound && !this.disposables.disposed) {
457462
const resetListenerFromRedis = this.subscriber.listenForWorkspaceInstanceUpdates(
458463
ws.ownerId,
459464
(ctx, instance) => {
@@ -3046,19 +3051,23 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
30463051
}
30473052

30483053
const project = await this.projectsService.createProject(params, user);
3054+
30493055
// update client registration for the logged in user
3050-
const prebuildUpdateHandler = (ctx: TraceContext, update: PrebuildWithStatus) =>
3051-
TraceContext.withSpan(
3052-
"forwardPrebuildUpdateToClient",
3053-
(ctx) => {
3054-
traceClientMetadata(ctx, this.clientMetadata);
3055-
TraceContext.setJsonRPCMetadata(ctx, "onPrebuildUpdate");
3056+
if (this.client && !this.disposables.disposed) {
3057+
const prebuildUpdateHandler = (ctx: TraceContext, update: PrebuildWithStatus) =>
3058+
TraceContext.withSpan(
3059+
"forwardPrebuildUpdateToClient",
3060+
(ctx) => {
3061+
traceClientMetadata(ctx, this.clientMetadata);
3062+
TraceContext.setJsonRPCMetadata(ctx, "onPrebuildUpdate");
3063+
3064+
this.client?.onPrebuildUpdate(update);
3065+
},
3066+
ctx,
3067+
);
30563068

3057-
this.client?.onPrebuildUpdate(update);
3058-
},
3059-
ctx,
3060-
);
3061-
this.disposables.pushAll([this.subscriber.listenForPrebuildUpdates(project.id, prebuildUpdateHandler)]);
3069+
this.disposables.pushAll([this.subscriber.listenForPrebuildUpdates(project.id, prebuildUpdateHandler)]);
3070+
}
30623071

30633072
return project;
30643073
}

0 commit comments

Comments
 (0)