Skip to content
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

fix(utils): add error handling on file close and syncing data to storage #443

Conversation

VibhorGits
Copy link
Contributor

Because

Handling Error while closing dst file and syncing the data to storage medium preventing data loss.

@codecov
Copy link

codecov bot commented Oct 28, 2023

Codecov Report

Attention: 17 lines in your changes are missing coverage. Please review.

Comparison is base (c6f255f) 1.04% compared to head (921032b) 1.03%.

❗ Current head 921032b differs from pull request most recent head 80ee8c3. Consider uploading reports for the commit 80ee8c3 to get more accurate results

Additional details and impacted files
@@           Coverage Diff            @@
##            main    #443      +/-   ##
========================================
- Coverage   1.04%   1.03%   -0.01%     
========================================
  Files         10      10              
  Lines       5373    5386      +13     
========================================
  Hits          56      56              
- Misses      5309    5322      +13     
  Partials       8       8              
Flag Coverage Δ
unittests 1.03% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
pkg/utils/file.go 0.00% <0.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

pkg/utils/file.go Fixed Show resolved Hide resolved
@VibhorGits VibhorGits changed the title Fix: Error Handling on file close and syncing data to storage medium fix: Error Handling on file close and syncing data to storage medium Oct 28, 2023
@heiruwu heiruwu changed the title fix: Error Handling on file close and syncing data to storage medium fix(utils): add error handling on file close and syncing data to storage Oct 31, 2023
@@ -277,10 +285,22 @@ func UpdateModelPath(modelDir string, dstDir string, owner string, model *datamo
return "", "", err
}
if _, err := io.Copy(dstFile, srcFile); err != nil {
srcFile.Close() // Close the source file in case of an error
dstFile.Close() // Close the destination file in case of an error
Copy link
Member

Choose a reason for hiding this comment

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

Hi @VibhorGits, these .Close() calls need to be handled as well.
I advise you to use defer function to avoid excessive error handling blocks, like this

defer func() {
	// try to close the file; if an error occurs, log the error
	if err := srcFile.Close(); err != nil {
		log.Printf("%v", err.Error())
	}
}()

@VibhorGits
Copy link
Contributor Author

Hey @heiruwu @tonywang10101 I made some changes in my pr adding defer function as per my understanding.
Have a look : 729f435

@@ -157,7 +157,15 @@ func Unzip(fPath string, dstDir string, owner string, uploadedModel *datamodel.M
return "", "", err
}

dstFile.Close()
// Close the file handle and sync the data to the storage medium
if err := dstFile.Close(); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Using defer instead of call it directly

// Defer the closing and syncing of dstFile
defer func() {
if err := dstFile.Close(); err != nil {
log.Printf("%v", err.Error())
Copy link
Contributor

Choose a reason for hiding this comment

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

log would be undefined here, suggestion:

defer func(logger *log.Logger) {
    if err := dstFile.Close(); err != nil {
        logger.Printf("%v", err.Error())
    }
    if err := dstFile.Sync(); err != nil {
        logger.Printf("%v", err.Error())
    }
}(log.Default()) // Pass the log object to the deferred function

@VibhorGits
Copy link
Contributor Author

Hey @tonywang10101 I made some changes and used logger as per your suggestion but there's something more work to be done considering the integration testing failing which I am not able to understand. 😕

@heiruwu heiruwu closed this Jan 4, 2024
@VibhorGits VibhorGits deleted the fix/Error-handling-for-file-closing branch January 6, 2024 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants