Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added missing exit_stack.close() to /v1/chat/completions #1796

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions llama_cpp/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,14 @@ async def create_completion(
else:
kwargs["logits_processor"].extend(_min_tokens_logits_processor)

iterator_or_completion: Union[
llama_cpp.CreateCompletionResponse,
Iterator[llama_cpp.CreateCompletionStreamResponse],
] = await run_in_threadpool(llama, **kwargs)
try:
iterator_or_completion: Union[
llama_cpp.CreateCompletionResponse,
Iterator[llama_cpp.CreateCompletionStreamResponse],
] = await run_in_threadpool(llama, **kwargs)
except Exception as err:
exit_stack.close()
raise err

if isinstance(iterator_or_completion, Iterator):
# EAFP: It's easier to ask for forgiveness than permission
Expand All @@ -344,6 +348,7 @@ def iterator() -> Iterator[llama_cpp.CreateCompletionStreamResponse]:
ping_message_factory=_ping_message_factory,
)
else:
exit_stack.close()
return iterator_or_completion


Expand Down Expand Up @@ -508,9 +513,13 @@ async def create_chat_completion(
else:
kwargs["logits_processor"].extend(_min_tokens_logits_processor)

iterator_or_completion: Union[
llama_cpp.ChatCompletion, Iterator[llama_cpp.ChatCompletionChunk]
] = await run_in_threadpool(llama.create_chat_completion, **kwargs)
try:
iterator_or_completion: Union[
llama_cpp.ChatCompletion, Iterator[llama_cpp.ChatCompletionChunk]
] = await run_in_threadpool(llama.create_chat_completion, **kwargs)
except Exception as err:
exit_stack.close()
raise err

if isinstance(iterator_or_completion, Iterator):
# EAFP: It's easier to ask for forgiveness than permission
Expand Down
6 changes: 4 additions & 2 deletions llama_cpp/server/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ def error_message_wrapper(
] = None,
) -> Tuple[int, ErrorResponse]:
"""Wraps error message in OpenAI style error response"""
print(f"Exception: {str(error)}", file=sys.stderr)
traceback.print_exc(file=sys.stderr)
if body is not None and isinstance(
body,
(
Expand All @@ -149,6 +147,10 @@ def error_message_wrapper(
if match is not None:
return callback(body, match)

# Only print the trace on unexpected exceptions
print(f"Exception: {str(error)}", file=sys.stderr)
traceback.print_exc(file=sys.stderr)

# Wrap other errors as internal server error
return 500, ErrorResponse(
message=str(error),
Expand Down