Build resource cleanup is unrecoverable when Celery publication fails

Open
#46,985 0 comments 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 1 day

@he-yufeng is already working on this.

Since Nov 23, 2026.

  • #46986 by @he-yufeng — open

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
18/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
postgresql, python, sqlite

Research direction

Start from the post-commit boundary where collect_agent_resources.delay() is published after the execution binding is set to RETIRED, and trace the four affected flows (conversation refresh, forced draft checkout, draft discard, draft apply). The regression harness injects RuntimeError("test broker unavailable") at that real publication point and asserts recovery for refresh, discard and apply. Done means the retirement and a durable outbox intent commit in one transaction, claimed attempts republish from the existing retention queue, and the intent is deleted only after idempotent backend cleanup.

Written by the indexing model from the issue text.

Description

Summary

Build execution-binding retirement is committed before its Celery cleanup task is published. If publication fails, the API request returns an error but the retired binding no longer has a durable dispatch record. The backend binding and workspace can then remain allocated without a way for a later retry to discover them.

Affected flows

The post-commit publication boundary is used by Build conversation refresh, forced draft checkout, draft discard, and draft apply. The impact is especially clear for apply because a successful apply deletes the personal Build draft before cleanup is published. Retrying the request cannot reconstruct the retired binding from that draft.

Steps to reproduce

  1. Create a personal Build draft with an active execution binding.
  2. Let the Build mutation commit successfully, then make collect_agent_resources.delay() raise a broker error.
  3. Observe that the request returns HTTP 500 while the binding is already RETIRED and the draft pointer is cleared, or the draft is deleted by apply.
  4. Retry the operation or inspect the remaining state. There is no persistent cleanup intent from which to republish the retired binding.

The regression harness uses a deterministic RuntimeError("test broker unavailable") at the real Celery publication boundary. Before the fix, refresh and discard both fail the recovery assertion, and apply leaves no recovery record after deleting its draft.

Expected behavior

Retirement and a durable cleanup dispatch intent should commit atomically. Publication should happen only after that commit. A broker failure should retain the intent for bounded retries, and successful physical cleanup should settle it idempotently.

Repeated delivery remains possible, so this does not require exactly-once delivery. It does require a durable recovery key and cleanup that is safe to repeat.

Actual behavior

The retired binding is persisted, but its only cleanup publication is attempted in memory after the commit. A publication exception is propagated without a durable retry record.

Proposed direction

Add a Build-scoped transactional outbox containing the tenant and binding IDs, attempt generation, last attempt time, and a bounded error type. Claim attempts with a conditional update, republish them from the existing retention queue, and delete an intent only after successful idempotent backend cleanup at the same claim generation. Retain exhausted records for operator review instead of silently dropping them.

Validation for the proposed patch uses Python 3.13.11, SQLite, an in-memory broker, and a loopback checkpoint server. It does not claim a hosted Postgres, external Celery broker, or production Agent backend verification.

Dominant language
Python
Stars
162k
Forks
25.6k
Avg merge
15h 9m
Merged PRs (30d)
606

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from langgenius/dify

All issues in langgenius/dify

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.