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

mujoco.MjData(None) will hang forever #2426

Open
2 tasks done
vikashplus opened this issue Feb 16, 2025 · 1 comment
Open
2 tasks done

mujoco.MjData(None) will hang forever #2426

vikashplus opened this issue Feb 16, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@vikashplus
Copy link

Intro

Hi,
I'm an advanced user

My setup

MuJoCo, python, OSX

What's happening? What did you expect?

Trying to create mj_data using model=None should gracefully fail or throw an error, but hangs forever

Steps for reproduction

Run attached code

Minimal model for reproduction

No model needed

Code required for reproduction

import mujoco

print(
    "Trying to create mj_data using model=None. This should gracefully fail or throw an error"
)
mj_data = mujoco.MjData(None)
print("Created mj_data")

Confirmations

@vikashplus vikashplus added the bug Something isn't working label Feb 16, 2025
@zacharias1219
Copy link

Hey,
I've tested this out as well, and my kernel crashes every time when calling mujoco.MjData(None).

I think the issue occurs because the constructor doesn't properly handle a None model input, leading to an infinite loop or memory access violation in MuJoCo's backend. Instead of throwing a Python exception, it completely freezes or crashes the interpreter.

Steps I Took to Reproduce the Crash

I ran the following minimal test:

import mujoco

print("Trying to create MjData with None...")
mj_data = mujoco.MjData(None)
print("Created MjData")  # This never prints because the process crashes

Expected behavior: It should gracefully fail with an error message.
Observed behavior: The Python kernel crashes, and the session needs to be restarted.

Proposed Fix

To prevent this, MjData.__init__ should explicitly check for None and raise a ValueError.

class MjData:
    def __init__(self, model):
        if model is None:
            raise ValueError("MjData cannot be initialized with None. Provide a valid mjModel instance.")
        self.model = model  # Normal initialization continues here

This ensures that instead of a crash, users get a clear error message.

Would love to hear your thoughts on this! I can open a PR if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants