From 7f589e0d830ff5e2ccd2aa1e24911fc9e28c7b20 Mon Sep 17 00:00:00 2001 From: xyaz1313 Date: Wed, 15 Apr 2026 05:12:23 +0800 Subject: [PATCH] Fix flaky test_max_pending_count by storing references to prevent premature GC The test creates DeviceNDArray objects without storing references, allowing Python's GC to non-deterministically collect them between loop iterations. This causes extra deallocations to appear in the pending queue, making assertions like 'len(deallocs) == i + 1' fail intermittently. Fix: store all arrays in a list, then explicitly delete them one by one. Added gc.collect() calls to ensure finalizers fire before assertions. Fixes #856 --- .../numba/cuda/tests/cudadrv/test_deallocations.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/numba_cuda/numba/cuda/tests/cudadrv/test_deallocations.py b/numba_cuda/numba/cuda/tests/cudadrv/test_deallocations.py index 63af86a3f..b23f3cb92 100644 --- a/numba_cuda/numba/cuda/tests/cudadrv/test_deallocations.py +++ b/numba_cuda/numba/cuda/tests/cudadrv/test_deallocations.py @@ -2,6 +2,7 @@ # SPDX-License-Identifier: BSD-2-Clause from contextlib import contextmanager +import gc import numpy as np @@ -24,12 +25,18 @@ def test_max_pending_count(self): deallocs = cuda.current_context().memory_manager.deallocations deallocs.clear() self.assertEqual(len(deallocs), 0) - # deallocate to maximum count + # Allocate device arrays, keeping references to prevent GC from + # collecting them prematurely (which caused flaky counts). + arrays = [cuda.to_device(np.arange(1)) + for _ in range(config.CUDA_DEALLOCS_COUNT)] + # Delete arrays one by one and check pending count increments for i in range(config.CUDA_DEALLOCS_COUNT): - cuda.to_device(np.arange(1)) + del arrays[i] + gc.collect() # Ensure finalizers run self.assertEqual(len(deallocs), i + 1) # one more to trigger .clear() cuda.to_device(np.arange(1)) + gc.collect() self.assertEqual(len(deallocs), 0) @skip_if_external_memmgr("Deallocation specific to Numba memory management")