fix: close the client after the warden is done because ServeHTTP is a blocking call#66
fix: close the client after the warden is done because ServeHTTP is a blocking call#66
Conversation
WalkthroughConsolidates Bitwarden client resource cleanup by moving the defer close operation from individual request handlers in the server to the middleware layer after successful login, centralizing lifecycle management. Changes
🚥 Pre-merge checks | ❌ 2❌ Failed checks (2 warnings)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. 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 |
… blocking call Signed-off-by: Gergely Brautigam <182850+Skarlso@users.noreply.github.com>
43c2f01 to
f87d5c9
Compare
|
@coderabbitai please explain how that would result in a new leak if these handlers are getting their own context that is constructed with the handlers where the Warden is the first handler in the chain that creates the client. Once the chain is done, the defer Close will close the client in the handler, no matter the outcome of the request. |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: Length of output: 4094 🏁 Script executed: Length of output: 5923 🏁 Script executed: Length of output: 394
Middleware Execution FlowWhy This Works
Why Removing Individual Defers is CorrectPreviously, if handlers had
Now with your PR:
This is the correct pattern for middleware-managed resources! The client lifecycle is now properly centralized in the Warden middleware, which is exactly where it should be. |
Problem Statement
In case of errors the client constructed by the Warden is actually never closed properly. So we defer close it in the Login since Next is a blocking call, this should be okay.
Related Issue
Fixes #65
Proposed Changes
How do you like to solve the issue and why?
Checklist
git commit --signoffmake testmake reviewableFix memory leak in Bitwarden client lifecycle
Problem: The Bitwarden client created during login was never closed, causing memory to grow indefinitely with each request.
Solution: Move client cleanup from individual request handlers to the login middleware by:
defer client.Close()in the Warden middleware after successful client creationdefer c.Close()calls from individual handlers (getSecretHandler, getByIdsSecretHandler, listSecretsHandler, deleteSecretHandler, createSecretHandler, updateSecretHandler)This ensures the client is properly closed after all downstream handlers complete, with the middleware-level defer being acceptable since ServeHTTP/Next is a blocking call.
Changes:
pkg/bitwarden/bitwarden.go: +1 line (added defer)pkg/server/server.go: -6 lines (removed redundant defers)