From d43361d50509807a3bc12f742fbf5244cf40afdf Mon Sep 17 00:00:00 2001 From: Phil Date: Tue, 3 Nov 2026 07:41:06 -0700 Subject: [PATCH] fix: Webmention worker runs inside the app context (v2.6.8) The worker thread called the SSRF checks, which read current_app.config, without an app context, so every incoming and outgoing webmention failed with 'Working outside of application context'. A teardown_appcontext hook also stopped the worker whenever a request ended. Pass the app to WebmentionWorker and run each poll in app.app_context(), like SyndicationWorker, and drop the teardown hook. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 7 ++ ...026-11-03-webmention-worker-app-context.md | 37 ++++++++++ starpunk/__init__.py | 11 +-- starpunk/webmention_worker.py | 13 ++-- tests/test_webmention_worker.py | 72 +++++++++++++++++++ 5 files changed, 127 insertions(+), 13 deletions(-) create mode 100644 docs/design/v2.6.8/2026-11-03-webmention-worker-app-context.md create mode 100644 tests/test_webmention_worker.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 9abf547..d235afa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.6.8] - 2026-11-03 + +### Fixed + +- Webmentions were never processed. The background worker checked URLs against this site's settings without the app loaded, so every incoming webmention was marked "error" ("Working outside of application context") and every outgoing one kept failing and retrying. The worker now runs with the app loaded. A received webmention showed up in the admin list but never on the note +- The webmention worker also stopped as soon as any request finished, so after startup it often wasn't running at all. It now runs for the life of the server process + ## [2.6.7] - 2026-11-02 ### Fixed diff --git a/docs/design/v2.6.8/2026-11-03-webmention-worker-app-context.md b/docs/design/v2.6.8/2026-11-03-webmention-worker-app-context.md new file mode 100644 index 0000000..8b9ed56 --- /dev/null +++ b/docs/design/v2.6.8/2026-11-03-webmention-worker-app-context.md @@ -0,0 +1,37 @@ +# v2.6.8: Webmention worker app context + +## Problem + +Production webmention #1 (Bridgy, reply from Mastodon to note 20261101162512) +was stored with `validation_status = 'error'`: +"Working outside of application context". Both outgoing queue items had the +same error and were on their last retry. + +## Causes + +1. `webmention_ssrf.safe_resolve()` reads `current_app.config['DEV_MODE']`. + `WebmentionWorker` is a plain thread with no Flask app context, so every + `validate_url_for_fetch()` call raised `RuntimeError`, for both + validation (incoming) and endpoint discovery and sending (outgoing). +2. `create_app()` registered a `teardown_appcontext` handler that called + `webmention_worker.stop()`. Teardown runs whenever any app context ends + (every request), so the worker stopped soon after startup. + +## Fix + +- `WebmentionWorker` takes the Flask app and runs each poll inside + `app.app_context()`, as `SyndicationWorker` already does. +- The teardown handler is removed. The worker is a daemon thread and ends with + the process. + +## Tests + +`tests/test_webmention_worker.py`: an outgoing send completes through the +worker thread, and the worker is still alive after an app context and a +request end. Both fail without the fix. + +## Production data + +After deploying, reset webmention 1 to `validation_status = 'pending'` and the +two queue items to `retry_count = 0, next_retry_at = NULL` so the worker +processes them again. diff --git a/starpunk/__init__.py b/starpunk/__init__.py index 65ddaf2..11d5f79 100644 --- a/starpunk/__init__.py +++ b/starpunk/__init__.py @@ -330,6 +330,7 @@ def create_app(config=None): if app.config.get('WEBMENTION_WORKER_ENABLED', True) and not app.config.get('TESTING', False): from starpunk.webmention_worker import WebmentionWorker webmention_worker = WebmentionWorker( + app, db_path=app.config['DATABASE_PATH'], site_url=app.config['SITE_URL'], poll_interval=app.config.get('WEBMENTION_POLL_INTERVAL', 10), @@ -345,12 +346,6 @@ def create_app(config=None): f"batch_size={webmention_worker.batch_size})" ) - # Register cleanup handler - @app.teardown_appcontext - def cleanup_webmention_worker(error=None): - if hasattr(app, 'webmention_worker') and app.webmention_worker.is_alive(): - app.webmention_worker.stop() - # Start syndication worker when a target is configured (v2.1.0) from starpunk.syndication import SyndicationWorker, targets_configured if targets_configured(app.config) and not app.config.get("TESTING", False): @@ -464,5 +459,5 @@ def create_app(config=None): # Package version (Semantic Versioning 2.0.0) # See docs/standards/versioning-strategy.md for details -__version__ = "2.6.7" -__version_info__ = (2, 6, 4) +__version__ = "2.6.8" +__version_info__ = (2, 6, 8) diff --git a/starpunk/webmention_worker.py b/starpunk/webmention_worker.py index 93b3586..5becfca 100644 --- a/starpunk/webmention_worker.py +++ b/starpunk/webmention_worker.py @@ -25,7 +25,6 @@ from lxml import html as lxml_html from starpunk.webmention_processor import process_webmention, ValidationError from starpunk.webmention_ssrf import validate_url_for_fetch, SSRFBlockedError -from flask import has_app_context logger = logging.getLogger(__name__) @@ -47,6 +46,7 @@ class WebmentionWorker(threading.Thread): def __init__( self, + app, db_path: Path, site_url: str, poll_interval: int = 10, @@ -58,6 +58,7 @@ class WebmentionWorker(threading.Thread): Initialize webmention worker thread Args: + app: Flask app (the SSRF checks read its config) db_path: Path to SQLite database site_url: Site URL for User-Agent header poll_interval: Seconds between queue checks (default: 10) @@ -66,6 +67,7 @@ class WebmentionWorker(threading.Thread): auto_approve: Auto-approve valid webmentions (default: True) """ super().__init__(daemon=True, name="webmention-worker") + self.app = app self.db_path = db_path self.site_url = site_url self.poll_interval = poll_interval @@ -100,11 +102,12 @@ class WebmentionWorker(threading.Thread): try: processed = 0 - # Process incoming webmentions - processed += self._process_pending_validations() + with self.app.app_context(): + # Process incoming webmentions + processed += self._process_pending_validations() - # Phase 3: Process outgoing webmentions - processed += self._process_pending_sends() + # Phase 3: Process outgoing webmentions + processed += self._process_pending_sends() # Update last processed time if processed > 0: diff --git a/tests/test_webmention_worker.py b/tests/test_webmention_worker.py new file mode 100644 index 0000000..31a6694 --- /dev/null +++ b/tests/test_webmention_worker.py @@ -0,0 +1,72 @@ +""" +Webmention worker regression tests (v2.6.8) + +The worker thread used Flask's current_app (via the SSRF checks) without an +app context, so every send and validation failed. It was also stopped +whenever any app context ended. +""" + +import sqlite3 +import time +from unittest.mock import MagicMock, patch + +from starpunk.webmention_worker import WebmentionWorker + + +def _wait_for(predicate, timeout=5): + deadline = time.time() + timeout + while time.time() < deadline: + if predicate(): + return True + time.sleep(0.05) + return False + + +def test_worker_sends_queued_webmention(app): + db_path = app.config["DATABASE_PATH"] + conn = sqlite3.connect(db_path) + conn.execute( + "INSERT INTO webmention_queue (source_url, target_url, status) " + "VALUES (?, ?, 'pending')", + ("http://localhost:5000/note/1", "https://example.org/post"), + ) + conn.commit() + conn.close() + + page = MagicMock(status_code=200, url="https://example.org/post", + headers={"Link": '; rel="webmention"'}) + accepted = MagicMock(status_code=202, text="") + + worker = WebmentionWorker(app, db_path=db_path, site_url=app.config["SITE_URL"], + poll_interval=0.05) + with patch("starpunk.webmention_ssrf.socket.gethostbyname", return_value="93.184.216.34"), \ + patch("starpunk.webmention_worker.httpx.get", return_value=page), \ + patch("starpunk.webmention_worker.httpx.post", return_value=accepted) as post: + worker.start() + try: + def status(): + c = sqlite3.connect(db_path) + row = c.execute("SELECT status, last_error FROM webmention_queue").fetchone() + c.close() + return row + assert _wait_for(lambda: status()[0] != "pending"), status() + assert status() == ("sent", None) + post.assert_called_once() + finally: + worker.stop() + worker.join(timeout=2) + + +def test_worker_survives_app_context_teardown(app): + worker = WebmentionWorker(app, db_path=app.config["DATABASE_PATH"], + site_url=app.config["SITE_URL"], poll_interval=0.05) + worker.start() + try: + with app.app_context(): + pass + app.test_client().get("/") + time.sleep(0.2) + assert worker.is_alive() + finally: + worker.stop() + worker.join(timeout=2)