mujoco icon indicating copy to clipboard operation
mujoco copied to clipboard

mujoco.MjData(None) will hang forever

Open vikashplus opened this issue 1 year ago • 1 comments

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 avatar Feb 16 '25 02:02 vikashplus

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.

zacharias1219 avatar Mar 08 '25 12:03 zacharias1219