-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path0012-Python-Keep-reference-to-callback-function-on-timeou.patch
170 lines (146 loc) · 8.28 KB
/
0012-Python-Keep-reference-to-callback-function-on-timeou.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
From f5f84d9e4a7296680a606ab4c6ddd0e265374efc Mon Sep 17 00:00:00 2001
From: Stefan Agner <stefan@agner.ch>
Date: Wed, 13 Dec 2023 19:51:20 +0100
Subject: [PATCH] [Python] Keep reference to callback function on timeout
(#30877)
* [Python] Keep reference to callback function on timeout
When using a timeout when calling GetConnectedDeviceSync() the callback
function DeviceAvailableCallback() gets potentially GC'ed. Make sure
we hold a reference to the instance.
* Use correct namespace for PyObject
* Fix types in pychip_GetConnectedDeviceByNodeId
* Call callback with context (self)
* Correctly pass context
* Use separate closure function
---
.../ChipDeviceController-ScriptBinding.cpp | 17 ++++-----
src/controller/python/chip/ChipDeviceCtrl.py | 35 ++++++++++++-------
2 files changed, 31 insertions(+), 21 deletions(-)
diff --git a/src/controller/python/ChipDeviceController-ScriptBinding.cpp b/src/controller/python/ChipDeviceController-ScriptBinding.cpp
index da742b2463..504b952d8a 100644
--- a/src/controller/python/ChipDeviceController-ScriptBinding.cpp
+++ b/src/controller/python/ChipDeviceController-ScriptBinding.cpp
@@ -87,7 +87,7 @@ using namespace chip::DeviceLayer;
extern "C" {
typedef void (*ConstructBytesArrayFunct)(const uint8_t * dataBuf, uint32_t dataLen);
typedef void (*LogMessageFunct)(uint64_t time, uint64_t timeUS, const char * moduleName, uint8_t category, const char * msg);
-typedef void (*DeviceAvailableFunc)(DeviceProxy * device, PyChipError err);
+typedef void (*DeviceAvailableFunc)(chip::Controller::Python::PyObject * context, DeviceProxy * device, PyChipError err);
typedef void (*ChipThreadTaskRunnerFunct)(intptr_t context);
typedef void (*DeviceUnpairingCompleteFunct)(uint64_t nodeId, PyChipError error);
}
@@ -202,7 +202,7 @@ const char * pychip_Stack_StatusReportToString(uint32_t profileId, uint16_t stat
void pychip_Stack_SetLogFunct(LogMessageFunct logFunct);
PyChipError pychip_GetConnectedDeviceByNodeId(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId,
- DeviceAvailableFunc callback);
+ chip::Controller::Python::PyObject * context, DeviceAvailableFunc callback);
PyChipError pychip_FreeOperationalDeviceProxy(chip::OperationalDeviceProxy * deviceProxy);
PyChipError pychip_GetLocalSessionId(chip::OperationalDeviceProxy * deviceProxy, uint16_t * localSessionId);
PyChipError pychip_GetNumSessionsToPeer(chip::OperationalDeviceProxy * deviceProxy, uint32_t * numSessions);
@@ -706,36 +706,37 @@ namespace {
struct GetDeviceCallbacks
{
- GetDeviceCallbacks(DeviceAvailableFunc callback) :
- mOnSuccess(OnDeviceConnectedFn, this), mOnFailure(OnConnectionFailureFn, this), mCallback(callback)
+ GetDeviceCallbacks(chip::Controller::Python::PyObject * context, DeviceAvailableFunc callback) :
+ mOnSuccess(OnDeviceConnectedFn, this), mOnFailure(OnConnectionFailureFn, this), mContext(context), mCallback(callback)
{}
static void OnDeviceConnectedFn(void * context, Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle)
{
auto * self = static_cast<GetDeviceCallbacks *>(context);
auto * operationalDeviceProxy = new OperationalDeviceProxy(&exchangeMgr, sessionHandle);
- self->mCallback(operationalDeviceProxy, ToPyChipError(CHIP_NO_ERROR));
+ self->mCallback(self->mContext, operationalDeviceProxy, ToPyChipError(CHIP_NO_ERROR));
delete self;
}
static void OnConnectionFailureFn(void * context, const ScopedNodeId & peerId, CHIP_ERROR error)
{
auto * self = static_cast<GetDeviceCallbacks *>(context);
- self->mCallback(nullptr, ToPyChipError(error));
+ self->mCallback(self->mContext, nullptr, ToPyChipError(error));
delete self;
}
Callback::Callback<OnDeviceConnected> mOnSuccess;
Callback::Callback<OnDeviceConnectionFailure> mOnFailure;
+ chip::Controller::Python::PyObject * const mContext;
DeviceAvailableFunc mCallback;
};
} // anonymous namespace
PyChipError pychip_GetConnectedDeviceByNodeId(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId,
- DeviceAvailableFunc callback)
+ chip::Controller::Python::PyObject * context, DeviceAvailableFunc callback)
{
VerifyOrReturnError(devCtrl != nullptr, ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT));
- auto * callbacks = new GetDeviceCallbacks(callback);
+ auto * callbacks = new GetDeviceCallbacks(context, callback);
return ToPyChipError(devCtrl->GetConnectedDevice(nodeId, &callbacks->mOnSuccess, &callbacks->mOnFailure));
}
diff --git a/src/controller/python/chip/ChipDeviceCtrl.py b/src/controller/python/chip/ChipDeviceCtrl.py
index 08dbdff224..1572829591 100644
--- a/src/controller/python/chip/ChipDeviceCtrl.py
+++ b/src/controller/python/chip/ChipDeviceCtrl.py
@@ -76,7 +76,7 @@ _DevicePairingDelegate_OnFabricCheckFunct = CFUNCTYPE(
#
# CHIP_ERROR is actually signed, so using c_uint32 is weird, but everything
# else seems to do it.
-_DeviceAvailableFunct = CFUNCTYPE(None, c_void_p, PyChipError)
+_DeviceAvailableCallbackFunct = CFUNCTYPE(None, py_object, c_void_p, PyChipError)
_IssueNOCChainCallbackPythonCallbackFunct = CFUNCTYPE(
None, py_object, PyChipError, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_uint64)
@@ -100,6 +100,11 @@ class NOCChain:
adminSubject: int
+@_DeviceAvailableCallbackFunct
+def _DeviceAvailableCallback(closure, device, err):
+ closure.deviceAvailable(device, err)
+
+
@_IssueNOCChainCallbackPythonCallbackFunct
def _IssueNOCChainCallbackPythonCallback(devCtrl, status: PyChipError, noc: c_void_p, nocLen: int, icac: c_void_p,
icacLen: int, rcac: c_void_p, rcacLen: int, ipk: c_void_p, ipkLen: int, adminSubject: int):
@@ -743,16 +748,6 @@ class ChipDeviceControllerBase():
returnErr = None
deviceAvailableCV = threading.Condition()
- @_DeviceAvailableFunct
- def DeviceAvailableCallback(device, err):
- nonlocal returnDevice
- nonlocal returnErr
- nonlocal deviceAvailableCV
- with deviceAvailableCV:
- returnDevice = c_void_p(device)
- returnErr = err
- deviceAvailableCV.notify_all()
-
if allowPASE:
res = self._ChipStack.Call(lambda: self._dmLib.pychip_GetDeviceBeingCommissioned(
self.devCtrl, nodeid, byref(returnDevice)), timeoutMs)
@@ -760,8 +755,22 @@ class ChipDeviceControllerBase():
print('Using PASE connection')
return DeviceProxyWrapper(returnDevice)
+ class DeviceAvailableClosure():
+ def deviceAvailable(self, device, err):
+ nonlocal returnDevice
+ nonlocal returnErr
+ nonlocal deviceAvailableCV
+ with deviceAvailableCV:
+ returnDevice = c_void_p(device)
+ returnErr = err
+ deviceAvailableCV.notify_all()
+ ctypes.pythonapi.Py_DecRef(ctypes.py_object(self))
+
+ closure = DeviceAvailableClosure()
+ ctypes.pythonapi.Py_IncRef(ctypes.py_object(closure))
self._ChipStack.Call(lambda: self._dmLib.pychip_GetConnectedDeviceByNodeId(
- self.devCtrl, nodeid, DeviceAvailableCallback), timeoutMs).raise_on_error()
+ self.devCtrl, nodeid, ctypes.py_object(closure), _DeviceAvailableCallback),
+ timeoutMs).raise_on_error()
# The callback might have been received synchronously (during self._ChipStack.Call()).
# Check if the device is already set before waiting for the callback.
@@ -1491,7 +1500,7 @@ class ChipDeviceControllerBase():
self._dmLib.pychip_ScriptDevicePairingDelegate_SetFabricCheckCallback.restype = PyChipError
self._dmLib.pychip_GetConnectedDeviceByNodeId.argtypes = [
- c_void_p, c_uint64, _DeviceAvailableFunct]
+ c_void_p, c_uint64, py_object, _DeviceAvailableCallbackFunct]
self._dmLib.pychip_GetConnectedDeviceByNodeId.restype = PyChipError
self._dmLib.pychip_FreeOperationalDeviceProxy.argtypes = [
--
2.44.0