Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
matt-allan opened this issue Oct 9, 2024 · 0 comments
Open

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

matt-allan opened this issue Oct 9, 2024 · 0 comments

Comments

@matt-allan
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant