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

MJCF attach segfaults with replicate and several other factors #2543

Closed
2 tasks done
jjyyxx opened this issue Mar 28, 2025 · 4 comments
Closed
2 tasks done

MJCF attach segfaults with replicate and several other factors #2543

jjyyxx opened this issue Mar 28, 2025 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@jjyyxx
Copy link
Contributor

jjyyxx commented Mar 28, 2025

Intro

Hi!

I am a graduate student at HKU, I use MuJoCo for my research on robotic manipulation.

My setup

MuJoCo 3.3.0, Python and C, Linux

What's happening? What did you expect?

With an attach hierarchy of

parent -- child_1 -- child_2
       |               |
       |---------------|

and replicate, MjSpec segfaults with seemingly not-directly-related factor of using the same name of "!!" in two places in the code below. Note that actually !! does not exist in child_2.xml, and can be any string. A more legit use case is replacing !! with the existing body 2, but segfaults anyway.

Steps for reproduction

See code below.

Minimal model for reproduction

parent.xml

<mujoco>
  <asset>
    <model name="1" file="child_1.xml" content_type="text/xml" />
    <model name="2" file="child_2.xml" content_type="text/xml" />
  </asset>

  <worldbody>
    <attach model="1" body="1" prefix="prefix" />

    <replicate count="1">
      <attach model="2" body="!!" prefix="prefix" />
    </replicate>
  </worldbody>
</mujoco>

child_1.xml

<mujoco>
  <asset>
    <model name="2" file="child_2.xml" content_type="text/xml"/>
  </asset>

  <worldbody>
    <body name="1">
      <body name="!!">
        <attach model="2" body="2" prefix="prefix2"/>
      </body>
    </body>
  </worldbody>
</mujoco>

child_2.xml

<mujoco>
  <worldbody>
    <body name="2"/>
  </worldbody>
</mujoco>

Code required for reproduction

import mujoco
mujoco.MjSpec.from_file('parent.xml')

Confirmations

@jjyyxx jjyyxx added the bug Something isn't working label Mar 28, 2025
@quagla
Copy link
Contributor

quagla commented Mar 28, 2025

Hi, I think the issue is that you are using the same prefix twice, which leads to duplicated names. But we should add some better error control to prevent the segfault.

@jjyyxx
Copy link
Contributor Author

jjyyxx commented Mar 29, 2025

I'm not sure what names are duplicating in this case. I thought "replicate" adds suffix to the name?

@yuvaltassa
Copy link
Collaborator

Replicate adds the suffix but in your case the attach happens before the replicate. As @quagla says, the correct behavior should be either an error upon attach, or a duplicate name error upon compile. We will definitely try to reproduce what you are seeing, in the meantime, could you check if having different prefixes (in parent.xml) solves the problem?

@jjyyxx
Copy link
Contributor Author

jjyyxx commented Mar 30, 2025

Yes, of course, having different prefixes solves this problem immediately. But for a complex scene, we used to group objects into categories with this prefix. Thinking of and organizing many prefixes is a pain 😢

But restrict the scope to this issue, I'm not seeking any change in current attach behavior: it is enough to provide a more user friendly error instead of a segfault.

That said, two things still feel a bit unergonomic here:

  1. Replicate adds the suffix but in your case the attach happens before the replicate, but attaching a non-existent name may not pass checks in the first place. (However it segfaults in this case)
  2. Changing "!!" to "2", removing the replicate wrapper, such that there are truely duplicate body names, "fixes" the segfault, though the model is silently wrong.

@quagla quagla self-assigned this Mar 31, 2025
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

3 participants