cuda_buffer: late import of a reused VMM block passes the UID staleness check and reads the next frame

Open Beginner friendly
#40 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

  • #41 by @TillBeemelmanns — closed without merging

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
74/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp

Research direction

The issue identifies cuda_buffer/src/cuda_memory_pool.cpp, in CudaMemoryPool::allocate(). Read the block reuse path and the UID staleness check to understand how shared IPCMetadata is used. Done means invalidating the shared UID when a block is handed out again so a late import is rejected as stale instead of returning overwritten frame data.

Written by the indexing model from the issue text.

Description

Summary

A subscriber that takes a CUDA buffer message after the publisher has reused the message's VMM block passes the UID staleness check. It then imports the block and reads the next frame's data under the old message's header.

Cause

CudaMemoryPool::allocate() hands a free block out again once is_block_ready() holds: refcount 0 and the 100 ms grace period since the publish has passed. When it does, it resets only the publisher's local copy of the UID:

block->current_uid = 0;
if (block->ipc_meta) {
  block->ipc_meta->publish_timestamp_us.store(0, std::memory_order_release);
}
return block;

The UID in the shared IPCMetadata keeps the old value until the next assign_uid() at publish time. Meanwhile the publisher writes the next frame into the block.

A subscriber only counts itself in (refcount.fetch_add) when it imports, that is, when it takes the message. So a message still queued in a subscriber's DDS history for more than 100 ms isn't protected. Callbacks blocked behind a slow callback in the same mutually exclusive group are enough. When that subscriber finally imports, check_uid_staleness() compares the descriptor's UID with ipc_meta->uid, which is still the old one, so the check passes. The data, however, is already, or is being, overwritten.

The same code is on main (cuda_buffer/src/cuda_memory_pool.cpp, allocate()).

Fix (patch we apply on 0.1.2 + ffe42cb)

Invalidate the shared UID when the block is handed out again. A late import then fails the staleness check and is dropped (Stale block ... dropping, Dropping stale descriptor) instead of returning wrong data:

--- a/cuda_buffer_backend/cuda_buffer/src/cuda_memory_pool.cpp
+++ b/cuda_buffer_backend/cuda_buffer/src/cuda_memory_pool.cpp
@@ VmmBlock * CudaMemoryPool::allocate(size_t byte_size)
         block->current_uid = 0;
         if (block->ipc_meta) {
           block->ipc_meta->publish_timestamp_us.store(0, std::memory_order_release);
+          // Invalidate the published UID before the block is rewritten: a subscriber that takes an older
+          // descriptor of this block from now on must see it as stale (and drop it) instead of importing it
+          // and reading the next frame's data under the old message's header.
+          block->ipc_meta->uid.store(0, std::memory_order_release);
         }
         return block;
Observed

This is with camera images at 30 fps, published with CUDA buffers (ZED ROS 2 wrapper 5.6) to subscribers in other processes on the same GPU, under heavy GPU load. With the patch, the subscribers log a few Stale block ... dropping per session: 2–4 in about 10 minutes. Without it, each of those frames would have been delivered with another frame's data. Late subscribers may get an empty buffer instead, which consumers should handle.

A remaining limitation, not fixed by this patch: the 100 ms grace period decides whether a slow subscriber gets the frame at all. Counting matched subscribers in at publish time, rather than at import, would close the window entirely.

Dominant language
C++
Stars
27
Forks
13
Avg merge
21d 23h
Merged PRs (30d)
1

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ros2/rosidl_buffer_backends

All issues in ros2/rosidl_buffer_backends

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.