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

ngclient updater: clean temp file if write fails #1757

Merged

Conversation

MVrachev
Copy link
Collaborator

@MVrachev MVrachev commented Jan 5, 2022

Fixes #1575

Description of the changes being introduced by the pull request:

When calling updater._persist_metadata() there is a possibility that
writing the temporary file to storage can succeed, but moving it with
os.replace could fail with OSError.
Make sure we are removing the newly created temporary file in that case.

I tried testing the first case described in the issue, but I had troubles
mocking write as it was not os.write().

Signed-off-by: Martin Vrachev mvrachev@vmware.com

Please verify and check that the pull request fulfills the following
requirements
:

  • The code follows the Code Style Guidelines
  • Tests have been added for the bug fix or new feature
  • Docs have been added for the bug fix or new feature

@coveralls
Copy link

coveralls commented Jan 5, 2022

Pull Request Test Coverage Report for Build 1682855792

  • 11 of 13 (84.62%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.05%) to 97.688%

Changes Missing Coverage Covered Lines Changed/Added Lines %
tuf/ngclient/updater.py 11 13 84.62%
Totals Coverage Status
Change from base Build 1682108010: -0.05%
Covered Lines: 4097
Relevant Lines: 4178

💛 - Coveralls

@ivanayov
Copy link
Collaborator

ivanayov commented Jan 6, 2022

Looks good to me. Probably adding one more assert that the file doesn't exist?

tests/test_updater_ng.py Outdated Show resolved Hide resolved
@MVrachev MVrachev force-pushed the persistent-tmp-file-cleaning branch from 3e34b0e to 8008329 Compare January 6, 2022 16:32
@MVrachev
Copy link
Collaborator Author

MVrachev commented Jan 6, 2022

Looks good to me. Probably adding one more assert that the file doesn't exist?

Yep, maybe you are correct that this way the test will be more complete.
I updated the pr adding a check for that.

tests/test_updater_ng.py Outdated Show resolved Hide resolved
@MVrachev MVrachev force-pushed the persistent-tmp-file-cleaning branch 3 times, most recently from 95d5d9f to 3f4458f Compare January 10, 2022 17:24
@MVrachev
Copy link
Collaborator Author

Updated the pr with @jku suggestions.

@MVrachev MVrachev requested a review from jku January 10, 2022 17:25
@MVrachev
Copy link
Collaborator Author

The CI failure is fixed in #1762.
I will rebase after the merge of this pr and the failure should be fixed.

Copy link
Member

@jku jku left a comment

Choose a reason for hiding this comment

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

Thank you, looks good. tempfile-not-found-during-exception case is untested but I'm fine with that.

When calling updater._persist_metadata() there is a possibility that
writing the temporary file to storage can succeed, but moving it with
os.replace could fail with OSError.
Make sure we are removing the newly created temporary file in that case.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
@MVrachev
Copy link
Collaborator Author

Thank you, looks good. tempfile-not-found-during-exception case is untested but I'm fine with that.

I tried adding a new test patching tempfile.NamedEntities().write() but after I didn't succeed for a couple of hours I left it.
If in the future we needed I can invest more time in this because surely there should be a way to do it.

@jku jku merged commit 40c9737 into theupdateframework:develop Jan 11, 2022
@MVrachev MVrachev deleted the persistent-tmp-file-cleaning branch January 12, 2022 13:26
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.

ngclient: should clean temp file if write fails
4 participants