From 33567a493a18c3a8e17e3715874d49943da494a6 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Fri, 30 Oct 2026 13:38:00 +0100 Subject: fix(sender): prevent concurrent duplicate sends Serialize delivery attempts per Webmention so duplicate queued tasks cannot send the same revision concurrently or consume additional retry attempts. Scope locks by Webmention UUID so unrelated deliveries can still run in parallel. --- webmentions_ssg/tasks/sender.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'webmentions_ssg/tasks') diff --git a/webmentions_ssg/tasks/sender.py b/webmentions_ssg/tasks/sender.py index 4aa59ee..a4a8367 100644 --- a/webmentions_ssg/tasks/sender.py +++ b/webmentions_ssg/tasks/sender.py @@ -13,6 +13,7 @@ import httpx import sqlalchemy as sa from bs4 import BeautifulSoup from flask import current_app +from huey.exceptions import TaskLockedException from .. import APP_NAME, VERSION from .. import DATABASE as db @@ -308,6 +309,20 @@ def send_webmention(webmention_uuid: uuid.UUID) -> None: :param webmention_uuid: Identifier of the sent Webmention to process. """ + # Huey locks have static names when used as decorators. Acquire one keyed by + # UUID inside the task so different Webmentions can still send in parallel. + # The pending/retry checks must happen *under* this lock. + try: + with huey.lock_task(f"send-webmention-{webmention_uuid}"): + send_webmention_task(webmention_uuid) + except TaskLockedException: + # Another worker is already sending this Webmention. The active worker + # owns its result; a later scan will requeue it if it remains pending. + return + + +def send_webmention_task(webmention_uuid: uuid.UUID) -> None: + """Process a Webmention while holding its per-UUID task lock.""" webmention = db.session.get(SentWebmention, webmention_uuid) if webmention is None: -- cgit v1.3.1