Skip to content

Commit 556ac52

Browse files
committed
fix(document): improve cancellation responsiveness in polling loop (Issue #10)
Added defensive abort check after sleep() completes for faster cancellation response. Changes: - Added explicit abortSignal check after sleep() in pollDocumentStatus() - Exits immediately if cancelled during sleep instead of waiting for next iteration - Avoids unnecessary poll interval calculation when operation is cancelled Technical Details: - sleep() method already has isSettled flag preventing double settlement (correct) - This change adds post-await check for immediate cancellation detection - Catches edge case where abort happens just as sleep timeout completes - Defensive programming pattern improves responsiveness to Ctrl+C Impact: - More responsive cancellation behavior - Exits faster when user cancels document translation - No functional changes - all 1454 tests pass Location: src/services/document-translation.ts:182-186
1 parent 6f40518 commit 556ac52

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6262
- **Performance Trade-off**: Minimal impact on throughput; CLI rarely needs >10 concurrent connections
6363
- Location: `src/api/deepl-client.ts:133-147` (HTTP/HTTPS agent configuration)
6464

65+
### Fixed
66+
- **Document Translation Cancellation Responsiveness** - Improved abort handling after sleep (Issue #10)
67+
- Added defensive check after sleep() completes to detect cancellation immediately
68+
- Previously, cancellation detection waited until next polling iteration
69+
- Now exits immediately if AbortSignal was triggered during sleep
70+
- sleep() method already had isSettled flag to prevent double settlement (correct)
71+
- This change adds explicit abort check after await completes for faster response
72+
- Avoids unnecessary poll interval calculation when operation is cancelled
73+
- **Impact**: More responsive cancellation behavior; exits faster when Ctrl+C pressed
74+
- **Technical Detail**: Defensive programming - catches edge case where abort happens just as sleep completes
75+
- Location: `src/services/document-translation.ts:182-186` (post-sleep abort check)
76+
6577
### Security
6678
- **Symlink Path Validation** - Prevents directory traversal attacks (Issue #6)
6779
- Rejects symlinks at translate() entry point before processing files

src/services/document-translation.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ export class DocumentTranslationService {
178178

179179
// Wait before next poll with exponential backoff
180180
await this.sleep(pollInterval, abortSignal);
181+
182+
// Issue #10: Defensive check after sleep completes
183+
// Exit immediately if cancelled during sleep instead of calculating new interval
184+
if (abortSignal?.aborted) {
185+
throw new Error('Document translation cancelled');
186+
}
187+
181188
pollInterval = Math.min(
182189
pollInterval * this.BACKOFF_MULTIPLIER,
183190
this.MAX_POLL_INTERVAL

0 commit comments

Comments
 (0)