This commit is contained in:
Timothy Jaeryang Baek
2026-04-13 13:13:45 -05:00
parent 20544d412e
commit 51765b619c
2 changed files with 41 additions and 31 deletions
+15 -13
View File
@@ -1787,11 +1787,12 @@ async def chat_completion(
log.info('Chat processing was cancelled') log.info('Chat processing was cancelled')
try: try:
event_emitter = await get_event_emitter(metadata) event_emitter = await get_event_emitter(metadata)
await asyncio.shield( if event_emitter:
event_emitter( await asyncio.shield(
{'type': 'chat:tasks:cancel'}, event_emitter(
{'type': 'chat:tasks:cancel'},
)
) )
)
except Exception as e: except Exception as e:
pass pass
finally: finally:
@@ -1812,15 +1813,16 @@ async def chat_completion(
) )
event_emitter = await get_event_emitter(metadata) event_emitter = await get_event_emitter(metadata)
await event_emitter( if event_emitter:
{ await event_emitter(
'type': 'chat:message:error', {
'data': {'error': {'content': str(e)}}, 'type': 'chat:message:error',
} 'data': {'error': {'content': str(e)}},
) }
await event_emitter( )
{'type': 'chat:tasks:cancel'}, await event_emitter(
) {'type': 'chat:tasks:cancel'},
)
except Exception: except Exception:
pass pass
+26 -18
View File
@@ -4692,25 +4692,33 @@ async def streaming_chat_response_handler(response, ctx):
await background_tasks_handler(ctx) await background_tasks_handler(ctx)
except asyncio.CancelledError: except asyncio.CancelledError:
log.warning('Task was cancelled!') log.warning('Task was cancelled!')
await event_emitter({'type': 'chat:tasks:cancel'}) try:
await asyncio.shield(event_emitter({'type': 'chat:tasks:cancel'}))
if not ENABLE_REALTIME_CHAT_SAVE: if not ENABLE_REALTIME_CHAT_SAVE:
# Save message in the database # Save message in the database
await Chats.upsert_message_to_chat_by_id_and_message_id( await asyncio.shield(
metadata['chat_id'], Chats.upsert_message_to_chat_by_id_and_message_id(
metadata['message_id'], metadata['chat_id'],
{ metadata['message_id'],
'done': True, {
'content': serialize_output(output), 'done': True,
'output': output, 'content': serialize_output(output),
}, 'output': output,
) },
else: )
await Chats.upsert_message_to_chat_by_id_and_message_id( )
metadata['chat_id'], else:
metadata['message_id'], await asyncio.shield(
{'done': True}, Chats.upsert_message_to_chat_by_id_and_message_id(
) metadata['chat_id'],
metadata['message_id'],
{'done': True},
)
)
except Exception:
pass
raise # re-raise CancelledError for proper propagation
if response.background is not None: if response.background is not None:
await response.background() await response.background()