langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
53.24k stars 7.77k forks source link

Instance '<Workflow at 0x7f7800591750>' is not persistent within this Session #10046

Open lichengwu opened 1 month ago

lichengwu commented 1 month ago

Self Checks

Dify version

0.9.1-fix1

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce


Exception: 500 Internal Server Error: The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
Traceback: Traceback (most recent call last):
  File "/api/controllers/service_api/app/completion.py", line 122, in post
    response = AppGenerateService.generate(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/api/services/app_generate_service.py", line 71, in generate
    AdvancedChatAppGenerator().generate(
  File "/api/core/app/apps/advanced_chat/app_generator.py", line 153, in generate
    return self._generate(
           ^^^^^^^^^^^^^^^
  File "/api/core/app/apps/advanced_chat/app_generator.py", line 270, in _generate
    response = self._handle_advanced_chat_response(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/api/core/app/apps/advanced_chat/app_generator.py", line 371, in _handle_advanced_chat_response
    return generate_task_pipeline.process()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/api/core/app/apps/advanced_chat/generate_task_pipeline.py", line 126, in process
    db.session.refresh(self._workflow)
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/orm/scoping.py", line 1767, in refresh
    return self._proxied.refresh(
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/orm/session.py", line 3143, in refresh
    self._expire_state(state, attribute_names)
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/orm/session.py", line 3269, in _expire_state
    self._validate_persistent(state)
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/orm/session.py", line 4138, in _validate_persistent
    raise sa_exc.InvalidRequestError(
sqlalchemy.exc.InvalidRequestError: Instance '<Workflow at 0x7f77dde9ff90>' is not persistent within this Session

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 880, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 865, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask_restful/__init__.py", line 489, in wrapper
    resp = resource(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask/views.py", line 110, in view
    return current_app.ensure_sync(self.dispatch_request)(**kwargs)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask_restful/__init__.py", line 604, in dispatch_request
    resp = meth(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/api/controllers/service_api/wraps.py", line 132, in decorated_view
    return view_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/api/controllers/service_api/app/completion.py", line 148, in post
    raise InternalServerError()
werkzeug.exceptions.InternalServerError: 500 Internal Server Error: The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

✔️ Expected Behavior

No response

❌ Actual Behavior

Instance '<Workflow at 0x7f7800591750>' is not persistent within this Session

dosubot[bot] commented 1 month ago

The error message "Instance '<Workflow at 0x7f7800591750>' is not persistent within this Session" indicates that the Workflow instance is not associated with the current SQLAlchemy session when db.session.refresh() is called. This can happen if the instance was not added to the session or if it was removed from the session before the refresh operation.

Here are a few steps you can take to resolve this issue:

  1. Ensure the Instance is Added to the Session: Before calling db.session.refresh(), make sure that the Workflow instance is added to the session using db.session.add(workflow_instance).

  2. Check for Session Expiry: If the session was closed or expired before the refresh operation, you might need to reopen or reattach the instance to a new session.

  3. Verify the Instance State: Use db.session.is_modified(workflow_instance) to check if the instance is in a state that can be refreshed. If it returns False, the instance might not be in a persistent state.

  4. Review Transaction Boundaries: Ensure that the session is not being prematurely committed or rolled back, which could detach the instance from the session.

By ensuring that the Workflow instance is properly managed within the session lifecycle, you should be able to resolve this error.

To continue talking to Dosu, mention @dosu.