Skip to content

Commit a207b1a

Browse files
authored
Fix NumPy owner release from Core ML queues (#2829)
1 parent db4dd46 commit a207b1a

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

‎coremlpython/CoreMLPythonArray.mm‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,15 @@ - (PybindCompatibleArray *)initWithArray:(py::array)array {
6565
return self;
6666
}
6767

68+
- (void)dealloc {
69+
// Core ML may release the multi-array on one of its private queues. Clear
70+
// the Python owner while holding the GIL so py::array does not decrement
71+
// its reference count from a non-Python thread.
72+
py::handle array = m_array.release();
73+
if (array) {
74+
py::gil_scoped_acquire gil;
75+
array.dec_ref();
76+
}
77+
}
78+
6879
@end
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Copyright (c) 2026, Apple Inc. All rights reserved.
2+
#
3+
# Use of this source code is governed by a BSD-3-clause license that can be
4+
# found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5+
6+
import subprocess
7+
import sys
8+
9+
import pytest
10+
11+
import coremltools as ct
12+
from coremltools.converters.mil import Builder as mb
13+
14+
15+
_NUMPY_OWNER_RELEASE_SCRIPT = """
16+
import ctypes
17+
import sys
18+
import time
19+
import weakref
20+
21+
import coremltools as ct
22+
import numpy as np
23+
24+
model = ct.models.MLModel(sys.argv[1], compute_units=ct.ComputeUnit.CPU_ONLY)
25+
input_name = model.get_spec().description.input[0].name
26+
27+
check_gil = ctypes.pythonapi.PyGILState_Check
28+
check_gil.restype = ctypes.c_int
29+
gil_states = []
30+
31+
input_array = np.zeros((1, 4), dtype=np.float32)
32+
input_ref = weakref.ref(
33+
input_array,
34+
lambda _: gil_states.append(bool(check_gil())),
35+
)
36+
model.predict({input_name: input_array})
37+
del input_array
38+
39+
deadline = time.monotonic() + 10
40+
while input_ref() is not None and time.monotonic() < deadline:
41+
time.sleep(0.05)
42+
43+
assert input_ref() is None, "Core ML did not release the NumPy input owner"
44+
assert gil_states == [True], f"NumPy input owner release GIL states: {gil_states}"
45+
"""
46+
47+
48+
@pytest.mark.skipif(
49+
ct.utils._macos_version() < (12, 0),
50+
reason="ML Program prediction is available only on macOS 12+",
51+
)
52+
def test_numpy_input_owner_is_released_with_gil(tmp_path):
53+
@mb.program(
54+
input_specs=[mb.TensorSpec(shape=(1, 4))],
55+
opset_version=ct.target.iOS16,
56+
)
57+
def program(x):
58+
return mb.add(x=x, y=1.0)
59+
60+
model = ct.convert(
61+
program,
62+
convert_to="mlprogram",
63+
compute_precision=ct.precision.FLOAT32,
64+
)
65+
model_path = tmp_path / "add_one.mlpackage"
66+
model.save(str(model_path))
67+
68+
# Core ML may release the NumPy owner asynchronously. Use a subprocess so
69+
# an unsafe release is reported as a test failure instead of terminating
70+
# the entire pytest process.
71+
result = subprocess.run(
72+
[sys.executable, "-c", _NUMPY_OWNER_RELEASE_SCRIPT, str(model_path)],
73+
capture_output=True,
74+
check=False,
75+
text=True,
76+
timeout=30,
77+
)
78+
79+
assert result.returncode == 0, (
80+
f"Prediction subprocess exited with {result.returncode}\n"
81+
f"stdout:\n{result.stdout}\n"
82+
f"stderr:\n{result.stderr}"
83+
)

0 commit comments

Comments
 (0)