Skip to content

fix: cancel downloads when the userscript panel or tab closes - #108

Merged
D3SOX merged 2 commits into
masterfrom
fix-cancel-on-panel-tab-close
Aug 12, 2026
Merged

fix: cancel downloads when the userscript panel or tab closes#108
D3SOX merged 2 commits into
masterfrom
fix-cancel-on-panel-tab-close

Conversation

@D3SOX

@D3SOX D3SOX commented Aug 12, 2026

Copy link
Copy Markdown
Owner

Closing the userscript panel or SoundCloud tab while Chromium was starting often left the download running. Cancel from the host page used a normal fetch to localhost, which Chrome blocks via Private Network Access, and blanking the iframe raced the embedded UI’s unload handler. Separately, close() during an in-flight browser launch was a no-op, so the process that finished starting could stay orphaned.

This change routes host cancel through GM_xmlhttpRequest, cancels on pagehide/beforeunload before tearing down the iframe, hardens the WebUI unload path with keepalive fetch, and adds CancellableBrowserLaunch so cancel waits out launch and closes that browser.

Test plan

  • Start a gate download, close the userscript panel while status is “Launching browser…”, confirm the job cancels and Chromium exits
  • Start a gate download, close the SoundCloud tab while the browser is up, confirm cancel reaches the API
  • Cancel via the in-UI Cancel button still works as before
  • Update/reinstall userscript 1.11.4 before testing the host-side path
  • bun test for cancellation / browser launch / userscript suites

Implemented with Composer in Cursor.

Summary by CodeRabbit

  • Bug Fixes
    • Improved cancellation during browser startup to prevent orphaned browser processes.
    • Downloads now shut down more reliably when cancelled or closed.
    • Active downloads are cancelled before page and iframe teardown.
    • Added cancellation handling when leaving or unloading the page.
  • Tests
    • Expanded coverage for browser-launch cancellation, downloader cleanup, and page-exit cancellation.
  • Updates
    • Updated the userscript version to 1.11.5.

Closing the panel/tab while Chromium was starting could miss cancel because page fetch is blocked by Private Network Access and mid-launch close was a no-op.
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: f4e768f1-671c-4020-9b0e-2ec09b1b63b2

📥 Commits

Reviewing files that changed from the base of the PR and between 22a7b74 and a409ef7.

📒 Files selected for processing (2)
  • userscript/sc-gate-dl.test.ts
  • userscript/sc-gate-dl.user.js
🚧 Files skipped from review as they are similar to previous changes (2)
  • userscript/sc-gate-dl.user.js
  • userscript/sc-gate-dl.test.ts

📝 Walkthrough

Walkthrough

The change adds cancellable browser lifecycle management to provider downloaders. It also adds active-job cancellation during userscript panel close and page lifecycle events, with tests for launch cancellation, downloader shutdown ordering, and cancellation request sequencing.

Changes

Cancellable browser lifecycle

Layer / File(s) Summary
Cancellable browser launch control
src/browserLaunch.ts, src/browserLaunch.test.ts
CancellableBrowserLaunch tracks in-flight launches, rejects cancelled launches, closes late-launched browsers, and suppresses cleanup errors. Tests cover cancellation during and before launch.
Downloader browser lifecycle wiring
src/downloadgater.ts, src/droploud.ts, src/gaterush.ts, src/hypeddit.ts, src/mypresskit.ts, src/pumpyoursound.ts, src/stillhype.ts, src/gateCancellation.test.ts
Provider downloaders use CancellableBrowserLaunch for browser startup and shutdown. Tests cover provider cancellation and direct-downloader close ordering.
Userscript and Web UI job teardown
userscript/sc-gate-dl.user.js, userscript/sc-gate-dl.test.ts, webui/src/components/App.tsx
Active jobs are cancelled through the GM bridge or keepalive fetch during panel close, pagehide, and beforeunload. Iframe cleanup follows cancellation. The userscript version changes to 1.11.5.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

  • D3SOX/sc-gate-dl#85: Adds the browser-launching code extended here with cancellable lifecycle management.
  • D3SOX/sc-gate-dl#89: Implements related download cancellation and browser cleanup handling.
  • D3SOX/sc-gate-dl#97: Adds the StillhypeDownloader browser lifecycle code updated here to use CancellableBrowserLaunch.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.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 summarizes the primary change: cancelling downloads when the userscript panel or tab closes.
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 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-cancel-on-panel-tab-close

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 `@userscript/sc-gate-dl.test.ts`:
- Around line 190-203: Add an assertion in the active-job cancellation test for
the production `beforeunload` listener registration, alongside the existing
`pagehide` assertion, so removal of either host-unload handler fails the test.

In `@userscript/sc-gate-dl.user.js`:
- Around line 1240-1249: Update the closePanel cleanup flow around
gmXmlHttpRequest so synchronous bridge exceptions are caught and do not reject
the operation. Treat the request as best-effort, allowing the subsequent iframe
reset and panel hiding logic to execute even when the GM bridge throws.
🪄 Autofix

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 Plus

Run ID: 837c6a43-38a3-4173-8d28-45313a730e75

📥 Commits

Reviewing files that changed from the base of the PR and between 1e97ac6 and 22a7b74.

📒 Files selected for processing (13)
  • src/browserLaunch.test.ts
  • src/browserLaunch.ts
  • src/downloadgater.ts
  • src/droploud.ts
  • src/gateCancellation.test.ts
  • src/gaterush.ts
  • src/hypeddit.ts
  • src/mypresskit.ts
  • src/pumpyoursound.ts
  • src/stillhype.ts
  • userscript/sc-gate-dl.test.ts
  • userscript/sc-gate-dl.user.js
  • webui/src/components/App.tsx

Comment thread userscript/sc-gate-dl.test.ts
Comment thread userscript/sc-gate-dl.user.js Outdated
Keep panel teardown moving if the cancel bridge throws, and assert both unload listeners in the regression test.
@D3SOX

D3SOX commented Aug 12, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai full review

@D3SOX
D3SOX merged commit a726f95 into master Aug 12, 2026
2 checks passed
@D3SOX
D3SOX deleted the fix-cancel-on-panel-tab-close branch August 12, 2026 15:23
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.

1 participant