Skip to content

timeoutMiddleware drops the handler's panic stack, so logged panics always point at middleware.go #2904

Open

Description

@mansur-akhmadi
  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

When a handler panics behind timeoutMiddleware, the stack that gets logged never shows where the panic actually happened. It always points at the panic(p) line inside timeoutMiddleware itself.

The handler runs in its own goroutine. Its deferred recover() sends only the panic value over panicChan, and the serving goroutine re-panics with panic(p). recoverer then logs debug.Stack(), which is the stack of that re-panic, not of the handler. The original stack is gone by then.

This is why the traces in #2233 and #2268 both end at middleware.go (:392 and :431 in those builds are the panic(p) line) and say nothing about the code that failed. In #2268 the actual cause was eventually found by reading code, and in #2508 the trace was read as a nil snapHeader inside the middleware, which couldn't be reproduced.

To Reproduce

  1. Mount any handler that panics, e.g. panic("boom"), behind recoverer and timeoutMiddleware, the same way NewAPIWithVersion does.
  2. Send a request to it.
  3. Look at the stack field of the logged panic: it has timeoutMiddleware.func... at the top and no frame from the handler.

I have a small test that does this against master (d7955e9) and checks that the logged stack contains the handler's function name. It fails on master.

Expected behavior

The logged stack points at the line in the handler that panicked.

System information

Additional context

A minimal fix: in the handler goroutine, capture debug.Stack() in the deferred recover and send it together with the value (e.g. a small handlerPanic{value, stack} type). recoverer unwraps it and logs the original stack. Happy to open a PR with the test if this approach works for you.

Activity

  1. aniketatgithub commented on Nov 6, 2026

    @aniketatgithub

    I'd like to work on this. The panic stack loss is in timeoutMiddleware's recover and re-panic path; I'll open a PR with a fix and a regression test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions