Skip to content

Add robust error handling and Sentry tracking for backup extraction #1455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: trunk
Choose a base branch
from

Conversation

bcotrim
Copy link
Contributor

@bcotrim bcotrim commented May 29, 2025

Related issues

Proposed Changes

  • Added Sentry error tracking to both WPress and ZIP backup handlers
  • Improved error handling in file stream operations to prevent ERR_STREAM_DESTROYED errors
  • Added proper cleanup and error propagation in stream operations
  • Implemented fail-once pattern to prevent multiple error rejections
  • Added detailed error context for better debugging

Testing Instructions

Note: I was unable to reliably reproduce the original ERR_STREAM_DESTROYED error in testing, but the error handling improvements are defensive and should prevent the issue.

  • Test with large WordPress sites (3GB+) by initiating sync operations from WordPress.com to Studio
  • Test error scenarios: corrupt backup files, disk space exhaustion, permission errors, process interruption during sync
  • Monitor sync temp directories during operations:
    TEMP_DIR=$(node -e "console.log(require('os').tmpdir())")
    ls -la "$TEMP_DIR/wp-studio-backups/"
  • Verify that sync errors show meaningful messages in Studio UI and don't leave Studio hanging

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?

@bcotrim bcotrim requested a review from a team May 29, 2025 15:52
@bcotrim bcotrim self-assigned this May 29, 2025
@bcotrim bcotrim marked this pull request as ready for review May 29, 2025 21:58
}
totalBytesToRead -= data.bytesRead;
}
endStream();
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need endStream here? Given that we have the same in the finally part.

Comment on lines 129 to 133
if ( ! outputStream.destroyed ) {
outputStream.write( buffer );
} else {
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if ( ! outputStream.destroyed ) {
outputStream.write( buffer );
} else {
return;
}
if ( outputStream.destroyed ) {
return;
}
outputStream.write( buffer );

Nitpick: Doing an early return would improve code readability.

@@ -47,6 +48,17 @@ export class BackupHandlerZip extends EventEmitter implements BackupHandler {
this.emit( ImportEvents.BACKUP_EXTRACT_START );

return new Promise( ( resolve, reject ) => {
let extractionFailed = false;
function failOnce( err: Error, context?: Record< string, unknown > ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
function failOnce( err: Error, context?: Record< string, unknown > ) {
const failOnce = (err: Error, context?: Record<string, unknown>) => {

Can we use an inline function instead of an arrow function for failOnce?

const writeStream = fs.createWriteStream( fullPath );

const onError = ( err: Error ) => {
failOnce( err, { fullPath, entry: entry.fileName, filePath: file.path } );
Copy link
Contributor

@gavande1 gavande1 May 30, 2025

Choose a reason for hiding this comment

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

If an error occurs while reading or writing the stream, and the stream remains open, we may be left with lingering streams. I believe the code can benefit from manually closing the stream if it hasn't already been closed when the error occurred. What do you think about it?

if (!readStream.destroyed) {
	readStream.destroy();
}
if (!writeStream.destroyed) {
	writeStream.destroy();
}

@bcotrim bcotrim requested a review from gavande1 May 30, 2025 11:19
@bcotrim
Copy link
Contributor Author

bcotrim commented May 30, 2025

@gavande1 thanks for the review and comments!
I believe I have addressed all of them, can you take another look please?

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.

2 participants