From b462e17a5bfb3ed2f67e544db46c3a17040fa271 Mon Sep 17 00:00:00 2001 From: lixiangcheng1 Date: Tue, 24 Feb 2026 16:53:07 +0800 Subject: [PATCH] [fix]A threading communication issue occurred when using the Trio asynchronous framework. The core error was OSError: [errno 9] Bad file descriptor, which occurred when Trio attempted to wake up the event loop in a multi-threaded environment --- api/app/tasks.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/api/app/tasks.py b/api/app/tasks.py index e6bdeb18..e29d0406 100644 --- a/api/app/tasks.py +++ b/api/app/tasks.py @@ -378,6 +378,9 @@ def build_graphrag_for_kb(kb_id: uuid.UUID): ) except Exception as e: print(f"{datetime.now().strftime('%H:%M:%S')} GraphRAG task failed for task {task}:\n{str(e)}\n") + finally: + if db: + db.close() print(f"{datetime.now().strftime('%H:%M:%S')} Knowledge Graph done ({time.time() - start_time}s)") result = f"build knowledge graph '{db_knowledge.name}' processed successfully." @@ -388,7 +391,8 @@ def build_graphrag_for_kb(kb_id: uuid.UUID): result = f"build knowledge grap '{db_knowledge.name}' failed." return result finally: - db.close() + if db: + db.close() @celery_app.task(name="app.core.rag.tasks.sync_knowledge_for_kb")