Skip to content

Investigate memory leaks in Cloudflare #19475

@JPeer264

Description

@JPeer264

Description

There are quite some memory leaks in Cloudflare. One of the biggest one is that the CloudflareClient can't be garbage collected and stays in memory forever, this is actually working as expected, as usually we only have one client and it is usually never removed.

In Cloudflare Workers we actually don't know how long isolates live. So when a lot of requests come in rather fast the request is actually done, but the client is still active, which takes some memory - this could lead to OOM of the worker, as it is limited currently to 128MB per worker.

Possible solution

We don't know how long an isolate will live, but we do now how long a request takes (as we can hook into all waitUntil). As an isolate can't be reused it is guaranteed that a new request will spawn a new isolate and therefore a new client. With that it is safe to assume that we can mark an client as "disposed" so it can be garbage collected.

Repro

https://github.com/JPeer264/temp-cloudflare-leak

There is a reproduction showing the memory leak over time. There is also an experimental fix applied, which disposes clients.

Metadata

Metadata

Assignees

Labels

Package: cloudflareIssues related to the Sentry Cloudflare Workers SDK

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions