From 32841549c116817159a230bba587e6a8c4cb807e Mon Sep 17 00:00:00 2001 From: Kevin Hu Date: Tue, 20 Jan 2026 16:56:41 +0800 Subject: [PATCH] Fix: Not within a request context (#12723) ### What problem does this PR solve? ERROR 1819426 Unhandled exception during request Traceback (most recent call last): File "/home/qinling/[github.com/infiniflow/ragflow/api/apps/document_app.py](http://github.com/infiniflow/ragflow/api/apps/document_app.py)", line 639, in run return await thread_pool_exec(_run_sync) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/qinling/[github.com/infiniflow/ragflow/common/misc_utils.py](http://github.com/infiniflow/ragflow/common/misc_utils.py)", line 132, in thread_pool_exec return await loop.run_in_executor(_thread_pool_executor(), func, *args) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.12/asyncio/futures.py", line 287, in __await__ yield self # This tells Task to wait for completion. ^^^^^^^^^^ File "/usr/lib/python3.12/asyncio/tasks.py", line 385, in __wakeup future.result() File "/usr/lib/python3.12/asyncio/futures.py", line 203, in result raise self._exception.with_traceback(self._exception_tb) File "/usr/lib/python3.12/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/qinling/[github.com/infiniflow/ragflow/api/apps/document_app.py](http://github.com/infiniflow/ragflow/api/apps/document_app.py)", line 593, in _run_sync if not DocumentService.accessible(doc_id, [current_user.id](http://current_user.id/)): ^^^^^^^^^^^^^^^ File "/home/qinling/[github.com/infiniflow/ragflow/.venv/lib/python3.12/site-packages/werkzeug/local.py](http://github.com/infiniflow/ragflow/.venv/lib/python3.12/site-packages/werkzeug/local.py)", line 318, in __get__ obj = instance._get_current_object() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/qinling/[github.com/infiniflow/ragflow/.venv/lib/python3.12/site-packages/werkzeug/local.py](http://github.com/infiniflow/ragflow/.venv/lib/python3.12/site-packages/werkzeug/local.py)", line 526, in _get_current_object return get_name(local()) ^^^^^^^ File "/home/qinling/[github.com/infiniflow/ragflow/api/apps/__init__.py](http://github.com/infiniflow/ragflow/api/apps/__init__.py)", line 97, in _load_user authorization = request.headers.get("Authorization") ^^^^^^^^^^^^^^^ File "/home/qinling/[github.com/infiniflow/ragflow/.venv/lib/python3.12/site-packages/werkzeug/local.py](http://github.com/infiniflow/ragflow/.venv/lib/python3.12/site-packages/werkzeug/local.py)", line 318, in __get__ obj = instance._get_current_object() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/qinling/[github.com/infiniflow/ragflow/.venv/lib/python3.12/site-packages/werkzeug/local.py](http://github.com/infiniflow/ragflow/.venv/lib/python3.12/site-packages/werkzeug/local.py)", line 519, in _get_current_object raise RuntimeError(unbound_message) from None RuntimeError: Not within a request context ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) --- api/apps/document_app.py | 6 ++++-- api/apps/file_app.py | 5 +++-- api/apps/kb_app.py | 5 +++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/api/apps/document_app.py b/api/apps/document_app.py index 2b2147579..1906f9ccd 100644 --- a/api/apps/document_app.py +++ b/api/apps/document_app.py @@ -587,10 +587,11 @@ async def rm(): @validate_request("doc_ids", "run") async def run(): req = await get_request_json() + uid = current_user.id try: def _run_sync(): for doc_id in req["doc_ids"]: - if not DocumentService.accessible(doc_id, current_user.id): + if not DocumentService.accessible(doc_id, uid): return get_json_result(data=False, message="No authorization.", code=RetCode.AUTHENTICATION_ERROR) kb_table_num_map = {} @@ -646,9 +647,10 @@ async def run(): @validate_request("doc_id", "name") async def rename(): req = await get_request_json() + uid = current_user.id try: def _rename_sync(): - if not DocumentService.accessible(req["doc_id"], current_user.id): + if not DocumentService.accessible(req["doc_id"], uid): return get_json_result(data=False, message="No authorization.", code=RetCode.AUTHENTICATION_ERROR) e, doc = DocumentService.get_by_id(req["doc_id"]) diff --git a/api/apps/file_app.py b/api/apps/file_app.py index ec535ad55..50cbd185a 100644 --- a/api/apps/file_app.py +++ b/api/apps/file_app.py @@ -247,6 +247,7 @@ def get_all_parent_folders(): async def rm(): req = await get_request_json() file_ids = req["file_ids"] + uid = current_user.id try: def _delete_single_file(file): @@ -285,14 +286,14 @@ async def rm(): return get_data_error_result(message="File or Folder not found!") if not file.tenant_id: return get_data_error_result(message="Tenant not found!") - if not check_file_team_permission(file, current_user.id): + if not check_file_team_permission(file, uid): return get_json_result(data=False, message="No authorization.", code=RetCode.AUTHENTICATION_ERROR) if file.source_type == FileSource.KNOWLEDGEBASE: continue if file.type == FileType.FOLDER.value: - _delete_folder_recursive(file, current_user.id) + _delete_folder_recursive(file, uid) continue _delete_single_file(file) diff --git a/api/apps/kb_app.py b/api/apps/kb_app.py index 7a57ab949..c746af574 100644 --- a/api/apps/kb_app.py +++ b/api/apps/kb_app.py @@ -269,7 +269,8 @@ async def list_kbs(): @validate_request("kb_id") async def rm(): req = await get_request_json() - if not KnowledgebaseService.accessible4deletion(req["kb_id"], current_user.id): + uid = current_user.id + if not KnowledgebaseService.accessible4deletion(req["kb_id"], uid): return get_json_result( data=False, message='No authorization.', @@ -277,7 +278,7 @@ async def rm(): ) try: kbs = KnowledgebaseService.query( - created_by=current_user.id, id=req["kb_id"]) + created_by=uid, id=req["kb_id"]) if not kbs: return get_json_result( data=False, message='Only owner of dataset authorized for this operation.',