fix: close file handle in MjSim.get_xml() to fix ResourceWarning (#801)#806
Open
dashitongzhi wants to merge 1 commit into
Open
fix: close file handle in MjSim.get_xml() to fix ResourceWarning (#801)#806dashitongzhi wants to merge 1 commit into
dashitongzhi wants to merge 1 commit into
Conversation
Fixes ARISE-Initiative#801 The get_xml() method in MjSim was using open(filename).read() without closing the file handle, causing a ResourceWarning. Changed to use a context manager (with statement) to properly close the file after reading. Co-authored-by: Cao Hanzhe <dashitongzhi@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Fixes a ResourceWarning caused by an unclosed file handle when MjModel.get_xml() reads the temporary XML produced by MuJoCo.
Changes:
- Replaced
open(filename).read()with awith open(...)context manager inMjModel.get_xml()to ensure the file handle is properly closed.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -503,7 +503,8 @@ def get_xml(self): | |||
| with TemporaryDirectory() as td: | |||
| filename = os.path.join(td, "model.xml") | |||
| ret = mujoco.mj_saveLastXML(filename.encode(), self._model) | |||
Author
|
Friendly ping for review 🙏 This is a small, focused fix. CI is passing. Thank you! |
Author
|
Hi! 👋 This PR looks ready for review. Could a maintainer please take a look when they have a chance? Thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Fixes #801
The
get_xml()method inMjSim(inrobosuite/utils/binding_utils.py) usesopen(filename).read()without properly closing the file handle. This causes aResourceWarning: unclosed filewheneverget_xml()orget_state()is called.Fix
Changed the bare
open(filename).read()call to use a context manager (withstatement) so the file is properly closed after reading:This is a minimal, safe change that ensures proper resource cleanup without altering any functionality.
Testing
The fix is straightforward and follows standard Python best practices for file I/O. The
withstatement ensures the file handle is closed when the block exits, eliminating theResourceWarning.