Skip to content

Fix adaptor lifetime issues by storing any_resource - #2208

Closed
bdice wants to merge 5 commits into
rapidsai:mainfrom
bdice:adaptors-any-resource
Closed

bdice wants to merge 5 commits into
rapidsai:mainfrom
bdice:adaptors-any-resource

Conversation

@bdice

@bdice bdice commented Jan 13, 2026

Copy link
Copy Markdown
Collaborator

Fixes #2207

Summary

Changes all RMM memory resource adaptors to store cuda::mr::any_resource instead of device_async_resource_ref, ensuring adaptors maintain ownership of their upstream resources through reification.

Changes

  • All 12 adaptors now store cuda::mr::any_resource<cuda::mr::device_accessible> (owning) instead of device_async_resource_ref (non-owning)
  • Constructors reify resource references to create owning copies
  • Members marked mutable to allow conversion to resource_ref in const methods
  • Added #include <cuda/memory_resource> to all adaptor headers

Key Benefit

Users only need to manage the adaptor's lifetime, not the upstream resource's lifetime. This eliminates a common source of undefined behavior when using adaptors with temporary resources.

Testing

  • Added new tests for construction from any_resource and upstream resource lifetime

Change all RMM memory resource adaptors to store cuda::mr::any_resource
instead of device_async_resource_ref. This ensures adaptors maintain
ownership of their upstream resources through "reification" - converting
resource references to owning copies.

Problem:
Adaptors previously stored non-owning references (device_async_resource_ref)
to upstream resources, leading to undefined behavior when the upstream
resource was destroyed before the adaptor. This was particularly dangerous
when setting global device resources with local upstream resources.

Solution:
- Store cuda::mr::any_resource<cuda::mr::device_accessible> (owning)
  instead of device_async_resource_ref (non-owning)
- Reify resource references in constructors to create owning copies
- Mark members mutable to allow conversion to resource_ref in const methods
- Add #include <cuda/memory_resource> to all adaptor headers

Updated adaptors:
- statistics_resource_adaptor
- tracking_resource_adaptor
- logging_resource_adaptor
- failure_callback_resource_adaptor
- limiting_resource_adaptor
- prefetch_resource_adaptor
- aligned_resource_adaptor
- thread_safe_resource_adaptor
- pool_memory_resource
- arena_memory_resource
- binning_memory_resource
- fixed_size_memory_resource

Benefits:
- Safe by default: adaptors automatically own their upstream resources
- Zero API breakage: existing code continues to work unchanged
- Simpler lifetime management: users only manage adaptor lifetime
- Minimal overhead: adds ~2-3 pointers per adaptor instance

Tests:
- Added comprehensive test suite (cccl_mr_adaptors_tests.cpp)
- All 87 existing RMM C++ tests pass
- Includes test validating the UB issue is resolved
@copy-pr-bot

copy-pr-bot Bot commented Jan 13, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@bdice bdice moved this to In Progress in RMM Project Board Jan 13, 2026
@bdice bdice self-assigned this Jan 13, 2026
@bdice bdice added feature request New feature or request non-breaking Non-breaking change labels Jan 13, 2026
Apply the same pattern used in memory resource adaptors - mark the
any_resource member as mutable to allow conversion to resource_ref in
const methods.

This allows memory_resource() to remain const, maintaining const-
correctness while still enabling the required conversion from
any_resource to device_async_resource_ref.

Changes:
- Mark device_buffer::_mr as mutable
- Restore const qualifier on device_buffer::memory_resource()
- Restore const qualifier on device_uvector::memory_resource()
@bdice
bdice marked this pull request as ready for review January 13, 2026 05:29
@bdice
bdice requested a review from a team as a code owner January 13, 2026 05:29
@bdice
bdice requested review from harrism and wence- January 13, 2026 05:29
Apply the same pattern used in memory resource adaptors - mark the
any_resource member as mutable to allow conversion to resource_ref in
const methods.

This allows memory_resource() to remain const, maintaining const-
correctness while still enabling the required conversion from
any_resource to device_async_resource_ref.

Changes:
- Mark device_buffer::_mr as mutable
- Restore const qualifier on device_buffer::memory_resource()
- Restore const qualifier on device_uvector::memory_resource()
@bdice
bdice force-pushed the adaptors-any-resource branch from 47d7bf4 to c8ee9ee Compare January 13, 2026 05:41
@wence- wence- added breaking Breaking change and removed non-breaking Non-breaking change labels Jan 13, 2026
@wence-

wence- commented Jan 13, 2026

Copy link
Copy Markdown
Contributor

I changed this to be a breaking change, because it is massively breaking.

Consider the (common) case where the "upstream" resource passed into the adaptor is stateful.

Previously, we passed a resource_ref (non-owning, definitely copyable).

Now we pass a resource_ref that is upgraded to an any_resource. I think this requires that all upstreams that we pass in must now be copyable. But many won't be.

The same goes for construction from a raw pointer.

@wence- wence- left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As noted, I think we need to think through how callers will now provide resources to adaptors.

std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT,
std::size_t alignment_threshold = default_alignment_threshold)
: upstream_{upstream},
: upstream_{cuda::mr::any_resource<cuda::mr::device_accessible>{upstream}},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throughout:

Suggested change
: upstream_{cuda::mr::any_resource<cuda::mr::device_accessible>{upstream}},
: upstream_{upstream},

An any_resource is constructable from a resource_ref (as the PR shows by constructing one). No need to construct twice.

Comment on lines +92 to +93
: upstream_{cuda::mr::any_resource<cuda::mr::device_accessible>{
to_device_async_resource_ref_checked(upstream)}},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
: upstream_{cuda::mr::any_resource<cuda::mr::device_accessible>{
to_device_async_resource_ref_checked(upstream)}},
: upstream_{to_device_async_resource_ref_checked(upstream)},

Comment on lines -110 to +113
return upstream_;
return device_async_resource_ref{upstream_};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A cuda::mr::any_resource is assignable to a cuda::mr::resource_ref is that not the case here? That is, why does return upstream_; not convert?

Comment on lines +165 to +202
TYPED_TEST(AdaptorTest, ConstructFromAnyResource)
{
if constexpr (std::is_same_v<TypeParam, owning_wrapper>) {
GTEST_SKIP() << "owning_wrapper has different construction pattern";
} else {
cuda::mr::any_resource<cuda::mr::device_accessible> cuda_mr = rmm::mr::cuda_memory_resource();
rmm::device_async_resource_ref any_mr{cuda_mr};

auto mr = this->make_adaptor(any_mr);

void* ptr = mr->allocate_sync(1024);
ASSERT_NE(ptr, nullptr);
EXPECT_NO_THROW(mr->deallocate_sync(ptr, 1024));
}
}

TYPED_TEST(AdaptorTest, UpstreamResourceLifetime)
{
if constexpr (std::is_same_v<TypeParam, owning_wrapper>) {
GTEST_SKIP() << "owning_wrapper has different construction pattern";
} else {
std::shared_ptr<TypeParam> mr;

{
cuda::mr::any_resource<cuda::mr::device_accessible> cuda_mr = rmm::mr::cuda_memory_resource();
rmm::device_async_resource_ref any_mr{cuda_mr};

mr = this->make_adaptor(any_mr);

// cuda_mr goes out of scope - adaptor owns a copy via reification
}

// Adaptor continues to work after upstream owner is destroyed
void* ptr = mr->allocate_sync(1024);
EXPECT_NE(ptr, nullptr);
EXPECT_NO_THROW(mr->deallocate_sync(ptr, 1024));
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let us please add a test that demonstrates this works with a stateful adaptor.

@github-project-automation github-project-automation Bot moved this from In Progress to Review in RMM Project Board Jan 13, 2026
@bdice bdice mentioned this pull request Jan 13, 2026
51 tasks done
@bdice
bdice marked this pull request as draft February 9, 2026 21:08
@bdice bdice added DO NOT MERGE Hold off on merging; see PR for details and removed DO NOT MERGE Hold off on merging; see PR for details labels Feb 9, 2026
@bdice

bdice commented Feb 9, 2026

Copy link
Copy Markdown
Collaborator Author

#2246 explores a better design for this. I'll need to merge that and do similar changes for all the other stateful adaptors/resources. I'll close this PR.

@bdice bdice closed this Feb 9, 2026
@github-project-automation github-project-automation Bot moved this from Review to Done in RMM Project Board Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking Breaking change feature request New feature or request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Use any_resource in RMM adaptors to fix lifetime issue

2 participants