Skip to content

fix: prevent crash when clipboard is locked by another application#3562

Open
Jefsky wants to merge 1 commit into
shadowsocks:v4from
Jefsky:fix-clipboard-crash
Open

fix: prevent crash when clipboard is locked by another application#3562
Jefsky wants to merge 1 commit into
shadowsocks:v4from
Jefsky:fix-clipboard-crash

Conversation

@Jefsky

@Jefsky Jefsky commented Jun 3, 2026

Copy link
Copy Markdown

Problem

When another application holds the clipboard lock, calling Clipboard.SetText, Clipboard.GetText, or Clipboard.SetDataObject throws a COMException (error code 0x800401D0 - Clipboard can not be opened) and crashes the application.

Fix

Added safe wrapper methods around all clipboard operations with retry logic:

  • SetClipboardTextSafe — wraps Clipboard.SetText in ServerSharingViewModel and OnlineConfigViewModel
  • GetClipboardTextSafe — wraps Clipboard.GetText in MenuViewController for URL import
  • SetClipboardDataObjectSafe — wraps Clipboard.SetDataObject in ShadowsocksController for PAC URL copy

Each wrapper retries up to 5 times with 50ms delay, gracefully failing silently instead of crashing.

Files Changed

  • shadowsocks-csharp/ViewModels/ServerSharingViewModel.cs — QR code link copy
  • shadowsocks-csharp/ViewModels/OnlineConfigViewModel.cs — online config link copy
  • shadowsocks-csharp/View/MenuViewController.cs — URL import from clipboard
  • shadowsocks-csharp/Controller/ShadowsocksController.cs — PAC URL copy

Test Plan

  1. Open another app that holds the clipboard (e.g., a terminal with copied text)
  2. Try all 4 clipboard operations in Shadowsocks:
    • Share Server Config → Copy Link
    • Online Config → Copy Link
    • Servers → Import URL from Clipboard
    • PAC → Copy Local PAC URL
  3. App should not crash

Wrap all clipboard access (SetText, GetText, SetDataObject) with retry
logic to prevent COMException crash when clipboard is busy.

- ServerSharingViewModel: SetClipboardTextSafe for QR code copy
- OnlineConfigViewModel: SetClipboardTextSafe for link copy
- MenuViewController: GetClipboardTextSafe for URL import
- ShadowsocksController: SetClipboardDataObjectSafe for PAC URL copy

Fixes shadowsocks#3561
@Jefsky

Jefsky commented Jun 9, 2026

Copy link
Copy Markdown
Author

Friendly bump — the PR has been sitting for a bit. The fix wraps clipboard access in try/catch across 4 sites (ShadowsocksController, MenuViewController, OnlineConfigViewModel, ServerSharingViewModel) to prevent the crash when the clipboard is locked by another app (~116 lines, 4 files). CI is green. Happy to address any feedback or split the changes per file if reviewers prefer.

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