fix: ensure response_buffer cleanup in RegularRequestHandler on cancellation#683
Open
algojogacor wants to merge 1 commit into
Open
Conversation
…llation When a regular (non-streaming) request is cancelled before receiving a response (e.g., client disconnect, timeout), the uid entry in response_buffer was never cleaned up, causing a memory leak. Added a finally block with pop(uid, None), matching the cleanup pattern already used by StreamingRequestHandler.stream_with_cleanup(). Fixes Lightning-AI#452
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #683 +/- ##
===================================
Coverage 85% 85%
===================================
Files 39 39
Lines 3282 3285 +3
===================================
+ Hits 2778 2781 +3
Misses 504 504 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #452
When a regular (non-streaming) request is cancelled before receiving a response — for example, due to client disconnect or timeout — the corresponding
uidentry inresponse_bufferpersists indefinitely, causing unbounded memory growth.Root Cause
RegularRequestHandler.handle_request(server.py, line 328–361) stores theuidinself.server.response_buffer[uid]at line 340, but only removes it on the success path at line 345 (response_buffer.pop(uid)). Ifawait event.wait()at line 342 raises aCancelledError(or any other exception), the entry is never cleaned up because the handler has nofinallyblock.The
StreamingRequestHandler(line 382–415) already avoids this issue viastream_with_cleanup()at line 404–411, which runsresponse_buffer.pop(uid, None)inside afinallyblock.Fix
Added a
finallyblock that conditionally removes the orphaned entry withpop(uid, None)(safe no-op if already popped). This matches the existing cleanup pattern used byStreamingRequestHandler.stream_with_cleanup()at lines 408–409.The
uid = Noneinitialization ensures the finally block is a no-op when_submit_requestitself fails before assigninguid.Changes
src/litserve/server.py: addedfinallyblock with safe buffer cleanup (+5 -0lines)Testing
response_bufferis cleaned up viapop(uid)on success, thenfinallyis a no-op ✅CancelledErrorraised duringevent.wait(),finallyblock removes orphaned entry ✅tests/unit/test_request_handlers.py— all 3 tests pass unchanged ✅