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

In astropy/utils/data.py, update _rmtree to use shutil.move instead of os.rename. #13730

Merged
merged 3 commits into from Apr 12, 2023

Conversation

tribeiro
Copy link
Contributor

@tribeiro tribeiro commented Sep 21, 2022

Using os.rename may raise an

OSError: [Errno 18] Invalid cross-device link

exception when running on a containairized environments deployed on kubernetes.

Description

This pull request is to address an error we get when using astropy in a containerized environment deployed using Kubernetes. For some reason using os.rename on these environments raises an OSError exception even when the file is in the same directory. Replacing os.rename with shuttle.move solves this issue.

Checklist for package maintainer(s)

This checklist is meant to remind the package maintainer(s) who will review this pull request of some common things to look for. This list is not exhaustive.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see "When to rebase and squash commits".
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the Extra CI label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the no-changelog-entry-needed label. If this is a manual backport, use the skip-changelog-checks label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • Is a milestone set? Milestone must be set but astropy-bot check might be missing; do not let the green checkmark fool you.
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate backport-X.Y.x label(s) before merge.

@github-actions github-actions bot added the utils label Sep 21, 2022
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Welcome to Astropy 👋 and congratulations on your first pull request! 🎉

A project member will respond to you as soon as possible; in the meantime, please have a look over the Checklist for Contributed Code and make sure you've addressed as many of the questions there as possible.

If you feel that this pull request has not been responded to in a timely manner, please leave a comment mentioning our software support engineer @embray, or send a message directly to the development mailing list. If the issue is urgent or sensitive in nature (e.g., a security vulnerability) please send an e-mail directly to the private e-mail feedback@astropy.org.

@pllim pllim added this to the v5.2 milestone Sep 28, 2022
@pllim pllim added the Extra CI Run cron CI as part of PR label Sep 28, 2022
@pllim pllim modified the milestones: v5.2, v5.0.5 Sep 28, 2022
@pllim pllim added Bug 💤 backport-v5.0.x on-merge: backport to v5.0.x 💤 backport-v5.1.x on-merge: backport to v5.1.x labels Sep 28, 2022
@pllim
Copy link
Member

pllim commented Sep 28, 2022

Alas, the failure looks related. Since @aarchiba wrote the original code and test here, maybe she can have a look as well.

@pllim
Copy link
Member

pllim commented Sep 28, 2022

If you decide to pursue this fix, please also add a change log. Thanks!

See https://github.com/astropy/astropy/blob/main/docs/changes/README.rst

@pllim
Copy link
Member

pllim commented Oct 20, 2022

Since there is failure that needs addressing, I am converting this to draft. FYI.

@pllim pllim marked this pull request as draft October 20, 2022 20:34
@pllim pllim modified the milestones: v5.0.5, v5.0.6 Oct 21, 2022
@saimn saimn removed the 💤 backport-v5.1.x on-merge: backport to v5.1.x label Oct 24, 2022
@github-actions
Copy link

Hi humans 👋 - this pull request hasn't had any new commits for approximately 4 months. I plan to close this in 30 days if the pull request doesn't have any new commits by then.

In lieu of a stalled pull request, please consider closing this and open an issue instead if a reminder is needed to revisit in the future. Maintainers may also choose to add keep-open label to keep this PR open but it is discouraged unless absolutely necessary.

If this PR still needs to be reviewed, as an author, you can rebase it to reset the clock.

If you believe I commented on this pull request incorrectly, please report this here.

@tribeiro tribeiro force-pushed the replace-os-rename-with-shutil-move branch 9 times, most recently from 6119b5c to b090c31 Compare March 22, 2023 22:23
@tribeiro tribeiro marked this pull request as ready for review March 23, 2023 00:11
@tribeiro
Copy link
Contributor Author

@pllim , I changed the implementation to keep the original os.rename strategy and fallback to shutil.move if if fails with the error I mentioned in the issue. I included tests for the changes so I am not sure why the code coverage is not catching it.

I also updated the changelog.

@tribeiro tribeiro force-pushed the replace-os-rename-with-shutil-move branch from b090c31 to ca38f95 Compare March 23, 2023 00:17
@@ -0,0 +1 @@
Update ``_rmtree`` to catch ``OSError`` and use ``shutil.move`` if errno == EXDEV.
Copy link
Member

Choose a reason for hiding this comment

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

Since _rmtree is "hidden", it should not be explicitly mentioned like this in a change log. But rather, what do you want the end-user to know about this patch? How is the data.py behavior changing for them at a high level?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changelog updated.

@pllim pllim removed Close? 💤 backport-v5.0.x on-merge: backport to v5.0.x labels Mar 23, 2023
@pllim pllim modified the milestones: v5.0.6, v5.3 Mar 23, 2023
@pllim
Copy link
Member

pllim commented Mar 23, 2023

@aarchiba , are you interested to review?

Thanks, everyone!

@pllim
Copy link
Member

pllim commented Mar 23, 2023

Re: coverage -- Codecov does say your patch is 100% covered (https://app.codecov.io/gh/astropy/astropy/pull/13730). However, it is complaining about "indirect changes" at the project level; I am not clear why either. Have you rebased against upstream/main lately?

@tribeiro
Copy link
Contributor Author

Re: coverage -- Codecov does say your patch is 100% covered (https://app.codecov.io/gh/astropy/astropy/pull/13730). However, it is complaining about "indirect changes" at the project level; I am not clear why either. Have you rebased against upstream/main lately?

I did rebased it..

@tribeiro tribeiro force-pushed the replace-os-rename-with-shutil-move branch 2 times, most recently from b2e5b7d to c902bfb Compare March 23, 2023 17:08
@pllim

This comment was marked as resolved.

Copy link
Member

@pllim pllim left a comment

Choose a reason for hiding this comment

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

I think this looks good now; just some minor comments. Thanks!

To get a clean passing CI, please rebase against upstream/main. I will approve once comments are addressed and CI all green.

with open(filename, "w") as f:
f.write(content)

with pytest.warns(AstropyWarning):
Copy link
Member

Choose a reason for hiding this comment

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

Should also use match= to check the warning message here.

monkeypatch.setattr(os, "rename", no_rename)

assert is_url_in_cache(u)
with pytest.warns(AstropyWarning):
Copy link
Member

Choose a reason for hiding this comment

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

Should also use match= to check the warning message here.

docs/changes/utils/13730.bugfix.rst Outdated Show resolved Hide resolved
docs/changes/utils/13730.bugfix.rst Outdated Show resolved Hide resolved
docs/changes/utils/13730.bugfix.rst Outdated Show resolved Hide resolved
if e.errno == errno.EXDEV:
warn(e.strerror, AstropyWarning)
shutil.move(path, os.path.join(d, "to-zap"))
else:
Copy link
Member

Choose a reason for hiding this comment

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

Now I see that the original code did not raise on OSError but perhaps it was an oversight. I do not see why it should be ignored.

…`shutil.move`` if errno == EXDEV.

Using `os.rename` may raise an "OSError: [Errno 18] Invalid cross-device link" exception when running o a containairized environments deployed on kubernetes.
…or when ``os.rename`` raises an OSError exception with ``errno = errno.EXDEV``.
@tribeiro tribeiro force-pushed the replace-os-rename-with-shutil-move branch from 2a4d864 to e5afb33 Compare April 3, 2023 16:08
@tribeiro
Copy link
Contributor Author

tribeiro commented Apr 3, 2023

Hi @pllim , I implemented all your suggestions and rebased to the branch you recommended. It seems like all the checks are passing now (though there's one still running at the time of this writing).

@tribeiro tribeiro requested a review from pllim April 6, 2023 18:08
@pllim
Copy link
Member

pllim commented Apr 6, 2023

Hi. Just want to let you know this is on my queue to look at. Thanks for your patience!

Copy link
Member

@pllim pllim left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@pllim pllim merged commit b483f46 into astropy:main Apr 12, 2023
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Extra CI Run cron CI as part of PR utils
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants