Skip to content

Celery initialization tests leak global app state into dataset cleanup tests #44432

Closed

Description

@hyoban

Description

The Celery initialization tests in api/tests/unit_tests/extensions/test_celery_ssl.py leave the process-wide current/default Celery app changed. Later shared tasks can then run inside the Flask app created by those tests instead of the app configured by their own fixtures.

This makes test_missing_elasticsearch_index_allows_parent_child_cleanup_after_retry[False/True] order-dependent: its local HTTP endpoint settings are bypassed, and the Elasticsearch client tries the default elasticsearch:9200 address until pytest-timeout interrupts it.

Observed CI failure: https://github.com/langgenius/dify/actions/runs/39414929741/job/118601858111

Reproduction

From the repository root, with the backend unit-test dependencies installed:

uv run --project api pytest \
  api/tests/unit_tests/extensions/test_celery_ssl.py \
  api/tests/unit_tests/tasks/test_clean_dataset_task.py \
  -k 'celery_init or missing_elasticsearch' \
  -o 'addopts=-p no:opik -p no:langsmith_plugin --import-mode=importlib' \
  --timeout=5 -q

The two Celery initialization tests pass, followed by two cleanup-test timeouts. Running the cleanup tests alone passes. CI runs with -n auto, so which tests share a worker determines whether the leak is observed.

Expected behavior

Celery initialization tests restore the previous current/default app and related Celery test state at teardown, including when a test fails. The cleanup tests should continue to use their local Elasticsearch endpoint regardless of prior test execution.

Logs

HTTPConnection(host='elasticsearch', port=9200)
socket.gaierror: [Errno -2] Name or service not known
Failed: Timeout (>20.0s) from pytest-timeout.

Activity

  1. dosubot commented on Oct 20, 2026

    @dosubot

    One additional teardown boundary to account for: init_app() also calls setup_workflow_warm_shutdown_handler(), which resets the active-workflow registry and connects two global Celery shutdown-signal receivers. Restoring only the current/default app would leave those side effects behind. For these SSL/task-registration tests, consider patching that setup function, since shutdown behavior is outside their scope. [1] [2]

    The Flask-context mechanism also matches your diagnosis: the task class created by init_app() captures its Flask app and enters that app’s context on every invocation. That can override the cleanup fixture’s context, while the Elasticsearch overrides exist only in the fixture app’s config. A regression assertion on Flask app identity inside an invoked shared task would catch this directly, without waiting for a network timeout. [3] [4]

    I couldn’t execute the reproduction in this environment; this is source-level confirmation.

    To reply, just mention @dosu.


    Share context across your team and agents. Try Dosu.

    Leave Feedback Learn about dify with Dosu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions