Skip to content

Commit 395263c

Browse files
committed
pybind/cephfs: increment ref before calling out to c++
At the time this construction seemed safe since the caller should have a reference but it could conveivably be the only ref. We don't want the ref count to reach 0. Additionally, catch errors so this callback is genuinely noexcept. Signed-off-by: Patrick Donnelly <[email protected]>
1 parent 302756c commit 395263c

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/pybind/cephfs/cephfs.pyx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,14 @@ cdef extern from "Python.h":
114114
cdef void completion_callback(int rc, const void* out, size_t outlen, const void* outs, size_t outslen, void* ud) nogil:
115115
# This GIL awkwardness is due to incompatible types with function pointers defined with mds_command2:
116116
with gil:
117-
pyout = (<unsigned char*>out)[:outlen]
118-
pyouts = (<unsigned char*>outs)[:outslen]
119-
(<object>ud).complete(rc, pyout, pyouts)
120-
ref.Py_DECREF(<object>ud)
117+
try:
118+
pyout = (<unsigned char*>out)[:outlen]
119+
pyouts = (<unsigned char*>outs)[:outslen]
120+
(<object>ud).complete(rc, pyout, pyouts)
121+
except:
122+
pass # we can't handle this in any useful way: e.g. which thread should get the exception?
123+
finally:
124+
ref.Py_DECREF(<object>ud)
121125

122126
class Error(Exception):
123127
def get_error_code(self):
@@ -2316,16 +2320,16 @@ cdef class LibCephFS(object):
23162320

23172321

23182322
try:
2323+
ref.Py_INCREF(result)
23192324
with nogil:
23202325
ret = ceph_mds_command2(self.cluster, _mds_spec,
23212326
<const char **>_cmd, _cmdlen,
23222327
<const char*>_inbuf, _inbuf_len,
23232328
_one_shot,
23242329
completion_callback,
23252330
<void*>result)
2326-
if ret == 0:
2327-
ref.Py_INCREF(result)
2328-
else:
2331+
if ret != 0:
2332+
ref.Py_DECREF(result)
23292333
raise make_ex(ret, "error in mds_command2")
23302334
finally:
23312335
free(_cmd)

0 commit comments

Comments
 (0)