API: Fix race condition when client disconnects

This commit is contained in:
turboderp
2025-10-05 21:23:02 +02:00
parent 52e093ae6c
commit d672dc2137
3 changed files with 6 additions and 15 deletions

View File

@@ -61,10 +61,7 @@ async def _stream_collector(data: GenerateRequest, request: Request):
async for generation in generator:
if disconnect_task.done():
abort_event.set()
handle_request_disconnect(
f"Kobold generation {data.genkey} cancelled by user."
)
raise CancelledError()
text = generation.get("text")
@@ -78,7 +75,7 @@ async def _stream_collector(data: GenerateRequest, request: Request):
break
except CancelledError:
# If the request disconnects, break out
if not disconnect_task.done():
if not abort_event.is_set():
abort_event.set()
handle_request_disconnect(
f"Kobold generation {data.genkey} cancelled by user."

View File

@@ -348,10 +348,7 @@ async def stream_generate_chat_completion(
# Consumer loop
while True:
if disconnect_task.done():
abort_event.set()
handle_request_disconnect(
f"Chat completion generation {request.state.id} cancelled by user."
)
raise CancelledError()
generation = await gen_queue.get()
@@ -401,7 +398,7 @@ async def stream_generate_chat_completion(
except CancelledError:
# Get out if the request gets disconnected
if not disconnect_task.done():
if not abort_event.is_set():
abort_event.set()
handle_request_disconnect("Chat completion generation cancelled by user.")
except Exception:

View File

@@ -226,10 +226,7 @@ async def stream_generate_completion(
# Consumer loop
while True:
if disconnect_task.done():
abort_event.set()
handle_request_disconnect(
f"Completion generation {request.state.id} cancelled by user."
)
raise CancelledError()
generation = await gen_queue.get()
@@ -248,7 +245,7 @@ async def stream_generate_completion(
except CancelledError:
# Get out if the request gets disconnected
if not disconnect_task.done():
if not abort_event.is_set():
abort_event.set()
handle_request_disconnect(
f"Completion generation {request.state.id} cancelled by user."