Skip to content

Suppress cancel-error dialog in auto-translate#11113

Merged
niksedk merged 2 commits into
mainfrom
fix/auto-translate-cancel-error
May 23, 2026
Merged

Suppress cancel-error dialog in auto-translate#11113
niksedk merged 2 commits into
mainfrom
fix/auto-translate-cancel-error

Conversation

@niksedk
Copy link
Copy Markdown
Member

@niksedk niksedk commented May 23, 2026

Summary

When the user pressed Cancel during auto-translate (reported with llama.cpp, but applies to every engine that honors the cancellation token), the HttpClient call inside the translator threw OperationCanceledException / TaskCanceledException. The catch-all in AutoTranslateViewModel.DoTranslate surfaced this as a TranslationErrorWindow with a stack trace, even though the user just clicked Cancel.

Add a guarded catch for user-initiated cancellation:

```csharp
catch (OperationCanceledException) when (_abort)
{
// User pressed Cancel — let the finally block report it; do not surface as an error.
}
```

  • TaskCanceledException derives from OperationCanceledException, so this covers both shapes.
  • The existing finally block still sets StatusText = TranslationCancelled and re-enables the UI.
  • The when (_abort) guard means a stray non-user cancellation (e.g. an internal client timeout that we didn't initiate) still hits the regular error dialog, so genuine failures aren't masked.

Test plan

  • Builds clean.
  • Start a llama.cpp auto-translation, press Cancel mid-stream — verify no error dialog appears and the status text reads "Translation cancelled".
  • Repeat with another engine (e.g. OpenAI / DeepSeek) that honors the token, to confirm the fix applies broadly.

🤖 Generated with Claude Code

When the user presses Cancel during translation, the HttpClient call
throws OperationCanceledException (or TaskCanceledException) which the
generic catch in DoTranslate surfaced as a translation-error dialog
with a stack trace. Cancel should be silent — the finally block
already sets StatusText to "Translation cancelled".

Add a guarded catch for user-initiated cancellation; genuine
cancellation-shaped errors still fall through to the existing handler.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adjusts the auto-translate workflow so that user-initiated cancellation (Cancel button) no longer surfaces as a translation error dialog when the underlying translator throws OperationCanceledException / TaskCanceledException.

Changes:

  • Add a guarded catch (OperationCanceledException) when (_abort) to suppress the error dialog on user cancellation.
  • Rely on the existing finally block to report “Translation cancelled” and restore UI state.

}

}
catch (OperationCanceledException) when (_abort)
Cancel() called _cancellationTokenSource.Cancel() before setting
_abort = true, so the worker thread could observe cancellation and
throw OperationCanceledException before _abort was set (and even
when ordering is right, the non-volatile bool write isn't guaranteed
visible across threads).

- Set _abort before cancelling the token, so intent is recorded first.
- Widen the catch guard to also check the CTS's IsCancellationRequested,
  which is internally synchronized and race-free.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@niksedk niksedk merged commit 74c894f into main May 23, 2026
2 of 3 checks passed
@niksedk niksedk deleted the fix/auto-translate-cancel-error branch May 23, 2026 06:38
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.

3 participants