Skip to content

fix: close file handle in MjSim.get_xml() to fix ResourceWarning (#801)#806

Open
dashitongzhi wants to merge 1 commit into
ARISE-Initiative:masterfrom
dashitongzhi:fix/unclosed-file-handle-get-xml
Open

fix: close file handle in MjSim.get_xml() to fix ResourceWarning (#801)#806
dashitongzhi wants to merge 1 commit into
ARISE-Initiative:masterfrom
dashitongzhi:fix/unclosed-file-handle-get-xml

Conversation

@dashitongzhi
Copy link
Copy Markdown

Description

Fixes #801

The get_xml() method in MjSim (in robosuite/utils/binding_utils.py) uses open(filename).read() without properly closing the file handle. This causes a ResourceWarning: unclosed file whenever get_xml() or get_state() is called.

Fix

Changed the bare open(filename).read() call to use a context manager (with statement) so the file is properly closed after reading:

# Before
return open(filename).read()

# After
with open(filename) as f:
    return f.read()

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 with statement ensures the file handle is closed when the block exits, eliminating the ResourceWarning.

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>
Copilot AI review requested due to automatic review settings May 8, 2026 15:37
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 a with open(...) context manager in MjModel.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)
@dashitongzhi
Copy link
Copy Markdown
Author

Friendly ping for review 🙏 This is a small, focused fix. CI is passing. Thank you!

@dashitongzhi
Copy link
Copy Markdown
Author

Hi! 👋 This PR looks ready for review. Could a maintainer please take a look when they have a chance? Thank you!

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.

get_xml() -> ResourceWarning: Enable tracemalloc to get the object allocation traceback

2 participants