ROS2 Humble、ROS2 Iron間のDockerコンテナ間通信で失敗する

# 現象

- `docker run`で起動したROS2 HumbleのDockerコンテナでPublishしたTopicをROS2 IronのDockerコンテナで見るとROS2 Humble側が`std::bad_alloc`を出して落ちる

https://x.com/porizou1/status/1752656734811009257?s=20

# 環境　
公式のDocker Image

https://hub.docker.com/_/ros/

ROS2 Humble : osrf/ros:humble-desktop
ROS2 Iron : osrf/ros:iron-desktop


# --net=host --ipc=hostを付けて実行した場合


HumbleとIronのDocker containerを起動

```bash
 docker run -it --rm --net=host --ipc=host osrf/ros:humble-desktop bash
```


```bash
 docker run -it --rm --net=host --ipc=host osrf/ros:iron-desktop bash
```


Humble側でtalkerを実行

```bash
 ros2 run demo_nodes_cpp talker
```

Iron側で`ros2 topic echo /chatter`や`ros2 topic list`を実行すると以下のメッセージを出してNodeが終了する。


```bash
[INFO] [1707644091.355929196] [talker]: Publishing: 'Hello World: 17'
[INFO] [1707644092.355686072] [talker]: Publishing: 'Hello World: 18'
[INFO] [1707644093.355800370] [talker]: Publishing: 'Hello World: 19'
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
[ros2run]: Aborted
```


# --net=host --ipc=hostを付けないで実行した場合


--net=hostを付けないでデフォルトの`docker0` bridgeネットワーク上で通信する。

```bash
 docker run -it --rm  osrf/ros:humble-desktop bash
```


```bash
 docker run -it --rm  osrf/ros:iron-desktop bash
```

同様にIron側で`ros2 topic echo /chatter`や`ros2 topic list`を実行するとNodeが終了する。

```bash
[INFO] [1707645792.494870332] [talker]: Publishing: 'Hello World: 2'
[INFO] [1707645793.494768898] [talker]: Publishing: 'Hello World: 3'
[INFO] [1707645794.494608487] [talker]: Publishing: 'Hello World: 4'
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
[ros2run]: Aborted
```


# DDSをEclipse Cyclone DDSに変更する

DDSをデフォルトのFast DDSからEclipse Cyclone DDSに変更する。
Cyclone DDS インストール済みのDockerfileを作成

```dockerfile
ARG ROS_DISTRO=iron
FROM osrf/ros:${ROS_DISTRO}-desktop

RUN apt-get update && apt-get install -y \
    ros-${ROS_DISTRO}-rmw-cyclonedds-cpp \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

ENV RMW_IMPLEMENTATION=rmw_cyclonedds_cpp
```

Humble用のImage 作成

```bash
docker build --build-arg ROS_DISTRO=humble -t osrf/ros:humble-desktop-cyclonedds .
```


Iron用のImage 作成

```bash
docker build --build-arg ROS_DISTRO=iron -t osrf/ros:iron-desktop-cyclonedds .
```

同様に実行

Humble 

```bash
docker run -it --rm --net=host --ipc=host osrf/ros:humble-desktop-cyclonedds bash
```

Iron 

```bash
docker run -it --rm --net=host --ipc=host osrf/ros:iron-desktop-cyclonedds bash
```

Humble側で`ros2 run demo_nodes_cpp talker`実行したあと、
Iron側で`ros2 topic echo /chatter`や`ros2 topic list`を実行するとHumble側とIron側でそれぞれ以下のメッセージがでる

```bash
>>> [rcutils|error_handling.c:108] rcutils_set_error_state()
This error state is being overwritten:

  'invalid data size, at ./src/serdata.cpp:384'

with this new error message:

  'string data is not null-terminated, at ./src/serdata.cpp:384'

rcutils_reset_error() should be called after error handling to avoid this.
<<<
```

```bash
# ros2 topic list
[WARN] [1707647132.838054054] [rmw_cyclonedds_cpp]: Failed to parse type hash for topic 'ros_discovery_info' with type 'rmw_dds_common::msg::dds_::ParticipantEntitiesInfo_' from USER_DATA '(null)'.
[WARN] [1707647132.838084382] [rmw_cyclonedds_cpp]: Failed to parse type hash for topic 'rt/rosout' with type 'rcl_interfaces::msg::dds_::Log_' from USER_DATA '(null)'.
[WARN] [1707647132.838100818] [rmw_cyclonedds_cpp]: Failed to parse type hash for topic 'rt/parameter_events' with type 'rcl_interfaces::msg::dds_::ParameterEvent_' from USER_DATA '(null)'.
[WARN] [1707647132.838104348] [rmw_cyclonedds_cpp]: Failed to parse type hash for topic 'rt/chatter' with type 'std_msgs::msg::dds_::String_' from USER_DATA '(null)'.
[WARN] [1707647132.838108761] [rmw_cyclonedds_cpp]: Failed to parse type hash for topic 'ros_discovery_info' with type 'rmw_dds_common::msg::dds_::ParticipantEntitiesInfo_' from USER_DATA '(null)'.
[WARN] [1707647132.838121432] [rmw_cyclonedds_cpp]: Failed to parse type hash for topic 'rt/parameter_events' with type 'rcl_interfaces::msg::dds_::ParameterEvent_' from USER_DATA '(null)'.
/chatter
/parameter_events
/rosout

```



コマンド実行・終了のタイミングで謎のメッセージが出るがIron側で`ros2 run demo_nodes_cpp listener`実行してもNodeが落ちることはないので問題なく通信できてそう


# 参考


https://tech.aptpod.co.jp/entry/2023/09/25/170000



https://qiita.com/tomoyafujita/items/07937a25bc48aa076056

https://answers.ros.org/question/370595/ros2-foxy-nodes-cant-communicate-through-docker-container-border/