cuda_buffer: late import of a reused VMM block passes the UID staleness check and reads the next frame
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
- Domain
- backend-api-design
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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ros2/rosidl_buffer_backends
-
Difficulty 5/5 Over a week Newbie friendliness 12/100
-
cuda_buffer: please release #12 (0.1.2 segfaults in get_endpoint_manager after a context restart)Possibly taken @yuanknv claimed this 3 days ago. Open
Difficulty 4/5 1-2 days Newbie friendliness 15/100
ros2/rosidl_buffer_backends#42 · 1 reaction · 1 assignee ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
-
Subscription accepting the cuda backend receives no messages when the cuda_buffer_backend plugin is not available in its processMay be free again @nvcyc claimed this 28 days ago, and no pull request is open. Open
ros2/rosidl_buffer_backends#34 · 1 assignee ·
-
Add ONNX Runtime tensor conversionsMay be free again @yuanknv claimed this 90 days ago, and no pull request is open. Openenhancement
ros2/rosidl_buffer_backends#14 · 2 assignees ·
All issues in ros2/rosidl_buffer_backends
Similar issues
-
bug
Difficulty 1/5 1-3 hours Newbie friendliness 82/100
EchoTools/nevr-runtime#2912 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
Maintainers usually reply within 1 day
-
category:port-update
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
bug-unconfirmed
Difficulty 1/5 Under an hour Newbie friendliness 75/100
Maintainers usually reply within 1 day
-
libc++
Difficulty 1/5 Under an hour Newbie friendliness 88/100
llvm/llvm-project#246508 ·
Maintainers usually reply within 1 day