Knowledge indexing stuck in queuing #16143

Closed
opened 2026-02-21 19:24:48 -05:00 by yindo · 2 comments
Owner

Originally created by @lordk911 on GitHub (Aug 13, 2025).

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

1.7.1

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

when upload file to a knowledge base,some time it will stuck in queuing state with backround error:

Task tasks.document_indexing_task.document_indexing_task[15200ca0-fb5f-4051-8992-6dfec1f458d8] received
2025-08-13 09:15:34,105.105 ERROR [Dummy-248] [trace.py:267] - Task tasks.document_indexing_task.document_indexing_task[15200ca0-fb5f-4051-8992-6dfec1f458d8] raised unexpected: OperationalError('(psycopg2.OperationalError) server closed the connection unexpectedly\n\tThis probably means the server terminated abnormally\n\tbefore or while processing the request.\n')
Traceback (most recent call last):
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1963, in _exec_single_context
    self.dialect.do_execute(
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 943, in do_execute
    cursor.execute(statement, parameters)
  File "/app/api/.venv/lib/python3.12/site-packages/psycogreen/gevent.py", line 32, in gevent_wait_callback
    state = conn.poll()
            ^^^^^^^^^^^
psycopg2.OperationalError: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/app/api/.venv/lib/python3.12/site-packages/celery/app/trace.py", line 453, in trace_task
    R = retval = fun(*args, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^
  File "/app/api/extensions/ext_celery.py", line 15, in __call__
    return self.run(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/tasks/document_indexing_task.py", line 27, in document_indexing_task
    dataset = db.session.query(Dataset).where(Dataset.id == dataset_id).first()
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/query.py", line 2759, in first
    return self.limit(1)._iter().first()  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/query.py", line 2857, in _iter
    result: Union[ScalarResult[_T], Result[_T]] = self.session.execute(
                                                  ^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2365, in execute
    return self._execute_internal(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2251, in _execute_internal
    result: Result[Any] = compile_state_cls.orm_execute_statement(
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/context.py", line 306, in orm_execute_statement
    result = conn.execute(
             ^^^^^^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1415, in execute
    return meth(
           ^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/sql/elements.py", line 523, in _execute_on_connection
    return connection._execute_clauseelement(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1637, in _execute_clauseelement
    ret = self._execute_context(
          ^^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1842, in _execute_context
    return self._exec_single_context(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1982, in _exec_single_context
    self._handle_dbapi_exception(
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 2351, in _handle_dbapi_exception
    raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1963, in _exec_single_context
    self.dialect.do_execute(
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 943, in do_execute
    cursor.execute(statement, parameters)
  File "/app/api/.venv/lib/python3.12/site-packages/psycogreen/gevent.py", line 32, in gevent_wait_callback
    state = conn.poll()
            ^^^^^^^^^^^
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.

[SQL: SELECT datasets.id AS datasets_id, datasets.tenant_id AS datasets_tenant_id, datasets.name AS datasets_name, datasets.description AS datasets_description, datasets.provider AS datasets_provider, datasets.permission AS datasets_permission, datasets.data_source_type AS datasets_data_source_type, datasets.indexing_technique AS datasets_indexing_technique, datasets.index_struct AS datasets_index_struct, datasets.created_by AS datasets_created_by, datasets.created_at AS datasets_created_at, datasets.updated_by AS datasets_updated_by, datasets.updated_at AS datasets_updated_at, datasets.embedding_model AS datasets_embedding_model, datasets.embedding_model_provider AS datasets_embedding_model_provider, datasets.collection_binding_id AS datasets_collection_binding_id, datasets.retrieval_model AS datasets_retrieval_model, datasets.built_in_field_enabled AS datasets_built_in_field_enabled 
FROM datasets 
WHERE datasets.id = %(id_1)s::UUID 
 LIMIT %(param_1)s]
[parameters: {'id_1': '31db47aa-7210-46bd-a81f-050769d1903e', 'param_1': 1}]
(Background on this error at: https://sqlalche.me/e/20/e3q8)

However, no errors were observed on the PostgreSQL side.
Delete the queuing state files and upload again it will be work.

✔️ Expected Behavior

Knowledge indexing succeed

Actual Behavior

No response

Originally created by @lordk911 on GitHub (Aug 13, 2025). ### Self Checks - [x] I have read the [Contributing Guide](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) and [Language Policy](https://github.com/langgenius/dify/issues/1542). - [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [x] I confirm that I am using English to submit this report, otherwise it will be closed. - [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :) - [x] Please do not modify this template :) and fill in all the required fields. ### Dify version 1.7.1 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce when upload file to a knowledge base,some time it will stuck in queuing state with backround error: ``` Task tasks.document_indexing_task.document_indexing_task[15200ca0-fb5f-4051-8992-6dfec1f458d8] received 2025-08-13 09:15:34,105.105 ERROR [Dummy-248] [trace.py:267] - Task tasks.document_indexing_task.document_indexing_task[15200ca0-fb5f-4051-8992-6dfec1f458d8] raised unexpected: OperationalError('(psycopg2.OperationalError) server closed the connection unexpectedly\n\tThis probably means the server terminated abnormally\n\tbefore or while processing the request.\n') Traceback (most recent call last): File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1963, in _exec_single_context self.dialect.do_execute( File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 943, in do_execute cursor.execute(statement, parameters) File "/app/api/.venv/lib/python3.12/site-packages/psycogreen/gevent.py", line 32, in gevent_wait_callback state = conn.poll() ^^^^^^^^^^^ psycopg2.OperationalError: server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/app/api/.venv/lib/python3.12/site-packages/celery/app/trace.py", line 453, in trace_task R = retval = fun(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^ File "/app/api/extensions/ext_celery.py", line 15, in __call__ return self.run(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/tasks/document_indexing_task.py", line 27, in document_indexing_task dataset = db.session.query(Dataset).where(Dataset.id == dataset_id).first() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/query.py", line 2759, in first return self.limit(1)._iter().first() # type: ignore ^^^^^^^^^^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/query.py", line 2857, in _iter result: Union[ScalarResult[_T], Result[_T]] = self.session.execute( ^^^^^^^^^^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2365, in execute return self._execute_internal( ^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2251, in _execute_internal result: Result[Any] = compile_state_cls.orm_execute_statement( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/context.py", line 306, in orm_execute_statement result = conn.execute( ^^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1415, in execute return meth( ^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/sql/elements.py", line 523, in _execute_on_connection return connection._execute_clauseelement( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1637, in _execute_clauseelement ret = self._execute_context( ^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1842, in _execute_context return self._exec_single_context( ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1982, in _exec_single_context self._handle_dbapi_exception( File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 2351, in _handle_dbapi_exception raise sqlalchemy_exception.with_traceback(exc_info[2]) from e File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1963, in _exec_single_context self.dialect.do_execute( File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 943, in do_execute cursor.execute(statement, parameters) File "/app/api/.venv/lib/python3.12/site-packages/psycogreen/gevent.py", line 32, in gevent_wait_callback state = conn.poll() ^^^^^^^^^^^ sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. [SQL: SELECT datasets.id AS datasets_id, datasets.tenant_id AS datasets_tenant_id, datasets.name AS datasets_name, datasets.description AS datasets_description, datasets.provider AS datasets_provider, datasets.permission AS datasets_permission, datasets.data_source_type AS datasets_data_source_type, datasets.indexing_technique AS datasets_indexing_technique, datasets.index_struct AS datasets_index_struct, datasets.created_by AS datasets_created_by, datasets.created_at AS datasets_created_at, datasets.updated_by AS datasets_updated_by, datasets.updated_at AS datasets_updated_at, datasets.embedding_model AS datasets_embedding_model, datasets.embedding_model_provider AS datasets_embedding_model_provider, datasets.collection_binding_id AS datasets_collection_binding_id, datasets.retrieval_model AS datasets_retrieval_model, datasets.built_in_field_enabled AS datasets_built_in_field_enabled FROM datasets WHERE datasets.id = %(id_1)s::UUID LIMIT %(param_1)s] [parameters: {'id_1': '31db47aa-7210-46bd-a81f-050769d1903e', 'param_1': 1}] (Background on this error at: https://sqlalche.me/e/20/e3q8) ``` However, no errors were observed on the PostgreSQL side. Delete the queuing state files and upload again it will be work. ### ✔️ Expected Behavior Knowledge indexing succeed ### ❌ Actual Behavior _No response_
yindo added the 🐞 bug label 2026-02-21 19:24:48 -05:00
yindo closed this issue 2026-02-21 19:24:48 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 13, 2025):

Your issue is likely related to database connection/session exhaustion in background tasks. Recent updates after 1.7.1 added missing db.session.close() calls in document indexing tasks to ensure proper cleanup and prevent connection leaks, which can cause the "server closed the connection unexpectedly" error you’re seeing (PR #23122, PR #23046).

To address this:

  • Make sure your deployment includes these fixes (they landed after 1.7.1).
  • Restart your worker container (docker compose restart worker) to clear any stuck connections or tasks.
  • Check Docker resource allocation (CPU, memory) and PostgreSQL logs for signs of resource exhaustion or restarts.
  • Review your database connection pool and Celery worker concurrency settings to ensure they fit your workload.
  • Monitor for network instability between your API and PostgreSQL containers.

These steps should help resolve or further diagnose the queuing and connection issues.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Aug 13, 2025): <!-- Answer --> Your issue is likely related to database connection/session exhaustion in background tasks. Recent updates after 1.7.1 added missing db.session.close() calls in document indexing tasks to ensure proper cleanup and prevent connection leaks, which can cause the "server closed the connection unexpectedly" error you’re seeing ([PR #23122](https://github.com/langgenius/dify/pull/23122), [PR #23046](https://github.com/langgenius/dify/pull/23046)). To address this: - Make sure your deployment includes these fixes (they landed after 1.7.1). - Restart your worker container (`docker compose restart worker`) to clear any stuck connections or tasks. - Check Docker resource allocation (CPU, memory) and PostgreSQL logs for signs of resource exhaustion or restarts. - Review your database connection pool and Celery worker concurrency settings to ensure they fit your workload. - Monitor for network instability between your API and PostgreSQL containers. These steps should help resolve or further diagnose the queuing and connection issues. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/c6752e66-2c93-44d5-b0dd-43278fe887c2?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/c6752e66-2c93-44d5-b0dd-43278fe887c2?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/c6752e66-2c93-44d5-b0dd-43278fe887c2?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/c6752e66-2c93-44d5-b0dd-43278fe887c2?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/c6752e66-2c93-44d5-b0dd-43278fe887c2?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/c6752e66-2c93-44d5-b0dd-43278fe887c2?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/c6752e66-2c93-44d5-b0dd-43278fe887c2?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/6088a4f4-073a-43f3-b3d4-4f06fb0c05a0/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/23909)
Author
Owner

@crazywoola commented on GitHub (Aug 13, 2025):

Check your database logs for more details.

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
@crazywoola commented on GitHub (Aug 13, 2025): Check your database logs for more details. ``` sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#16143