🐛 bug: guard session logger tag against released middleware#4265
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughLogger tag ChangesSession logger + lifecycle
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.12.1)level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies" Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request improves the session middleware by implementing thread-safe access to the session object during log tag registration, which prevents panics when a session is nil or has been released. It also includes a new regression test to ensure stability when using the session logger tag. The review feedback recommends returning an empty string if the session ID is empty to prevent misleading redacted output and suggests adding an assertion to the regression test to verify that the log output is empty.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4265 +/- ##
==========================================
+ Coverage 91.17% 91.19% +0.02%
==========================================
Files 129 129
Lines 12757 12767 +10
==========================================
+ Hits 11631 11643 +12
+ Misses 710 709 -1
+ Partials 416 415 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR hardens the session middleware’s auto-registered ${session-id} logger tag to avoid dereferencing a released session/middleware instance, and adds a regression test for the logger-outside-session middleware ordering.
Changes:
- Updated
${session-id}tag extraction to readm.Sessionunderm.mu.RLock()and return""if the session is already released. - Added
Test_SessionLoggerTagWithOuterLoggerDoesNotPanicto cover the outer-logger ordering that previously could panic.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| middleware/session/middleware.go | Adjusts ${session-id} tag renderer to avoid calling through a released session/middleware instance. |
| middleware/session/middleware_test.go | Adds a regression test for logger mounted outside session middleware. |
|
Addressing comments |
|
@copilot apply changes based on the comments in this thread |
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/b24016f2-47b5-453f-9327-d0f9a40ad69e Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/b24016f2-47b5-453f-9327-d0f9a40ad69e Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/b24016f2-47b5-453f-9327-d0f9a40ad69e Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Applied the requested changes in e2bb09a. The session logger tag now reads an immutable request-scoped session ID instead of a pooled middleware pointer, the session/middleware context values are cleared on release so an outer logger renders an empty |
Motivation
${session-id}logger tag could callm.ID()after the session middleware had clearedm.Sessionand returned the*Middlewareto the pool, which can cause a nil-pointer panic when logger runs after downstream middleware returns.Description
middleware/session/middleware.gothe logger context tag was changed to safely readm.Sessionunderm.mu.RLock()and return an empty string when the session has already been released instead of callingm.ID()directly.Test_SessionLoggerTagWithOuterLoggerDoesNotPanicinmiddleware/session/middleware_test.gothat mountsloggeroutsidesessionand asserts a request completes without panic whenFormat: "${session-id}"is used.