You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@answer.apache.org by "hhc7 (via GitHub)" <gi...@apache.org> on 2026/05/28 10:00:38 UTC

[I] The Gin engine is initialized without a recovery middleware, causing connection drops on panic [answer]

hhc7 opened a new issue, #1536:
URL: https://github.com/apache/answer/issues/1536

   ## Describe the bug
   
   In `internal/base/server/http.go`, the Gin engine is initialized via `gin.New()` without mounting a recovery middleware. When a panic occurs in any handler or middleware, the connection is dropped without returning a proper HTTP response to the client.
   
   ## Current behavior
   
   `gin.New()` (unlike `gin.Default()`) does not install any recovery middleware. When a panic propagates out of a handler:
   
   - The Go runtime's `net/http` server recovers per-connection, preventing the process from exiting.
   - But it closes the connection without writing any response body.
   - The client sees a connection reset / EOF rather than a structured 5xx response.
   - The panic is not logged through the project's logging facility (`pacman/log`).
   
   ## To Reproduce
   
   This is a defensive coding issue rather than a directly triggerable bug. To verify the behavior, one can temporarily add `panic("test")` to any controller handler and observe that the client receives a connection error instead of an HTTP response.
   
   ## Expected behavior
   
   The server should return a unified 500 JSON response with `reason: base.unknown`, consistent with how other errors are handled in the project (see `internal/base/handler/handler.go`). The panic message and stack trace should be logged via the project's logger for diagnostics.
   
   ## Additional context
   
   I'd like to submit a PR to fix this by adding a `middleware.Recovery()` that:
   
   - Logs the panic message and full stack trace via `log.Errorf` + `debug.Stack()`.
   - Returns the standard error response using `handler.NewRespBody` + `TrMsg` to stay consistent with the rest of the codebase.
   - Is mounted as the first middleware in `http.go` so it covers all subsequent middleware and handlers.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@answer.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@answer.apache.org
For additional commands, e-mail: issues-help@answer.apache.org


Re: [I] The Gin engine is initialized without a recovery middleware, causing connection drops on panic [answer]

Posted by "LinkinStars (via GitHub) via issues" <is...@answer.apache.org>.
LinkinStars closed issue #1536: The Gin engine is initialized without a recovery middleware, causing connection drops on panic
URL: https://github.com/apache/answer/issues/1536


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@answer.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@answer.apache.org
For additional commands, e-mail: issues-help@answer.apache.org


Re: [I] The Gin engine is initialized without a recovery middleware, causing connection drops on panic [answer]

Posted by "lwc18 (via GitHub) via issues" <is...@answer.apache.org>.
lwc18 commented on issue #1536:
URL: https://github.com/apache/answer/issues/1536#issuecomment-6608840674

   This appears fixed by #1537 (merged) plus the follow-up commit `682811f7` ("fix: scope JSON 500 to API routes, skip rewriting already-flushed responses").
   
   Current `main` mounts the recovery middleware in `internal/base/server/http.go`:
   
   ```go
   r := gin.New()
   r.Use(middleware.Recovery(
       uiConf.APIBaseURL+"/answer/api/v1",
       uiConf.APIBaseURL+"/answer/admin/api",
   ))
   ```
   
   It logs the panic with a stack trace via `pacman/log`, and returns `handler.NewRespBody(http.StatusInternalServerError, reason.UnknownError)` for API routes — falling back to a bare 500 for non-API routes and skipping already-flushed (SSE) responses. `internal/base/middleware/recovery_test.go` covers it.
   
   Suggest closing as completed, unless you see a case still uncovered.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@answer.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@answer.apache.org
For additional commands, e-mail: issues-help@answer.apache.org


Re: [I] The Gin engine is initialized without a recovery middleware, causing connection drops on panic [answer]

Posted by "LinkinStars (via GitHub) via issues" <is...@answer.apache.org>.
LinkinStars commented on issue #1536:
URL: https://github.com/apache/answer/issues/1536#issuecomment-6625339325

   Fix in v2.0.3


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@answer.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@answer.apache.org
For additional commands, e-mail: issues-help@answer.apache.org