Repository navigation
fix(messaging): stop messages getting stranded in processing - #13091
Conversation
A message sits at 'processing' from the moment it is enqueued until the worker writes a terminal status. Any error in between stranded it there for good: a failed job is rejected to a dead-letter list rather than redelivered, and delete was blocked on that state. An unreadable message document was the common trigger. It fell through with every attribute unset, so the provider lookup rejected a null type and aborted the job with a misleading query error. - guard the message read in the worker - mark a message failed when the job dies before writing a status - allow deleting a processing message so stranded ones can be cleared
Greptile SummaryThis PR prevents external messaging jobs from remaining indefinitely in
Confidence Score: 4/5This PR does not yet appear safe to merge because deleting a processing message can still race an active worker and permit an untracked external delivery. The terminal-status preservation fix addresses the cleanup-overwrite thread, but the delete endpoint still removes processing messages without coordinating with workers that may already hold an in-memory copy and proceed to send before their final database update fails. Files Needing Attention: app/controllers/api/messaging.php; src/Appwrite/Platform/Workers/Messaging.php Important Files Changed
Reviews (5): Last reviewed commit: "Merge branch 'main' into fix-messaging-s..." | Re-trigger Greptile |
Delivery is followed by attachment cleanup, which throws on its own when a bucket or file backing an attachment is gone. Reaching the fallback with a status already written rewrote a delivered message to failed and attributed a storage error to it, while it kept its delivery count and timestamp. Skip the fallback write once the message holds a terminal status.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Drop the note describing the old fall-through, which the code no longer does, and shorten the rest to match the density of the surrounding file.
What does this PR do?
A message sits at
processingfrom the moment it is enqueued until the worker writes a terminal status. Any error in between stranded it there permanently — and because delete was also blocked on that state, the message could never be updated or removed through the API.The trigger.
sendExternalMessageis handed whatevergetDocument('messages', $messageId)returns, with noisEmpty()guard. An unreadable document falls through with every attribute unset:$statusis null so the idempotency guard doesn't catch it, and$providerTypeis null by the time we reach the provider lookup —findOne('providers', [Query::equal('type', [null])])then throwsInvalid query: Query value is invalid for attribute "type", three lines before the firststatus => failedwrite.providerTypeis hardcoded at create time for all three message types, so a null value always means the worker read the message back and did not find it.This is live on production today. From
worker-messagingover a 10h window, 5 errors, all identical:{"level":"error","action":"worker.messaging","message.type":"external", "message.provider_type":null, "error.type":"Utopia\\Database\\Exception\\Query", "error.message":"Invalid query: Query value is invalid for attribute \"type\"", "error.line":8529,"span.duration":0.035}Why it is permanent. The queue does not redeliver.
Adapter::process()rejects a failed job, andBroker\Redis::reject()pushes the pid onto{namespace}.failed.{queue}— a dead-letter list. Nothing ever revisits the message.The change:
Message not found: <id>instead of a misleading query error further down.MESSAGE_ALREADY_SCHEDULED.Update remains blocked on
processing— mutating a message mid-send is a different question from clearing one, and nothing here needs it.Test Plan
testDeleteMessageasserted that deleting a message handed straight to the worker returns 400. That assertion encoded the behaviour being fixed, so it now asserts 204, and the POST that sets it up asserts 201 rather than going unchecked. The 404 case is unchanged.Not run locally — the suite needs the full stack plus an email DSN, without which
testDeleteMessageskips atsetupSentEmailData. Relying on CI. Pint passes on all three files; PHPStan reports only a pre-existing error in an unrelated VCS file.Worth a reviewer's eye:
buildMessageatMessaging.php:645sits outside the try/catch at:654, inside abatch()task. If it throws, the exception escapes the task coroutine rather than the job, so the new catch never sees it.Semaphore::withLockreleases viafinallyso no lock leaks, but I could not verify locally whetherbatch()deadlocks or silently drops the result. Left alone here.Related PRs and Issues
Checklist