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. --- tests/tasks/test_sender.py | 72 +++++++++++++++++++++++++++++++++++++++-- webmentions_ssg/tasks/sender.py | 15 +++++++++ 2 files changed, 84 insertions(+), 3 deletions(-) diff --git a/tests/tasks/test_sender.py b/tests/tasks/test_sender.py index 6b189ad..d43f36f 100644 --- a/tests/tasks/test_sender.py +++ b/tests/tasks/test_sender.py @@ -848,9 +848,7 @@ def test_sender_does_not_retry_before_due_even_after_restart( app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch ) -> None: identifier = create_sent_webmention( - app, - attempt_count=2, - next_retry_at=datetime.now(UTC) + timedelta(days=1), + app, attempt_count=2, next_retry_at=datetime.now(UTC) + timedelta(days=1) ) httpx_client, _, discover, _ = mock_sender_requests(sender_module, monkeypatch) sender_module.send_webmention.call_local(identifier) @@ -903,6 +901,74 @@ def test_sender_request_error_uses_same_retry_budget( assert webmention.next_retry_at is None +def test_send_webmention_skips_duplicate_while_locked( + app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch +) -> None: + identifier = create_sent_webmention(app) + _, _, discover, post = mock_sender_requests(sender_module, monkeypatch) + + # Simulate a first worker already sending this Webmention. The second + # worker must not make HTTP requests or consume an attempt. + with sender_module.huey.lock_task(f"send-webmention-{identifier}"): + sender_module.send_webmention.call_local(identifier) + + discover.assert_not_called() + post.assert_not_called() + with app.app_context(): + webmention = db.session.get(SentWebmention, identifier) + assert webmention is not None + assert webmention.attempt_count == 0 + assert webmention.processed_revision is None + + # Once the other worker releases the lock, a queued task can complete. + sender_module.send_webmention.call_local(identifier) + sender_module.send_webmention.call_local(identifier) + discover.assert_called_once() + post.assert_called_once() + + with app.app_context(): + webmention = db.session.get(SentWebmention, identifier) + assert webmention is not None + assert webmention.attempt_count == 1 + assert webmention.processed_revision == 1 + + +def test_send_webmention_lock_is_scoped_to_uuid( + app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch +) -> None: + first_id = create_sent_webmention(app) + other_target = "https://example.com/another-post" + + with app.app_context(): + first = db.session.get(SentWebmention, first_id) + assert first is not None + second = SentWebmention( + source_id=first.source_id, target=other_target, desired_revision=1 + ) + db.session.add(second) + db.session.commit() + second_id = second.uuid + + _, _, discover, post = mock_sender_requests(sender_module, monkeypatch) + + with sender_module.huey.lock_task(f"send-webmention-{first_id}"): + sender_module.send_webmention.call_local(first_id) + sender_module.send_webmention.call_local(second_id) + + discover.assert_called_once() + post.assert_called_once() + assert post.call_args.kwargs["target"] == other_target + + with app.app_context(): + first = db.session.get(SentWebmention, first_id) + second = db.session.get(SentWebmention, second_id) + assert first is not None and second is not None + assert first.attempt_count == 0 + assert first.processed_revision is None + assert second.attempt_count == 1 + assert second.processed_revision == 1 + + def test_send_webmention_ignores_processed_revision( app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch ) -> None: 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