sqlalchemy.exc.InvalidRequestError: Could not refresh instance '<WorkflowNodeExecution at 0x7fad6504f040>' #4353

Closed
opened 2026-02-21 18:06:02 -05:00 by yindo · 5 comments
Owner

Originally created by @NiuBlibing on GitHub (Jun 30, 2024).

Originally assigned to: @takatost on GitHub.

Self Checks

  • 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 (我已阅读并同意 Language Policy).
  • 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.6.11

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

send many requests.

✔️ Expected Behavior

All requests is successful.

Actual Behavior

Some success failed with 'curl: (92) HTTP/2 stream 1 was not closed cleanly: INTERNAL_ERROR (err 2)'.
API error log:

[2024-07-01 03:25:06 +0000] [227] [ERROR] Error handling request
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/gunicorn/workers/base_async.py", line 115, in handle_request
for item in respiter:
File "/usr/local/lib/python3.10/site-packages/werkzeug/wsgi.py", line 256, in __next__
return self._next()
File "/usr/local/lib/python3.10/site-packages/werkzeug/wrappers/response.py", line 32, in _iter_encoded
for item in iterable:
File "/usr/local/lib/python3.10/site-packages/flask/helpers.py", line 113, in generator
yield from gen
File "/app/api/libs/helper.py", line 162, in generate
yield from response
File "/app/api/core/app/apps/base_app_generate_response_converter.py", line 28, in _generate
for chunk in cls.convert_stream_full_response(response):
File "/app/api/core/app/apps/workflow/generate_response_converter.py", line 45, in convert_stream_full_response
for chunk in stream_response:
File "/app/api/core/app/apps/workflow/generate_task_pipeline.py", line 155, in _to_stream_response
for stream_response in generator:
File "/app/api/core/app/apps/workflow/generate_task_pipeline.py", line 180, in _process_stream_response
workflow_node_execution = self._handle_node_start(event)
File "/app/api/core/app/task_pipeline/workflow_cycle_manage.py", line 415, in _handle_node_start
workflow_node_execution = self._init_node_execution_from_workflow_run(
File "/app/api/core/app/task_pipeline/workflow_cycle_manage.py", line 188, in _init_node_execution_from_workflow_run
db.session.refresh(workflow_node_execution)
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/scoping.py", line 1767, in refresh
return self._proxied.refresh(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 3169, in refresh
raise sa_exc.InvalidRequestError(
sqlalchemy.exc.InvalidRequestError: Could not refresh instance '<WorkflowNodeExecution at 0x7fad6504f040>'
Originally created by @NiuBlibing on GitHub (Jun 30, 2024). Originally assigned to: @takatost on GitHub. ### Self Checks - [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 (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [X] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [X] Please do not modify this template :) and fill in all the required fields. ### Dify version 0.6.11 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce send many requests. ### ✔️ Expected Behavior All requests is successful. ### ❌ Actual Behavior Some success failed with 'curl: (92) HTTP/2 stream 1 was not closed cleanly: INTERNAL_ERROR (err 2)'. API error log: ``` [2024-07-01 03:25:06 +0000] [227] [ERROR] Error handling request Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/gunicorn/workers/base_async.py", line 115, in handle_request for item in respiter: File "/usr/local/lib/python3.10/site-packages/werkzeug/wsgi.py", line 256, in __next__ return self._next() File "/usr/local/lib/python3.10/site-packages/werkzeug/wrappers/response.py", line 32, in _iter_encoded for item in iterable: File "/usr/local/lib/python3.10/site-packages/flask/helpers.py", line 113, in generator yield from gen File "/app/api/libs/helper.py", line 162, in generate yield from response File "/app/api/core/app/apps/base_app_generate_response_converter.py", line 28, in _generate for chunk in cls.convert_stream_full_response(response): File "/app/api/core/app/apps/workflow/generate_response_converter.py", line 45, in convert_stream_full_response for chunk in stream_response: File "/app/api/core/app/apps/workflow/generate_task_pipeline.py", line 155, in _to_stream_response for stream_response in generator: File "/app/api/core/app/apps/workflow/generate_task_pipeline.py", line 180, in _process_stream_response workflow_node_execution = self._handle_node_start(event) File "/app/api/core/app/task_pipeline/workflow_cycle_manage.py", line 415, in _handle_node_start workflow_node_execution = self._init_node_execution_from_workflow_run( File "/app/api/core/app/task_pipeline/workflow_cycle_manage.py", line 188, in _init_node_execution_from_workflow_run db.session.refresh(workflow_node_execution) File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/scoping.py", line 1767, in refresh return self._proxied.refresh( File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 3169, in refresh raise sa_exc.InvalidRequestError( sqlalchemy.exc.InvalidRequestError: Could not refresh instance '<WorkflowNodeExecution at 0x7fad6504f040>' ```
yindo added the 🐞 bug label 2026-02-21 18:06:02 -05:00
yindo closed this issue 2026-02-21 18:06:02 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jun 30, 2024):

The error you're encountering, sqlalchemy.exc.InvalidRequestError: Could not refresh instance '<WorkflowNodeExecution at 0x7fad6504f040>', along with the curl: (92) HTTP/2 stream 1 was not closed cleanly: INTERNAL_ERROR (err 2), is likely due to resource limitations or misconfigurations in your Docker setup.

To address this, ensure your Docker environment meets the following prerequisites:

  • macOS: Docker Desktop with a minimum of 2 virtual CPUs (vCPUs) and 8 GB of initial memory.
  • Linux: Docker 19.03 or later and Docker Compose 1.25.1 or later.
  • Windows with WSL 2 enabled: Docker Desktop with the source code and data stored in the Linux file system.

Additionally, consider upgrading to Dify 0.6.12, which includes significant enhancements to Docker Compose deployment that might resolve your issues. To upgrade, follow these steps:

cd dify/docker
git pull origin main
docker compose down
docker compose pull
docker compose up -d

Check the status of all containers with:

docker compose ps

If the issue persists, review and adjust the following environment variables in your docker-compose setup to handle the load more effectively:

  • SQLALCHEMY_POOL_SIZE: Increase the size of the database connection pool (default is 30).
  • SQLALCHEMY_POOL_RECYCLE: Adjust the database connection pool recycling time (default is 3600 seconds).

Also, ensure the following settings are configured appropriately for a high-load environment:

  • SERVER_WORKER_AMOUNT: Number of API server workers, recommended formula: number of cpu cores x 2 + 1.
  • GUNICORN_TIMEOUT: Set to 360 to support longer SSE connection times.
  • CELERY_WORKER_AMOUNT: Adjust the number of Celery workers based on the load.

For example:

SERVER_WORKER_AMOUNT: ${SERVER_WORKER_AMOUNT}
GUNICORN_TIMEOUT: ${GUNICORN_TIMEOUT}
CELERY_WORKER_AMOUNT: ${CELERY_WORKER_AMOUNT}

These adjustments should help manage the database connections and handle high request loads more effectively [1][2][3].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Jun 30, 2024): <!-- Answer --> The error you're encountering, `sqlalchemy.exc.InvalidRequestError: Could not refresh instance '<WorkflowNodeExecution at 0x7fad6504f040>'`, along with the `curl: (92) HTTP/2 stream 1 was not closed cleanly: INTERNAL_ERROR (err 2)`, is likely due to resource limitations or misconfigurations in your Docker setup. To address this, ensure your Docker environment meets the following prerequisites: - **macOS**: Docker Desktop with a minimum of 2 virtual CPUs (vCPUs) and 8 GB of initial memory. - **Linux**: Docker 19.03 or later and Docker Compose 1.25.1 or later. - **Windows with WSL 2 enabled**: Docker Desktop with the source code and data stored in the Linux file system. Additionally, consider upgrading to Dify 0.6.12, which includes significant enhancements to Docker Compose deployment that might resolve your issues. To upgrade, follow these steps: ```bash cd dify/docker git pull origin main docker compose down docker compose pull docker compose up -d ``` Check the status of all containers with: ```bash docker compose ps ``` If the issue persists, review and adjust the following environment variables in your `docker-compose` setup to handle the load more effectively: - **SQLALCHEMY_POOL_SIZE**: Increase the size of the database connection pool (default is 30). - **SQLALCHEMY_POOL_RECYCLE**: Adjust the database connection pool recycling time (default is 3600 seconds). Also, ensure the following settings are configured appropriately for a high-load environment: - **SERVER_WORKER_AMOUNT**: Number of API server workers, recommended formula: `number of cpu cores x 2 + 1`. - **GUNICORN_TIMEOUT**: Set to 360 to support longer SSE connection times. - **CELERY_WORKER_AMOUNT**: Adjust the number of Celery workers based on the load. For example: ```yaml SERVER_WORKER_AMOUNT: ${SERVER_WORKER_AMOUNT} GUNICORN_TIMEOUT: ${GUNICORN_TIMEOUT} CELERY_WORKER_AMOUNT: ${CELERY_WORKER_AMOUNT} ``` These adjustments should help manage the database connections and handle high request loads more effectively <sup>[[1]](https://github.com/langgenius/dify-docs/blob/main/en/getting-started/install-self-hosted/docker-compose.md)</sup><sup>[[2]](https://github.com/langgenius/dify-docs/blob/main/en/getting-started/install-self-hosted/environments.md)</sup><sup>[[3]](https://github.com/langgenius/dify-docs/blob/main/zh_CN/getting-started/install-self-hosted/environments.md)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@NiuBlibing commented on GitHub (Jul 1, 2024):

@dosu. not work.

@NiuBlibing commented on GitHub (Jul 1, 2024): @dosu. not work.
Author
Owner

@dosubot[bot] commented on GitHub (Jul 1, 2024):

To resolve the sqlalchemy.exc.InvalidRequestError and the curl: (92) HTTP/2 stream 1 was not closed cleanly: INTERNAL_ERROR (err 2) error, upgrading to Dify 0.6.12 is recommended. This version includes specific changes that address these issues.

Steps to Upgrade to Dify 0.6.12

  1. Navigate to the Docker directory:

    cd dify/docker
    
  2. Pull the latest changes:

    git pull origin main
    
  3. Bring down the current Docker Compose setup:

    docker compose down
    
  4. Pull the latest Docker images:

    docker compose pull
    
  5. Start the updated Docker Compose setup:

    docker compose up -d
    

Key Environment Variables to Check

Ensure the following environment variables are correctly configured:

  • Database Configuration:

    • SQLALCHEMY_POOL_SIZE: Increase the size of the database connection pool (default is 30).
    • SQLALCHEMY_POOL_RECYCLE: Adjust the database connection pool recycling time (default is 3600 seconds).
  • API Service Configuration:

    • SERVER_WORKER_AMOUNT: Number of server workers (recommended formula: CPU cores x 2 + 1).
    • GUNICORN_TIMEOUT: Set to 360 to support longer SSE connection times.

Specific Code Changes in Version 0.6.12

The generate_task_pipeline.py file in version 0.6.12 includes changes that address the sqlalchemy.exc.InvalidRequestError:

  1. Refreshing the session before closing:

    db.session.refresh(self._workflow)
    db.session.refresh(self._user)
    db.session.close()
    
  2. Committing and refreshing the session before closing:

    db.session.commit()
    db.session.refresh(self._message)
    db.session.close()
    

These changes ensure that the session is properly refreshed and committed before being closed, which can help prevent the InvalidRequestError [1].

By following these steps and ensuring your environment variables are correctly set, you should be able to resolve the issues you are experiencing with your Dify setup.

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Jul 1, 2024): <!-- Answer --> To resolve the `sqlalchemy.exc.InvalidRequestError` and the `curl: (92) HTTP/2 stream 1 was not closed cleanly: INTERNAL_ERROR (err 2)` error, upgrading to Dify 0.6.12 is recommended. This version includes specific changes that address these issues. ### Steps to Upgrade to Dify 0.6.12 1. **Navigate to the Docker directory:** ```bash cd dify/docker ``` 2. **Pull the latest changes:** ```bash git pull origin main ``` 3. **Bring down the current Docker Compose setup:** ```bash docker compose down ``` 4. **Pull the latest Docker images:** ```bash docker compose pull ``` 5. **Start the updated Docker Compose setup:** ```bash docker compose up -d ``` ### Key Environment Variables to Check Ensure the following environment variables are correctly configured: - **Database Configuration:** - `SQLALCHEMY_POOL_SIZE`: Increase the size of the database connection pool (default is 30). - `SQLALCHEMY_POOL_RECYCLE`: Adjust the database connection pool recycling time (default is 3600 seconds). - **API Service Configuration:** - `SERVER_WORKER_AMOUNT`: Number of server workers (recommended formula: CPU cores x 2 + 1). - `GUNICORN_TIMEOUT`: Set to 360 to support longer SSE connection times. ### Specific Code Changes in Version 0.6.12 The `generate_task_pipeline.py` file in version 0.6.12 includes changes that address the `sqlalchemy.exc.InvalidRequestError`: 1. **Refreshing the session before closing:** ```python db.session.refresh(self._workflow) db.session.refresh(self._user) db.session.close() ``` 2. **Committing and refreshing the session before closing:** ```python db.session.commit() db.session.refresh(self._message) db.session.close() ``` These changes ensure that the session is properly refreshed and committed before being closed, which can help prevent the `InvalidRequestError` <sup>[[1]](https://github.com/langgenius/dify/blob/main/api/core/app/apps/advanced_chat/generate_task_pipeline.py)</sup>. By following these steps and ensuring your environment variables are correctly set, you should be able to resolve the issues you are experiencing with your Dify setup. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@NiuBlibing commented on GitHub (Jul 2, 2024):

@takatost could you help with this workflow issue?

@NiuBlibing commented on GitHub (Jul 2, 2024): @takatost could you help with this workflow issue?
Author
Owner

@NiuBlibing commented on GitHub (Jul 5, 2024):

https://github.com/langgenius/dify/issues/5794
Same reason: postgresql consistency of master and slave.

@NiuBlibing commented on GitHub (Jul 5, 2024): https://github.com/langgenius/dify/issues/5794 Same reason: postgresql consistency of master and slave.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#4353