Skip to content

Fix sycl::queue leak in usm_ndarray get_queue()/get_device() - #3042

Open
vlad-perevezentsev wants to merge 8 commits into
masterfrom
fix_queue_leak
Open

Fix sycl::queue leak in usm_ndarray get_queue()/get_device()#3042
vlad-perevezentsev wants to merge 8 commits into
masterfrom
fix_queue_leak

Conversation

@vlad-perevezentsev

@vlad-perevezentsev vlad-perevezentsev commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

This PR fixes a memory leak in the C++ accessors usm_ndarray::get_queue() and get_device() in dpnp/include/dpnp4pybind11.hpp

UsmNDArray_GetQueueRef_ (dpnp's usm_ndarray.get_queue_ref()) returns an owning copy of the queue (it calls DPCTLQueue_Copy i.e. new sycl::queue)
Both accessors dereferenced this pointer and copied the queue out but never freed the heap copy, leaking one sycl::queue per call. Under queue churn this keeps queue_impl alive and prevents reuse of the associated Level Zero command lists / event pools.

The fix adds UsmNDArray_RemoveQueueRef C-API to dpnp_capi (it binds DPCTLQueue_Delete); get_queue()/get_device() take ownership of the returned copy with std::unique_ptr<sycl::queue> whose custom deleter frees it via UsmNDArray_RemoveQueueRef. This keeps allocation and deallocation on dpctl's side of the ABI.

dpctl needs no change because its Memory_GetQueueRef returns a borrowed ref, whereas dpnp's UsmNDArray_GetQueueRef is documented to return a copy so it must be deleted.

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?

@github-actions

Copy link
Copy Markdown
Contributor

View rendered docs @ https://intelpython.github.io/dpnp/pull/3042/index.html

@github-actions

github-actions Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.21.0dev6=py314ha0e2e8e_25 ran successfully.
Passed: 1375
Failed: 0
Skipped: 7

@coveralls

coveralls commented Aug 26, 2026

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 78.471% (-0.01%) from 78.483% — fix_queue_leak into master

@antonwolfy antonwolfy added this to the 0.21.0 release milestone Aug 26, 2026
// UsmNDArray_GetQueueRef_ returns an owning copy (DPCTLQueue_Copy,
// i.e. `new sycl::queue`); wrap it in a unique_ptr to avoid leaking a
// queue per call.
DPCTLSyclQueueRef QRef = api.UsmNDArray_GetQueueRef_(raw_ar);

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.

QRef might be NULL on OOM inside DPCTLQueue_Copy, while below dereferencing of NULL is UB.
It seems legacy and can be done in the follow-up PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

A good catch
I will be implemented in the follow-up PR

Comment thread dpnp/include/dpnp4pybind11.hpp Outdated
Comment thread dpnp/include/dpnp4pybind11.hpp Outdated

@antonwolfy antonwolfy 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.

Thank you @vlad-perevezentsev, LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants