Add timeout and cancellation support to prevent infinite hangs in Prettier formatting #3852
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Prettier can hang indefinitely when formatting malformed JSX (e.g., improperly self-closed tags), causing 100% CPU usage and "infinite saving" state with no way to cancel. This PR adds timeout protection and cancellation token support to prevent these infinite hangs.
Changes
Timeout Protection
prettier.timeoutsetting (default: 30s, set to 0 to disable)withTimeout()utility with proper cleanup via try-finally to prevent memory leaksPrettierMainThreadInstance(Prettier v2) andPrettierWorkerInstance(Prettier v3+)Cancellation Support
CancellationTokenthrough formatting pipeline from VS Code's formatter APIPrettierEditServiceImplementation Details
Files Modified
package.json- Added timeout configuration settingpackage.nls.json- Added localization for timeout settingsrc/types.d.ts- Added timeout to configuration typessrc/utils/timeout.ts- New timeout utility with proper cleanupsrc/PrettierInstance.ts- Added setTimeoutMs method to interfacesrc/PrettierMainThreadInstance.ts- Applied timeout to format methodsrc/PrettierWorkerInstance.ts- Applied timeout to format methodsrc/ModuleResolver.ts- Set timeout from config when creating instancessrc/PrettierEditProvider.ts- Pass cancellation token through pipelinesrc/PrettierEditService.ts- Check cancellation at strategic pointssrc/test/suite/timeout.test.ts- New comprehensive timeout testsRelated Issue
Fixes #3217
Type of Change
Checklist
npm run lintpasses)npm run prettierto format my codenpm test)Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.