Skip to content

test(cuda.core): drain imported pool frees in IPC tests and share a thread runner - #3544

Merged
juenglin merged 6 commits into
NVIDIA:mainfrom
juenglin:ipc-tests-drain-imported-frees
Nov 24, 2026
Merged

juenglin merged 6 commits into
NVIDIA:mainfrom
juenglin:ipc-tests-drain-imported-frees

Conversation

@juenglin

Copy link
Copy Markdown
Contributor

Description

Makes the cuda.core IPC tests honor the free ordering of pool imports and fail cleanly when worker threads fail. Test-only.

Closing a buffer imported from a memory pool enqueues a stream-ordered cuMemFreeAsync, and the importer's free must complete before the exporter frees the allocation. Several IPC test children broke that order: some synchronized before closing, one drained the wrong device, one never closed its buffer, and many drained only on success. A new drain_on_exit(stack, synchronizable) helper in helpers/memory.py registers the sync on an ExitStack so it runs after the imported buffers close, also when an assertion fails. VMM imports are unaffected, since closing a VMM buffer synchronizes before it unmaps.

A new run_in_threads helper in helpers/misc.py replaces the hand-written thread start/join loops (follow-up from #3531). It re-raises the first worker failure, aborts a shared barrier so other workers do not hang, and with a timeout fails on a thread that does not finish. This fixes test_ipc_concurrent_import, which swallowed worker exceptions, test_ipc_import_release_race, which could hang and blame the race under test when a worker failed before its barrier, and two test_program_cache tests whose thread assertions were swallowed.

_enter_import from test_ipc_pool_duplicate_import moves to helpers/memory.py as enter_imported_pool, since test_ipc_pool_kind now uses it too.

Checklist

  • I have read the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

Several tests started and joined worker threads by hand, each with its own
error handling, and some got it wrong: test_ipc_concurrent_import never
collected worker exceptions, so a failing import let the test pass, and
test_ipc_import_release_race could hang if a worker failed before reaching
its barrier, which the parent then reported as the very race under test.

Add run_in_threads to helpers/misc.py. It runs each callable on its own
thread, joins every started thread, and re-raises the first failure (not a
BrokenBarrierError). A failing thread, or a thread that fails to start,
aborts the shared barrier so the other workers do not block forever. With a
timeout, a thread still running after its join raises TimeoutError.

Use it in test_ipc_pool_duplicate_import (replacing _run_threads),
test_ipc_concurrent_import, test_ipc_import_release_race,
test_resource_live_count, and the two threaded tests in
test_program_cache, whose thread_b assertions were previously swallowed.
test_memory's concurrent VMM grow test keeps its own loop: on a timeout it
deliberately leaves buffers open that a stuck thread may still use.

Where a worker failure now raises, the drain that used to follow the join
moves into a finally block, and the release race closes each imported
buffer with a with block. Unit tests cover the helper, and tests/AGENTS.md
points to it.

Signed-off-by: Ralf Juengling <rjuengling@nvidia.com>
Closing a buffer imported from a memory pool enqueues a stream-ordered
cuMemFreeAsync, and the importer's free must complete before the exporter
frees the allocation. Several IPC test children broke that order:

- test_ipc_pool_kind synchronized before closing, so the free stayed
  pending at exit.
- TestIpcImportedPinnedBufferOnSecondDevice synchronized device 1, but
  unpickling had bound the buffer's free to device 0's default stream.
- TestImportOversizedBufferDescriptorSize's expected rejection enqueues
  the free of what it mapped, and the harness never drained it.
- test_event_ipc never closed its imported buffer, and test_device_uuid
  returned without a drain.
- Many children drained only on success, so a failed assertion between
  the import and the close skipped both.

Add drain_on_exit(stack, synchronizable) to helpers/memory.py. Registered
on an ExitStack before the imported buffers, it synchronizes after they
close, also when the body raises. Also move _enter_import from
test_ipc_pool_duplicate_import to helpers/memory.py as enter_imported_pool,
now that test_ipc_pool_kind uses it too. The children enter their imported
buffers and pools on a stack with drain_on_exit;
TestBufferPeerAccessAfterImport, which switches devices, repeats its
close-and-drain tail in a finally block instead.

VMM imports are unaffected: closing a VMM buffer synchronizes before it
unmaps.

Signed-off-by: Ralf Juengling <rjuengling@nvidia.com>
@juenglin juenglin added this to the cuda.core 1.4.0 milestone Nov 24, 2026
@juenglin juenglin added test Improvements or additions to tests cuda.core Everything related to the cuda.core module labels Nov 24, 2026
@copy-pr-bot

copy-pr-bot Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@juenglin juenglin self-assigned this Nov 24, 2026
…maining tests

The worker pool IPC tests now drain the import's free on failure too, and two more threaded tests run through run_in_threads.
@juenglin
juenglin marked this pull request as ready for review November 24, 2026 04:02
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Tests
    • Improved reliability checks for concurrent CUDA memory, IPC, compilation, and caching scenarios.
    • Expanded coverage for worker failures, timeouts, and cleanup when operations fail, including ensuring waiting workers are released.
    • Updated resource-lifetime checks across memory and IPC test scenarios, with cleanup handled consistently when work succeeds or encounters an error.
    • Added checks that failures in concurrent operations are reported after other workers finish.

The changes add shared helpers for threaded test execution and imported-memory cleanup. They update concurrency and CUDA IPC tests to use these helpers or to close and synchronize resources through scoped cleanup.

Changes: Test concurrency and IPC cleanup

Layer / File(s) Summary
Shared test helpers
cuda_core/tests/helpers/misc.py, cuda_core/tests/helpers/memory.py, cuda_core/tests/test_helpers.py, cuda_core/tests/AGENTS.md
Adds run_in_threads for worker execution and failure handling. Adds helpers to register stream synchronization and imported-pool cleanup with an ExitStack. Documents thread-runner guidance and tests failure, barrier, and timeout behavior.
Concurrent test execution
cuda_core/tests/memory/test_resource_live_count.py, cuda_core/tests/test_memory.py, cuda_core/tests/test_program.py, cuda_core/tests/test_program_cache.py, cuda_core/tests/memory_ipc/test_ipc_concurrent_import.py, cuda_core/tests/memory_ipc/test_ipc_import_release_race.py, cuda_core/tests/memory_ipc/test_ipc_pool_duplicate_import.py
Replaces manual thread lifecycle and local exception collection with run_in_threads in concurrent tests. Retains existing barriers and adds timeout handling where described.
IPC resource lifetime management
cuda_core/tests/memory_ipc/test_device_uuid.py, cuda_core/tests/memory_ipc/test_errors.py, cuda_core/tests/memory_ipc/test_event_ipc.py, cuda_core/tests/memory_ipc/test_ipc_duplicate_import.py, cuda_core/tests/memory_ipc/test_ipc_pool_duplicate_import.py, cuda_core/tests/memory_ipc/test_ipc_pool_kind.py, cuda_core/tests/memory_ipc/test_memory_ipc.py, cuda_core/tests/memory_ipc/test_peer_access.py, cuda_core/tests/memory_ipc/test_send_buffers.py, cuda_core/tests/memory_ipc/test_serialize.py, cuda_core/tests/memory_ipc/test_workerpool.py
Adds scoped buffer and imported-pool cleanup, stream draining, and explicit synchronization in IPC child processes. Existing checks and buffer operations remain in place.

Suggested reviewers: mdboom, jpascucci-nv

Change: Other

Merge Risk: Minimal · up to 156a5

This is a test-only change that adds shared thread-runner and IPC cleanup helpers. It does not change production behavior. No actionable merge-blocking risk is identified.

Security architecture risk: Minimal · up to 156a5

The changes help tests report worker failures and complete imported-buffer frees in the correct order. They do not change the shipped API, resource ownership, or deployment behavior.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 77bfb2c and 156a572.

Files selected for processing: 1

  • cuda_core/tests/helpers/misc.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 9 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between e195f81 and 08ab4c4.

Files selected for processing: 21

  • cuda_core/tests/AGENTS.md
  • cuda_core/tests/helpers/memory.py
  • cuda_core/tests/helpers/misc.py
  • cuda_core/tests/memory/test_resource_live_count.py
  • cuda_core/tests/memory_ipc/test_device_uuid.py
  • cuda_core/tests/memory_ipc/test_errors.py
  • cuda_core/tests/memory_ipc/test_event_ipc.py
  • cuda_core/tests/memory_ipc/test_ipc_concurrent_import.py
  • cuda_core/tests/memory_ipc/test_ipc_duplicate_import.py
  • cuda_core/tests/memory_ipc/test_ipc_import_release_race.py
  • cuda_core/tests/memory_ipc/test_ipc_pool_duplicate_import.py
  • cuda_core/tests/memory_ipc/test_ipc_pool_kind.py
  • cuda_core/tests/memory_ipc/test_memory_ipc.py
  • cuda_core/tests/memory_ipc/test_peer_access.py
  • cuda_core/tests/memory_ipc/test_send_buffers.py
  • cuda_core/tests/memory_ipc/test_serialize.py
  • cuda_core/tests/memory_ipc/test_workerpool.py
  • cuda_core/tests/test_helpers.py
  • cuda_core/tests/test_memory.py
  • cuda_core/tests/test_program.py
  • cuda_core/tests/test_program_cache.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 9 remain after this review.

Comment thread cuda_core/tests/helpers/misc.py Resolved
@juenglin

Copy link
Copy Markdown
Contributor Author

/ok to test 08ab4c4

A worker still waiting at the barrier after a timeout would block until interpreter shutdown, because the helper's threads are non-daemon. Abort the barrier before raising TimeoutError so such a worker raises BrokenBarrierError and exits without running the code after its wait.
@juenglin

Copy link
Copy Markdown
Contributor Author

/ok to test 3c63d1d

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor
Doc Preview CI
Preview removed because the pull request was closed or merged.

Comment thread cuda_core/tests/helpers/misc.py Resolved
…e in run_in_threads

When one worker failed and another outlived the join timeout, the helper raised only the failure. Raise TimeoutError first, chained to the failure, so callers still learn that a thread may be using their resources.
Comment thread cuda_core/tests/helpers/misc.py Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 3c63d1d and 77bfb2c.

Files selected for processing: 2

  • cuda_core/tests/helpers/misc.py
  • cuda_core/tests/test_helpers.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 10 remain after this review.

Comment thread cuda_core/tests/helpers/misc.py Resolved
…_in_threads

A worker that failed right after a timed-out join could be missed: the failure was read while the error list was still empty, and the worker was no longer alive by the time liveness was checked, so the helper returned normally. Check liveness first.
@juenglin

Copy link
Copy Markdown
Contributor Author

/ok to test 156a572

@jpascucci-nv jpascucci-nv 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.

lgtm

@juenglin
juenglin enabled auto-merge (squash) November 24, 2026 17:41
@juenglin
juenglin merged commit afcdf40 into NVIDIA:main Nov 24, 2026
121 checks passed
juenglin added a commit to juenglin/cuda-python that referenced this pull request Nov 24, 2026
Adopt the test helpers from NVIDIA#3544 in the tests this branch adds: import
pools with enter_imported_pool, which replaces _enter_import, and run the
lookup race test's threads with run_in_threads, which replaces
_run_threads and the hand-written error collection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
juenglin added a commit to juenglin/cuda-python that referenced this pull request Nov 25, 2026
test_concurrent_set_current_keeps_one_primary_retain runs its workers
with run_in_threads (NVIDIA#3544), which aborts the barrier if a worker fails
and bounds the joins.

test_green_context_replaces_stale_plain_entry notes rollback failures
with BaseException.add_note() instead of the _attach_rollback_failure_for_testing
production hook, which NVIDIA#3555 moves out of _rt.

test_driver_calls_go_through_the_table: the primary-context table's mutex
is a second site of raw driver calls under a lock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
github-actions Bot pushed a commit that referenced this pull request Nov 25, 2026
Removed preview folders for the following PRs:
- PR #3499
- PR #3524
- PR #3543
- PR #3544
- PR #3546
- PR #3551
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda.core Everything related to the cuda.core module test Improvements or additions to tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants