Skip to content

Commit 30721cd

Browse files
committed
DISPATCH-1962 Fix leak of IoAdapter_init
1 parent 23558e0 commit 30721cd

File tree

5 files changed

+30
-14
lines changed

5 files changed

+30
-14
lines changed

src/dispatch.c

+1
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ qd_error_t qd_dispatch_prepare(qd_dispatch_t *qd)
341341
void qd_dispatch_set_agent(qd_dispatch_t *qd, void *agent) {
342342
assert(agent);
343343
assert(!qd->agent);
344+
Py_IncRef(agent);
344345
qd->agent = agent;
345346
}
346347

src/python_embedded.c

+21-5
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,8 @@ static PyTypeObject LogAdapterType = {
564564
.tp_dealloc = (destructor)LogAdapter_dealloc,
565565
.tp_flags = Py_TPFLAGS_DEFAULT,
566566
.tp_methods = LogAdapter_methods,
567-
.tp_init = (initproc)LogAdapter_init
567+
.tp_init = (initproc)LogAdapter_init,
568+
.tp_new = PyType_GenericNew,
568569
};
569570

570571

@@ -718,10 +719,24 @@ static int IoAdapter_init(IoAdapter *self, PyObject *args, PyObject *kwds)
718719
return 0;
719720
}
720721

722+
// visit all members which may conceivably participate in reference cycles
723+
static int IoAdapter_traverse(IoAdapter* self, visitproc visit, void *arg)
724+
{
725+
Py_VISIT(self->handler);
726+
return 0;
727+
}
728+
729+
static int IoAdapter_clear(IoAdapter* self)
730+
{
731+
Py_CLEAR(self->handler);
732+
return 0;
733+
}
734+
721735
static void IoAdapter_dealloc(IoAdapter* self)
722736
{
723737
qdr_core_unsubscribe(self->sub);
724-
Py_DECREF(self->handler);
738+
PyObject_GC_UnTrack(self);
739+
IoAdapter_clear(self);
725740
Py_TYPE(self)->tp_free((PyObject*)self);
726741
}
727742

@@ -814,10 +829,13 @@ static PyTypeObject IoAdapterType = {
814829
.tp_name = DISPATCH_MODULE ".IoAdapter",
815830
.tp_doc = "Dispatch IO Adapter",
816831
.tp_basicsize = sizeof(IoAdapter),
832+
.tp_traverse = (traverseproc)IoAdapter_traverse,
833+
.tp_clear = (inquiry)IoAdapter_clear,
817834
.tp_dealloc = (destructor)IoAdapter_dealloc,
818-
.tp_flags = Py_TPFLAGS_DEFAULT,
835+
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
819836
.tp_methods = IoAdapter_methods,
820837
.tp_init = (initproc)IoAdapter_init,
838+
.tp_new = PyType_GenericNew,
821839
};
822840

823841

@@ -833,8 +851,6 @@ static void qd_register_constant(PyObject *module, const char *name, uint32_t va
833851

834852
static void qd_python_setup(void)
835853
{
836-
LogAdapterType.tp_new = PyType_GenericNew;
837-
IoAdapterType.tp_new = PyType_GenericNew;
838854
if ((PyType_Ready(&LogAdapterType) < 0) || (PyType_Ready(&IoAdapterType) < 0)) {
839855
qd_error_py();
840856
qd_log(log_source, QD_LOG_CRITICAL, "Unable to initialize Adapters");

src/router_node.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2145,12 +2145,12 @@ void qd_router_free(qd_router_t *router)
21452145

21462146
qd_container_set_default_node_type(router->qd, 0, 0, QD_DIST_BOTH);
21472147

2148+
qd_router_python_free(router);
21482149
qdr_core_free(router->router_core);
21492150
qd_tracemask_free(router->tracemask);
21502151
qd_timer_free(router->timer);
21512152
sys_mutex_free(router->lock);
21522153
qd_router_configure_free(router);
2153-
qd_router_python_free(router);
21542154

21552155
free(router);
21562156
free(node_id);

src/router_pynode.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ qd_error_t qd_router_python_setup(qd_router_t *router)
447447
// Instantiate the router
448448
//
449449
pyRouter = PyObject_CallObject(pClass, pArgs);
450+
Py_DECREF(pClass);
450451
Py_DECREF(pArgs);
451452
Py_DECREF(adapterType);
452453
QD_ERROR_PY_RET();
@@ -459,7 +460,12 @@ qd_error_t qd_router_python_setup(qd_router_t *router)
459460
}
460461

461462
void qd_router_python_free(qd_router_t *router) {
462-
// empty
463+
Py_XDECREF(pyRouter);
464+
Py_XDECREF(pyTick);
465+
Py_XDECREF(pySetMobileSeq);
466+
Py_XDECREF(pySetMyMobileSeq);
467+
Py_XDECREF(pyLinkLost);
468+
PyGC_Collect();
463469
}
464470

465471

tests/lsan.supp

-7
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
# found by AddressSanitizer (ASAN)
33
#
44

5-
# to be triaged; pretty much all tests
6-
leak:^IoAdapter_init$
7-
85
# to be triaged; pretty much all tests
96
leak:^load_server_config$
107
leak:^qd_dispatch_configure_connector$
@@ -36,10 +33,6 @@ leak:^pni_init_default_logger$
3633
# DISPATCH-1844 - shutdown leak
3734
leak:sys_mutex
3835

39-
# expected, not a bug:
40-
#
41-
leak:qdr_core_subscribe
42-
4336
# Ubuntu 16.04 (Xenial)
4437
#
4538
leak:_ctypes_alloc_format_string

0 commit comments

Comments
 (0)