Skip to content

Binding tower layers in wrong order causes a memory leak #695

Open
@matt-allan

Description

@matt-allan

Hello,

This isn't really a bug in sentry-rust and is definitely user error, but I wanted to open an issue in case someone else runs into the same problem.

The problem

I followed the docs to setup sentry-tower with tower-http. The docs have a great example of binding both layers using tower::ServiceBuilder.

The problem is, I am using Axum, which applies layers in the opposite order. I naively copied the Sentry example without re-ordering the layers, like this:

let app = Router::new()
    .route("/", get(handler))
    .layer(sentry_tower::NewSentryLayer::<Request>::new_from_top())
    .layer(sentry_tower::SentryHttpLayer::with_transaction())

Once deployed the server started leaking memory and crashing every 2 days with an OOM error.

Solution

I realized my mistake and re-ordered the middleware, using a ServiceBuilder like Axum suggests:

let app = Router::new()
    .route("/", get(handler))
    .layer(ServiceBuilder::new()
        sentry_tower::NewSentryLayer::<Request>::new_from_top()
        sentry_tower::SentryHttpLayer::with_transaction()
     )

That seems to have fixed the leak. I haven't run a heap profile, but I am guessing it's because we are also using the tracing integration with breadcrumbs and traces and those weren't being disposed of properly.

Suggestions

Again this was user error, so feel free to close this issue. Perhaps the docs could be updated with an additional warning for Axum users?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions