Add automatic retry mechanism for hotkey registration#8329
Add automatic retry mechanism for hotkey registration#8329tomstetson wants to merge 1 commit intoShareX:developfrom
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9257f5a787
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // Use retry mechanism to handle race conditions with other apps (e.g., OneDrive) | ||
| await Program.HotkeyManager.UpdateHotkeysWithRetryAsync(Program.HotkeysConfig.Hotkeys, !Program.IgnoreHotkeyWarning); |
There was a problem hiding this comment.
Avoid blocking InitHotkeys on retry delays
Awaiting UpdateHotkeysWithRetryAsync here means startup readiness is delayed by the full retry window whenever hotkey registration fails (e.g., OneDrive holding PrintScreen). Because IsReady isn’t set until InitHotkeys completes (see UpdateControls), WaitFormLoad(5000) in Program.cs gives up after 5 seconds and drops command-line arguments from a second instance if the retries take longer than that. This makes CLI invocations flaky specifically in the hotkey-conflict scenario the change is trying to improve. Consider running retries without blocking readiness (e.g., fire-and-forget or set IsReady before waiting), or otherwise ensuring WaitFormLoad still succeeds during long retries.
Useful? React with 👍 / 👎.
- Initial registration is synchronous (fast startup) - Retries run in background to avoid blocking WaitFormLoad - Fixes race condition with OneDrive claiming hotkeys at startup - Configurable via HotkeyRetryEnabled, HotkeyRetryCount, HotkeyRetryDelayMs - Values clamped to safe ranges to prevent DoS via malicious config
9257f5a to
6abad76
Compare
Addressed Codex Review FeedbackThanks for the review! I've updated the implementation to address the P2 - Avoid blocking InitHotkeys on retry delays feedback. Changes Made:
Result:
|
Summary
HotkeyRetryEnabled,HotkeyRetryCount,HotkeyRetryDelayMsUpdateHotkeys()method preservedProblem
When ShareX starts, it immediately attempts to register global hotkeys using the Win32
RegisterHotKeyAPI. Other applications like OneDrive often claim these same hotkeys (PrintScreen, Alt+PrintScreen) during their own startup sequence, causing:RegisterAllHotkeys()immediately at startupRegisterHotKeyreturnsfalse, hotkey status set toFailedNote: OneDrive claims these hotkeys even when its screenshot feature is disabled.
Solution
New Method:
UpdateHotkeysWithBackgroundRetry()IsReady- CLI invocations from second instances work immediatelyNew Configuration Settings (ApplicationConfig)
HotkeyRetryEnabledHotkeyRetryCountHotkeyRetryDelayMsSecurity Considerations
Settings are clamped to safe ranges to prevent DoS via malicious config files:
Files Changed
ShareX/ApplicationConfig.csShareX/HotkeyManager.csUpdateHotkeysWithBackgroundRetry(),RetryFailedHotkeysInBackgroundAsync(), andHasFailedHotkeys()ShareX/Forms/MainForm.csInitHotkeys()to use non-blocking retry methodRelated Issues