Fix: Refactor AsyncExitStack usage to resolve lock handling errors #1862
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.
Fixes #1861
This PR replaces the manual creation of
exit_stack = contextlib.AsyncExitStack()
with its use as a context manager in thecreate_completion
andcreate_chat_completion
functions.The original implementation manually managed
exit_stack
and explicitly calledexit_stack.aclose()
(inget_event_publisher(on_complete=exit_stack.aclose())
), which resulted in the following error when sending a request tollama_cpp.server
with"stream": true
parameter:This error apparently occurred because an attempt was made to release a lock in an execution context different from the one in which it was acquired.
The changes:
AsyncExitStack
management with theasync with contextlib.AsyncExitStack()
pattern.exit_stack.aclose()
and the passing ofon_complete=exit_stack.aclose
, as resource cleanup is now handled automatically by the context manager.