From 5efd91baa46a2498697070efb169fa16ba8ea0cf Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Tue, 17 Feb 2026 00:54:42 +0100 Subject: [PATCH] Dont background thread so we dont break tests --- changedetectionio/blueprint/ui/__init__.py | 26 ++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/changedetectionio/blueprint/ui/__init__.py b/changedetectionio/blueprint/ui/__init__.py index 75285d462..f24c60fea 100644 --- a/changedetectionio/blueprint/ui/__init__.py +++ b/changedetectionio/blueprint/ui/__init__.py @@ -194,9 +194,9 @@ def construct_blueprint(datastore: ChangeDetectionStore, update_q, worker_pool, tag_limit = request.args.get('tag') now = int(time.time()) - # Mark watches as viewed in background thread to avoid blocking - def mark_viewed_background(): - """Background thread to mark watches as viewed - discarded after completion.""" + # Mark watches as viewed - use background thread only for large watch counts + def mark_viewed_impl(): + """Mark watches as viewed - can run synchronously or in background thread.""" marked_count = 0 try: for watch_uuid, watch in datastore.data['watching'].items(): @@ -209,15 +209,23 @@ def construct_blueprint(datastore: ChangeDetectionStore, update_q, worker_pool, datastore.set_last_viewed(watch_uuid, now) marked_count += 1 - logger.info(f"Background marking complete: {marked_count} watches marked as viewed") + logger.info(f"Marking complete: {marked_count} watches marked as viewed") except Exception as e: - logger.error(f"Error in background mark as viewed: {e}") + logger.error(f"Error marking as viewed: {e}") - # Start background thread and return immediately - thread = threading.Thread(target=mark_viewed_background, daemon=True) - thread.start() + # For small watch counts (< 10), run synchronously to avoid race conditions in tests + # For larger counts, use background thread to avoid blocking the UI + watch_count = len(datastore.data['watching']) + if watch_count < 10: + # Run synchronously for small watch counts + mark_viewed_impl() + flash(gettext("Marked as viewed.")) + else: + # Start background thread for large watch counts + thread = threading.Thread(target=mark_viewed_impl, daemon=True) + thread.start() + flash(gettext("Marking watches as viewed in background...")) - flash(gettext("Marking watches as viewed in background...")) return redirect(url_for('watchlist.index', tag=tag_limit)) @ui_blueprint.route("/delete", methods=['GET'])