Issue/502 request context cleanup#503
Conversation
|
Warning Rate limit exceeded
To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe PR implements post-response request-context cleanup by adding a ChangesRequest-Context Lifecycle Cleanup
Sequence DiagramsequenceDiagram
participant App as WebApp Lifecycle
participant Response as Response Handler
participant Request as Request State
App->>Response: send response
Response->>Response: emit headers & body
activate Response
Response-->>App: send complete
deactivate Response
App->>Request: clearMatchedRoute()
Request-->>App: ✓
App->>Request: flush()
Request-->>App: clear headers, body, params
App->>Response: flush()
Response-->>App: clear body, headers, status
Note over App,Response: Request-scoped state cleaned<br/>App safe for next request
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes 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 unit tests (beta)
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 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #503 +/- ##
=========================================
Coverage 90.87% 90.87%
- Complexity 2926 2927 +1
=========================================
Files 255 255
Lines 7703 7708 +5
=========================================
+ Hits 7000 7005 +5
Misses 703 703 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/App/Traits/WebAppTrait.php (1)
142-147:⚠️ Potential issue | 🟠 Major | ⚡ Quick winEnsure cleanup always runs on exception paths.
If
handleCors()or$response->send()throws,cleanupRequestContext()is skipped, which can leak request-scoped state across consecutive in-process requests.Proposed fix
private function sendResponse(Response $response): void { - $this->handleCors($response); - $response->send(); - $this->cleanupRequestContext(); + try { + $this->handleCors($response); + $response->send(); + } finally { + $this->cleanupRequestContext(); + } }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/App/Traits/WebAppTrait.php` around lines 142 - 147, The sendResponse method can skip cleanupRequestContext if handleCors() or Response::send() throws; update sendResponse to ensure cleanupRequestContext() always runs by wrapping the calls to $this->handleCors($response) and $response->send() in a try/finally (or try/catch/finally) so cleanupRequestContext() is invoked in the finally block, and optionally rethrow any caught exception after cleanup.
🧹 Nitpick comments (1)
tests/Unit/App/Adapters/WebAppAdapterTest.php (1)
23-69: ⚡ Quick winAdd one exception-path cleanup test.
Consider adding a test where response sending fails (or CORS setup throws) and then assert request/response context is still cleaned. That locks in the lifecycle guarantee under failure paths too.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/Unit/App/Adapters/WebAppAdapterTest.php` around lines 23 - 69, Add a new test (e.g., testWebAppAdapterCleansUpOnException) that simulates an exception during the adapter lifecycle (for example mock/stub the component that sends the response or the CORS setup to throw when response()->send() or cors()->setup() is invoked) then call $this->webAppAdapter->start() wrapped to catch the exception and finally assert the same cleanup guarantees as the other tests: request() has no matched route and no URI, response()->all() and response()->allHeaders() are empty, and response()->getStatusCode() is 200; this ensures webAppAdapter->start() cleans request/response context even on failure.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@src/App/Traits/WebAppTrait.php`:
- Around line 142-147: The sendResponse method can skip cleanupRequestContext if
handleCors() or Response::send() throws; update sendResponse to ensure
cleanupRequestContext() always runs by wrapping the calls to
$this->handleCors($response) and $response->send() in a try/finally (or
try/catch/finally) so cleanupRequestContext() is invoked in the finally block,
and optionally rethrow any caught exception after cleanup.
---
Nitpick comments:
In `@tests/Unit/App/Adapters/WebAppAdapterTest.php`:
- Around line 23-69: Add a new test (e.g., testWebAppAdapterCleansUpOnException)
that simulates an exception during the adapter lifecycle (for example mock/stub
the component that sends the response or the CORS setup to throw when
response()->send() or cors()->setup() is invoked) then call
$this->webAppAdapter->start() wrapped to catch the exception and finally assert
the same cleanup guarantees as the other tests: request() has no matched route
and no URI, response()->all() and response()->allHeaders() are empty, and
response()->getStatusCode() is 200; this ensures webAppAdapter->start() cleans
request/response context even on failure.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 6c73b4dc-2281-4265-9a07-92732ee58d24
📒 Files selected for processing (4)
src/App/Traits/WebAppTrait.phpsrc/Module/Templates/DemoApi/src/Middlewares/Auth.php.tplsrc/Module/Templates/DemoWeb/src/Middlewares/Auth.php.tpltests/Unit/App/Adapters/WebAppAdapterTest.php
Closes #502
Summary by CodeRabbit
Bug Fixes
Tests