Skip to content

feat(feeds): add per-channel timeout to FEED_REFRESH loop#892

Open
jollySleeper wants to merge 1 commit into
TeamPiped:masterfrom
jollySleeper:feat/feed-refresh-channel-timeout
Open

feat(feeds): add per-channel timeout to FEED_REFRESH loop#892
jollySleeper wants to merge 1 commit into
TeamPiped:masterfrom
jollySleeper:feat/feed-refresh-channel-timeout

Conversation

@jollySleeper

@jollySleeper jollySleeper commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Wraps each channel refresh in the FEED_REFRESH loop with a CompletableFuture and a 60-second timeout
  • ChannelInfo.getInfo() makes multiple HTTP calls per channel — even though the downloader has a 10s per-request timeout, the aggregate time for one channel can exceed that significantly
  • A single hanging channel currently blocks the entire refresh loop indefinitely; with this change it gets skipped with a log message

Context

PR #889 introduced the FEED_REFRESH periodic channel-refresh loop. The loop processes channels serially with paced delays, which is the right model for being a good citizen to YouTube's API. However, it has no per-channel time ceiling — if one channel's extraction hangs (network issues, YouTube serving unusual responses, etc.), the entire loop stalls.

This adds a lightweight safety net: each channel gets up to 60 seconds to complete, which is generous given the 10s HTTP timeout, but prevents indefinite blocking.

Changes

  • Main.java: Wrap per-channel refresh in CompletableFuture.runAsync(...).get(60, TimeUnit.SECONDS)
  • On TimeoutException: log and skip to next channel
  • On ExecutionException: unwrap cause and pass to existing ExceptionHandler
  • On InterruptedException: restore interrupt flag and exit loop cleanly

Test plan

  • Build compiles cleanly (./gradlew build)
  • Deploy with FEED_REFRESH=true and verify normal refresh cycle completes
  • Verify timeout log message appears if a channel takes >60s (can be tested by temporarily lowering timeout)

Summary by CodeRabbit

  • Bug Fixes
    • Improved feed refresh stability by limiting individual channel updates to a 60-second timeout.
    • Slow or stalled channels are now skipped instead of blocking the entire refresh process.
    • Refresh interruptions and failures are handled more gracefully, helping the app recover more reliably.

Wrap each channel refresh in a CompletableFuture with a 60-second
timeout. ChannelInfo.getInfo() can make multiple HTTP calls per channel,
so even though the downloader has a 10s per-request timeout, the
aggregate time for one channel can exceed that significantly. A single
hanging channel currently blocks the entire refresh loop indefinitely.

With this change, if a channel refresh exceeds 60 seconds, it is
skipped with a log message and the loop moves to the next channel.
@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The feed refresh loop now runs each channel update asynchronously with a 60-second wait, logs timed-out channels, unwraps execution failures to the exception handler, and stops the loop when interrupted.

Changes

Feed refresh timeout handling

Layer / File(s) Summary
Async per-channel refresh
src/main/java/me/kavin/piped/Main.java
Main adds the concurrency imports and changes channel refresh execution to CompletableFuture.runAsync with a 60-second timeout, timeout logging, unwrapped execution failure handling, and interrupt-aware loop exit.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

(_/)
(•_•) A timeout hop, a feeder run,
/ >🥕 Each channel gets its async bun;
The errors tumble, ears stay keen,
The interrupt says, “pause the scene,”
And moonlit refreshes softly hum.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding a per-channel timeout to the FEED_REFRESH loop.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/main/java/me/kavin/piped/Main.java`:
- Around line 301-303: The interruption handling in Main’s refresh loop only
breaks the inner for-loop, so the surrounding while(true) can continue after
Thread.currentThread().interrupt(). Update the InterruptedException catch in the
refresh thread logic to exit the entire refresh thread cleanly when interrupted,
using the refresh loop around the DB query/refresh pass as the target to stop
rather than only the inner loop.
- Around line 34-36: The feed refresh job in Main.refresh needs to be truly
cancellable instead of only timing out the wait on
CompletableFuture.runAsync(...).get(60, TimeUnit.SECONDS). Run the refresh work
on an interruptible task/thread, keep a handle to it, and explicitly cancel or
interrupt it when the timeout is hit so the work stops before mutating channel
state. Also update the InterruptedException handling in the refresh loop so it
exits the outer refresh flow, not just the inner channel loop, when an interrupt
is received.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3931f627-dc7d-4038-a176-8d0394dd63cc

📥 Commits

Reviewing files that changed from the base of the PR and between 6d0ad06 and 84903b0.

📒 Files selected for processing (1)
  • src/main/java/me/kavin/piped/Main.java

Comment on lines +34 to +36
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major

🧩 Analysis chain

🌐 Web query:

In Java 21, does CompletableFuture.get(long, TimeUnit) cancel or interrupt the asynchronous computation on TimeoutException, and does FutureTask.cancel(true) interrupt the runner?

💡 Result:

In Java 21, the behavior is as follows: 1. CompletableFuture.get(long, TimeUnit) and TimeoutException: Calling CompletableFuture.get(long, TimeUnit) does not cancel or interrupt the underlying asynchronous computation when a TimeoutException is thrown [1][2]. The TimeoutException only indicates that the calling thread has stopped waiting for the result; the task continues to execute in the background [1][2]. To stop the computation, you must explicitly call cancel(true) on the CompletableFuture instance after catching the TimeoutException [1][2]. Alternatively, you can use the non-blocking orTimeout(long, TimeUnit) method to cause the CompletableFuture to complete exceptionally with a TimeoutException if it does not finish within the specified time [3][4]. 2. FutureTask.cancel(true) and the Runner: Yes, FutureTask.cancel(true) attempts to interrupt the thread currently executing the task [5][6]. If the task has already started, passing true to the cancel method signals that the thread running the task should be interrupted [5][6]. The implementation sets the task state to INTERRUPTING and calls thread.interrupt on the runner [7][8]. However, the success of this interruption depends on whether the task code is responsive to interrupts (e.g., it checks the interrupted status or calls blocking methods that throw InterruptedException) [1][2][9].

Citations:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the relevant section of Main.java with line numbers
file="src/main/java/me/kavin/piped/Main.java"
wc -l "$file"
sed -n '260,320p' "$file" | cat -n

Repository: TeamPiped/Piped-Backend

Length of output: 4114


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Show the imports and the feed refresh block in Main.java
file="src/main/java/me/kavin/piped/Main.java"
sed -n '1,60p' "$file" | cat -n
printf '\n---\n'
sed -n '280,315p' "$file" | cat -n

Repository: TeamPiped/Piped-Backend

Length of output: 5590


Make the feed refresh job cancellable.

  • CompletableFuture.runAsync(...).get(60, TimeUnit.SECONDS) only times out the waiter; the refresh keeps running on the common pool and can still mutate channel state after being skipped. Use an interruptible task/thread and cancel it on timeout.
  • The current InterruptedException handler only breaks the inner channel loop, so an interrupt does not stop the outer refresh loop.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/main/java/me/kavin/piped/Main.java` around lines 34 - 36, The feed
refresh job in Main.refresh needs to be truly cancellable instead of only timing
out the wait on CompletableFuture.runAsync(...).get(60, TimeUnit.SECONDS). Run
the refresh work on an interruptible task/thread, keep a handle to it, and
explicitly cancel or interrupt it when the timeout is hit so the work stops
before mutating channel state. Also update the InterruptedException handling in
the refresh loop so it exits the outer refresh flow, not just the inner channel
loop, when an interrupt is received.

Comment on lines +301 to +303
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Exit the refresh thread on interruption.

Line 303 only breaks out of the for loop. Because the interrupt flag is restored, the surrounding while (true) can immediately start another DB query/refresh pass instead of shutting down cleanly.

Proposed fix
                             } catch (InterruptedException e) {
                                 Thread.currentThread().interrupt();
-                                break;
+                                return;
                             }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/main/java/me/kavin/piped/Main.java` around lines 301 - 303, The
interruption handling in Main’s refresh loop only breaks the inner for-loop, so
the surrounding while(true) can continue after
Thread.currentThread().interrupt(). Update the InterruptedException catch in the
refresh thread logic to exit the entire refresh thread cleanly when interrupted,
using the refresh loop around the DB query/refresh pass as the target to stop
rather than only the inner loop.

@FireMasterK

Copy link
Copy Markdown
Member

QQ: Have you investigated why the ChannelInfo.getInfo call times out in the first place? Do you have a stack trace/thread dump when it was stuck?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants