fix: Webmention processing works end to end (v2.6.9)
- Normalise mf2 author values: u-photo with alt text is a dict in mf2py and broke the database update for most real senders - Pass str(response.url) to urljoin during endpoint discovery; the TypeError marked every target as having no endpoint. Retry on discovery errors - Hold a cross-process flock so only one gunicorn process runs the worker - Keep author URL/photo only if http(s) (XSS via javascript: URLs) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3c1e109358
commit
106495d114
6 files changed
+184
-18
No files matched your search
@@ -12,6 +12,7 @@ References:
|
||||
- ADR-063: Webmention Implementation
|
||||
"""
|
||||
|
||||
import fcntl
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
@@ -76,6 +77,7 @@ class WebmentionWorker(threading.Thread):
|
||||
self.auto_approve = auto_approve
|
||||
self._stop_event = threading.Event()
|
||||
self._last_processed = None
|
||||
self._lock_file = None
|
||||
|
||||
# Phase 3: Discovery cache (worker instance variable)
|
||||
# Per architect's answer to Q4
|
||||
@@ -94,11 +96,14 @@ class WebmentionWorker(threading.Thread):
|
||||
try:
|
||||
logger.info("Webmention worker started")
|
||||
|
||||
# Cleanup orphaned items from previous crashes
|
||||
self._cleanup_orphaned_items()
|
||||
|
||||
# Main processing loop
|
||||
while not self._stop_event.is_set():
|
||||
# Every gunicorn process starts a worker; only the one holding
|
||||
# the lock processes queues, so nothing is sent twice
|
||||
if not self._acquire_lock():
|
||||
self._stop_event.wait(self.poll_interval)
|
||||
continue
|
||||
|
||||
try:
|
||||
processed = 0
|
||||
|
||||
@@ -124,8 +129,40 @@ class WebmentionWorker(threading.Thread):
|
||||
except Exception as e:
|
||||
logger.error(f"Webmention worker thread failed: {e}", exc_info=True)
|
||||
finally:
|
||||
self._release_lock()
|
||||
logger.info("Webmention worker stopped")
|
||||
|
||||
def _acquire_lock(self) -> bool:
|
||||
"""
|
||||
Take the cross-process worker lock (non-blocking)
|
||||
|
||||
On first acquisition, also reset items left mid-send by a previous
|
||||
holder. The OS releases the lock if this process dies.
|
||||
|
||||
Returns:
|
||||
True if this worker holds the lock
|
||||
"""
|
||||
if self._lock_file:
|
||||
return True
|
||||
|
||||
lock_file = open(Path(self.db_path).parent / "webmention-worker.lock", "w")
|
||||
try:
|
||||
fcntl.flock(lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
except OSError:
|
||||
lock_file.close()
|
||||
return False
|
||||
|
||||
self._lock_file = lock_file
|
||||
logger.info("Webmention worker holds the queue lock")
|
||||
self._cleanup_orphaned_items()
|
||||
return True
|
||||
|
||||
def _release_lock(self):
|
||||
"""Release the cross-process worker lock if held"""
|
||||
if self._lock_file:
|
||||
self._lock_file.close()
|
||||
self._lock_file = None
|
||||
|
||||
def _cleanup_orphaned_items(self):
|
||||
"""
|
||||
Clean up orphaned 'sending' status items on startup
|
||||
@@ -510,7 +547,7 @@ class WebmentionWorker(threading.Thread):
|
||||
# 1. Check Link header
|
||||
link_header = response.headers.get('Link', '')
|
||||
if link_header:
|
||||
endpoint = self._parse_link_header(link_header, response.url)
|
||||
endpoint = self._parse_link_header(link_header, str(response.url))
|
||||
if endpoint:
|
||||
self._cache_endpoint(target_url, endpoint, hours=1)
|
||||
return endpoint
|
||||
@@ -518,7 +555,7 @@ class WebmentionWorker(threading.Thread):
|
||||
# 2. Parse HTML for <link> and <a> elements
|
||||
content_type = response.headers.get('Content-Type', '')
|
||||
if 'html' in content_type.lower():
|
||||
endpoint = self._parse_html_for_endpoint(response.text, response.url)
|
||||
endpoint = self._parse_html_for_endpoint(response.text, str(response.url))
|
||||
if endpoint:
|
||||
self._cache_endpoint(target_url, endpoint, hours=1)
|
||||
return endpoint
|
||||
@@ -529,9 +566,9 @@ class WebmentionWorker(threading.Thread):
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
# Don't cache; the caller schedules a retry
|
||||
logger.warning(f"Failed to discover endpoint for {target_url}: {e}")
|
||||
# Don't cache on error - allow retry
|
||||
return None
|
||||
raise
|
||||
|
||||
def _parse_link_header(self, link_header: str, base_url: str) -> Optional[str]:
|
||||
"""
|
||||
|
||||
Reference in new issue
Block a user