RobotecAI / ros2cs

A C# (.Net) implementation of ros2 client library (rcl), enabling communication between ros2 ecosystem and C#/.Net applications such as Unity3D
Apache License 2.0
91 stars 22 forks source link

Get "Unhandled exception. ROS2.RuntimeError: publisher's context is invalid, at ./src/rcl/publisher.c:389" when I run example more than 20+ minutes. #49

Open Nickiven opened 1 year ago

Nickiven commented 1 year ago

OS: Ubuntu22.04 ROS2 Humble .NET 6.0

Hi, I have modified example "ROSTalker" and "ROSListener" files to implement these two nodes can send and receive data at the same time. Details as below: image After build and launch, everything goes well. But 20mintues later, I get an exception as below: image Please Help. Thanks.

Deric-W commented 1 year ago

Can reproduce this issue on Ubuntu with Ros2 Humble and the talker example, removing the Sleep call speeds the whole process up.

I think it is somehow caused by the garbage collector since removing all calls to Shutdown does not fix this issue while calling GC.TryStartNoGCRegion (or GC.GetGCMemoryInfo, I dont know why) seem to prevent it.

Furthermore, the issue does not occur using the changes in #47, which allocate the structs in native memory.

Nickiven commented 1 year ago

Can reproduce this issue on Ubuntu with Ros2 Humble and the talker example, removing the Sleep call speeds the whole process up.

I think it is somehow caused by the garbage collector since removing all calls to Shutdown does not fix this issue while calling GC.TryStartNoGCRegion (or GC.GetGCMemoryInfo, I dont know why) seem to prevent it.

Furthermore, the issue does not occur using the changes in #47, which allocate the structs in native memory.

Hi Deric,

According to your suggestion, I revert my changes on listener and talker example. After launching program, it's running smoothly for over 5 hours. It seems that my changes caused the issue, and I don't know why... Thanks for your responding!

Nick

Deric-W commented 1 year ago

Hi, I was able to reproduce the error with the normal talker example included in this repository, which means the error is not your fault and still present but it seems it is triggered erratically. I try to find out what exactly is causing it.

Deric-W commented 1 year ago

While I am not 100% certain I think it is because of the garbage collector moving the rcl_context_t struct since almost always the error occurs when this happens (on my machine). The invalid context is not picked up by Ros2cs.Ok() which is probably because after the move the publisher struct contains the old address now pointing to somewhere in memory (for which rcl_context_is_valid is undefined) while the call to rcl_context_is_valid in Ros2cs.Ok() receives the updated one of the still valid context. Also, it is possible to call Ros2cs.Shutdown() after such a move and keep publishing by removing the shutdown checks in Publisher.Publish and hoping that the old memory location has not been touched since.

adamdbrw commented 1 year ago

@Nickiven was this issue resolved for you?