|
583 | 583 | from open_webui.tasks import ( |
584 | 584 | redis_task_command_listener, |
585 | 585 | list_task_ids_by_item_id, |
| 586 | + has_active_tasks, |
| 587 | + cleanup_task, |
586 | 588 | create_task, |
587 | 589 | stop_task, |
588 | 590 | stop_item_tasks, |
@@ -2063,22 +2065,21 @@ async def emit_cancel_event(): |
2063 | 2065 | except BaseException as e: |
2064 | 2066 | log.debug(f'Error cleaning up MCP clients: {e}') |
2065 | 2067 |
|
| 2068 | + # Deregister this task, then emit chat:active=false if no others remain |
2066 | 2069 | try: |
2067 | | - if metadata.get('chat_id'): |
2068 | | - |
2069 | | - async def emit_inactive_event(): |
2070 | | - try: |
2071 | | - event_emitter = await get_event_emitter(metadata, update_db=False) |
2072 | | - if event_emitter: |
2073 | | - await event_emitter({'type': 'chat:active', 'data': {'active': False}}) |
2074 | | - except Exception: |
2075 | | - pass |
2076 | | - |
2077 | | - try: |
2078 | | - # Shield the event emission so it finishes even if the main task is cancelled |
2079 | | - await asyncio.shield(emit_inactive_event()) |
2080 | | - except asyncio.CancelledError: |
2081 | | - pass |
| 2070 | + chat_id = metadata.get('chat_id') |
| 2071 | + task_id = metadata.get('task_id') |
| 2072 | + if chat_id and task_id: |
| 2073 | + await cleanup_task(request.app.state.redis, task_id, chat_id) |
| 2074 | + if not await has_active_tasks(request.app.state.redis, chat_id): |
| 2075 | + event_emitter = await get_event_emitter(metadata, update_db=False) |
| 2076 | + if event_emitter: |
| 2077 | + try: |
| 2078 | + await asyncio.shield( |
| 2079 | + event_emitter({'type': 'chat:active', 'data': {'active': False}}) |
| 2080 | + ) |
| 2081 | + except asyncio.CancelledError: |
| 2082 | + pass |
2082 | 2083 | except Exception: |
2083 | 2084 | pass |
2084 | 2085 |
|
@@ -2128,6 +2129,7 @@ async def emit_inactive_event(): |
2128 | 2129 | ), |
2129 | 2130 | id=chat_id, |
2130 | 2131 | ) |
| 2132 | + per_model_metadata['task_id'] = task_id |
2131 | 2133 | task_ids.append(task_id) |
2132 | 2134 |
|
2133 | 2135 | # Emit chat:active=true |
|
0 commit comments