Skip to content

Commit 436130b

Browse files
authored
Fix listeners leaks – EXP-206 (#18321)
* [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 * [server] fix metric registration don't override default registry, which breaks other metrics. * fixup for getMetricsAsArray * [server] bump prom-client
1 parent 8a529c1 commit 436130b

File tree

7 files changed

+42
-22
lines changed

7 files changed

+42
-22
lines changed

components/gitpod-db/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"typeorm": "0.2.38",
3434
"ioredis": "^5.3.2",
3535
"ioredis-mock": "^8.7.0",
36-
"prom-client": "^13.2.0"
36+
"prom-client": "^14.2.0"
3737
},
3838
"devDependencies": {
3939
"@testdeck/mocha": "^0.3.3",

components/gitpod-protocol/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"nice-grpc-common": "^2.0.0",
6161
"opentracing": "^0.14.5",
6262
"parse-duration": "^1.0.3",
63-
"prom-client": "^13.2.0",
63+
"prom-client": "^14.2.0",
6464
"random-number-csprng": "^1.0.2",
6565
"react": "17.0.2",
6666
"react-dom": "17.0.2",

components/server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
"passport-gitlab2": "5.0.0",
8484
"passport-http": "^0.3.0",
8585
"probot": "12.1.1",
86-
"prom-client": "^13.2.0",
86+
"prom-client": "^14.2.0",
8787
"rate-limiter-flexible": "^2.3.6",
8888
"redlock": "^5.0.0-beta.2",
8989
"reflect-metadata": "^0.1.10",

components/server/src/monitoring-endpoints.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ import { registerServerMetrics } from "./prometheus-metrics";
1313
@injectable()
1414
export class MonitoringEndpointsApp {
1515
public create(): express.Application {
16-
let registry = prometheusClient.register;
16+
const registry = prometheusClient.register;
1717

1818
prometheusClient.collectDefaultMetrics({ register: registry });
1919
registerDBMetrics(registry);
2020
registerServerMetrics(registry);
21-
registry = prometheusClient.Registry.merge([registry, redisMetricsRegistry()]);
21+
22+
// Append redis metrics to default registry
23+
redisMetricsRegistry()
24+
.getMetricsAsArray()
25+
.forEach((metric) => registry.registerMetric(metric as any));
2226

2327
const monApp = express();
2428
monApp.get("/metrics", async (req, res) => {

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
}

components/ws-manager-bridge/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"express": "^4.17.3",
3232
"inversify": "^6.0.1",
3333
"ioredis": "^5.3.2",
34-
"prom-client": "^13.2.0",
34+
"prom-client": "^14.2.0",
3535
"reflect-metadata": "^0.1.13"
3636
},
3737
"devDependencies": {

yarn.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13609,7 +13609,14 @@ progress@^2.0.0:
1360913609
resolved "https://registry.npmjs.org/progress/-/progress-2.0.3.tgz"
1361013610
integrity sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA==
1361113611

13612-
prom-client@^13.2.0, "prom-client@~11.3.0 || ^12.0.0 || ^13.0.0":
13612+
prom-client@^14.2.0:
13613+
version "14.2.0"
13614+
resolved "https://registry.yarnpkg.com/prom-client/-/prom-client-14.2.0.tgz#ca94504e64156f6506574c25fb1c34df7812cf11"
13615+
integrity sha512-sF308EhTenb/pDRPakm+WgiN+VdM/T1RaHj1x+MvAuT8UiQP8JmOEbxVqtkbfR4LrvOg5n7ic01kRBDGXjYikA==
13616+
dependencies:
13617+
tdigest "^0.1.1"
13618+
13619+
"prom-client@~11.3.0 || ^12.0.0 || ^13.0.0":
1361313620
version "13.2.0"
1361413621
resolved "https://registry.npmjs.org/prom-client/-/prom-client-13.2.0.tgz"
1361513622
integrity sha512-wGr5mlNNdRNzEhRYXgboUU2LxHWIojxscJKmtG3R8f4/KiWqyYgXTLHs0+Ted7tG3zFT7pgHJbtomzZ1L0ARaQ==

0 commit comments

Comments
 (0)