Add more robust timeout handling for RSS feed parsing to prevent hanging feeds

This commit is contained in:
Jarian Cottingham 2026-01-31 11:27:14 -06:00
parent a07c637984
commit d5625605ad

View File

@ -10,7 +10,7 @@ from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support import expected_conditions as EC
from concurrent.futures import ThreadPoolExecutor, as_completed from concurrent.futures import ThreadPoolExecutor, as_completed, TimeoutError
import nltk import nltk
from nltk.downloader import Downloader from nltk.downloader import Downloader
@ -76,8 +76,21 @@ def mine_all_articles(rss_feed_sources, limit=None):
"""Parse a single RSS feed with timeout and error handling""" """Parse a single RSS feed with timeout and error handling"""
try: try:
logger.info(f"Parsing RSS feed: {data['rss_url']}") logger.info(f"Parsing RSS feed: {data['rss_url']}")
# Add more aggressive timeout settings # Add more aggressive timeout settings with fallback
feed = feedparser.parse(data["rss_url"], timeout=15) # 15 second timeout # Use a wrapper to ensure we don't hang indefinitely
import signal
def timeout_handler(signum, frame):
raise TimeoutError(f"Timeout parsing feed: {site}")
# Set up signal-based timeout (this is a fallback for truly hanging requests)
old_handler = signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(10) # 10 second alarm
feed = feedparser.parse(data["rss_url"], timeout=8) # 8 second timeout
signal.alarm(0) # Cancel the alarm
signal.signal(signal.SIGALRM, old_handler)
feed_entries = feed.entries[:limit] if limit else feed.entries feed_entries = feed.entries[:limit] if limit else feed.entries
entries = [] entries = []
@ -85,6 +98,9 @@ def mine_all_articles(rss_feed_sources, limit=None):
if "link" in entry and "title" in entry: if "link" in entry and "title" in entry:
entries.append((site, entry.title, entry.link)) entries.append((site, entry.title, entry.link))
return entries return entries
except TimeoutError as e:
logger.error(f"Timeout parsing RSS feed: {site} Error: {str(e)}")
return []
except Exception as e: except Exception as e:
logger.error(f"Error parsing RSS feed: {site} Error: {str(e)}") logger.error(f"Error parsing RSS feed: {site} Error: {str(e)}")
return [] return []