Skip to content

fix(functions): requeue jobs callbacks that lose the deployment lock - #15852

Open
Arnav-1456 wants to merge 1 commit into
appwrite:mainfrom
Arnav-1456:fix-15745-jobs-callback-contention-requeue
Open

Arnav-1456 wants to merge 1 commit into
appwrite:mainfrom
Arnav-1456:fix-15745-jobs-callback-contention-requeue

Conversation

@Arnav-1456

@Arnav-1456 Arnav-1456 commented Nov 19, 2026 •

Copy link
Copy Markdown

What does this PR do?

Fixes #15745.

The jobs worker applies every state callback (artifact/exit/complete) under a per-deployment distributed lock, waiting at most LOCK_TIMEOUT (10 s) for it. With more than one appwrite-worker-jobs container — the normal production layout — state callbacks race for that lock, and the loser throws Utopia\Lock\Exception\Contention. The queue treats any non-PermanentFailure exception as retryable, but on the Redis broker (the self-hosted default) a rejection is final: the message is dead-lettered to utopia-queue.failed.v1-jobs and never retried. The jobs-service does not resend either, because the ingest endpoint already answered 204.

Consequences reported in #15745: deployments stuck in building when a lost exit/complete callback would have finalized them, missing build log lines when a contended log batch dead-lettered, and a failed list that grows by hundreds of entries per deploy day. #15881 already batches log callbacks behind a per-process gate so they no longer contend within one worker process — but the gate leader still races for the shared lock against state callbacks and other workers' leaders, so cross-process contention could still dead-letter log batches.

This PR makes any contended callback requeue itself instead of dead-lettering:

  • src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php
    • catches Contention at both contended sites — the state-callback $locks(...) call in the action body, and the gated log batch leader's $locks(...) call inside log() (via a shared private requeueCallback() helper) — exceptions from the callback body keep their current semantics;
    • contention is raised before the callback body runs, so nothing has been applied and the message is safe to requeue as-is; the existing dedupe marker still guards a redelivery after a partial application;
    • the requeued message goes through publisherForJobs (existing resource) back onto v1-jobs, landing at the tail of the queue — the queue itself provides the spacing between attempts, and each attempt waits up to LOCK_TIMEOUT again; a requeued log callback re-adds its lines on the next delivery, and the batch's dedupe marker keeps them from appending twice;
    • requeues are capped (MAX_REQUEUES = 3, i.e. 4 deliveries); beyond that the message is dead-lettered on purpose via PermanentFailure, so a persistently stuck lock cannot loop a message forever;
    • if the requeue enqueue itself fails, a RuntimeException surfaces it (mirrors the existing marker-save failure handling) instead of losing the callback silently.
  • src/Appwrite/Event/Message/Jobs.php
    • adds an optional attempts field (default 0) to the envelope, round-tripped in toArray()/fromArray(); the ingest endpoint is unchanged, payloads already in queues stay compatible.
  • Regression tests (per the review's finding that none covered this path):
    • testContendedCallbacksOfALockedBuildAreRequeuedAndApplied — holds the deployment lock while a real build streams its callbacks, releases, and asserts the build still completes: on a worker without the requeue the exit/complete callbacks dead-letter on their first contention and the deployment never completes;
    • testALockHeldBeyondTheRequeueCapDeadLettersCallbacks — holds the lock past the requeue cap and asserts a state callback dead-letters only after carrying the cap's requeue count (attempts >= 3), and that the deployment never becomes ready.

No API surface changes; no new dependencies.

Test Plan

  • vendor/bin/pint --config pint.json --test on all changed files — PASS (PSR-12, repo config).
  • vendor/bin/phpstan analyse -c phpstan.neon on all changed files — 0 errors.
  • test tests/e2e/Services/Functions against the local dev stack (docker compose up -d), which deploys real functions through the executor and exercises the jobs worker callback path end-to-end — 101 tests, all green (was 99 before this PR; +2 regression tests).
  • The two new tests directly cover the review's finding: one proves a contended callback is republished and eventually applied, the other proves the retry limit is enforced.

Related PRs and Issues

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs? — No API metadata changes.

Copilot AI balanced review requested due to automatic review settings November 19, 2026 18:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@appwrite

appwrite Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Code review

Commit Status Tier Verdict Details
072450a Reviewed Reviewed 🟢 S · Ready to merge 💬 Commented View review

Both previously open findings are addressed, and I found no remaining actionable defects in the changed behavior.

The jobs worker now republishes state and log callbacks when they contend for the deployment lock, with a bounded requeue count carried in the jobs-message envelope. The new end-to-end tests cover eventual callback application after lock release and dead-lettering after the retry cap, using a helper to read the project sequence and hold the Redis lock.

Note

@Arnav-1456 does not have write access to this repository, so Hansi does not approve automatically. A maintainer can review and approve.

Walkthrough (4)
File Change
src/Appwrite/Event/Message/Jobs.php Adds an optional attempts counter to the jobs callback envelope.
src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php Requeues contended state and log callbacks through the jobs publisher, capped at three requeues.
tests/e2e/Services/Functions/FunctionsCustomServerTest.php Adds end-to-end tests for callback recovery and exhausting the requeue cap, plus Redis lock helpers.
tests/resources/functions/project-sequence.php Adds a subprocess helper that reads a project's internal sequence for the lock tests.
Fixed since the last review (2)
  • Log callbacks still dead-letter on cross-process contention · src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php:80
  • Launch the test helper without a shell command string · tests/e2e/Services/Functions/FunctionsCustomServerTest.php:3344

Reviewed 072450a

Tip

Comment @appwrite review to re-run, or mention @appwrite with a question.

@appwrite appwrite Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · See the inline comments. Summary

@appwrite appwrite Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · See the inline comments. Summary

A callback (log batch leader, artifact, exit, complete) that cannot
acquire the per-deployment lock within LOCK_TIMEOUT (10s) threw
Contention, and on the Redis broker a rejection is final: the message
was dead-lettered to the failed list and never applied, while the
jobs-service does not resend (the ingest endpoint already answered 204).
A lost exit or complete callback could leave a deployment in building,
and a lost log batch lost its lines - the failed list grew by hundreds
of entries per deploy day (issue appwrite#15745).

Contention is raised before the callback body runs, so a contended
message has applied nothing and is safe to requeue as-is: catch it (for
the state callbacks in the action body and for the gated log batch
leader in log()), publish the same envelope back onto v1-jobs through
publisherForJobs, and let the queue provide the spacing. A requeued log
callback re-adds its lines on the next delivery, and the batch dedupe
marker keeps them from appending twice. Requeues are capped at
MAX_REQUEUES (3, i.e. four deliveries); beyond that the message is
dead-lettered on purpose via PermanentFailure, so a persistently stuck
lock cannot loop a message forever. A failed requeue enqueue surfaces as
a RuntimeException like the existing marker-save failures, instead of
losing the callback silently.

The envelope gains an attempts field (default 0) that only the worker
raises; the ingest endpoint and payloads already in queues are
unchanged. Within one worker process log callbacks keep batching at
appwrite#15881's gate and no longer contend with each other at all.

Two e2e regression tests cover the contention path: one holds the
deployment lock while a real build streams its callbacks and asserts the
build completes after the release, the other holds the lock past the cap
and asserts a callback dead-letters only after carrying the cap's
requeue count.
@Arnav-1456
Arnav-1456 force-pushed the fix-15745-jobs-callback-contention-requeue branch from 2d4f416 to 072450a Compare November 20, 2026 19:18

@appwrite appwrite Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Bug Report: Jobs worker drops deployment callbacks on lock contention, a deployment can hang in building

2 participants