Skip to content

Commit 9eab6a8

Browse files
committed
DISPATCH-1962 Fix leak of IoAdapter_init
1 parent 9718ba8 commit 9eab6a8

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
@@ -562,7 +562,8 @@ static PyTypeObject LogAdapterType = {
562562
.tp_dealloc = (destructor)LogAdapter_dealloc,
563563
.tp_flags = Py_TPFLAGS_DEFAULT,
564564
.tp_methods = LogAdapter_methods,
565-
.tp_init = (initproc)LogAdapter_init
565+
.tp_init = (initproc)LogAdapter_init,
566+
.tp_new = PyType_GenericNew,
566567
};
567568

568569

@@ -716,10 +717,24 @@ static int IoAdapter_init(IoAdapter *self, PyObject *args, PyObject *kwds)
716717
return 0;
717718
}
718719

720+
// visit all members which may conceivably participate in reference cycles
721+
static int IoAdapter_traverse(IoAdapter* self, visitproc visit, void *arg)
722+
{
723+
Py_VISIT(self->handler);
724+
return 0;
725+
}
726+
727+
static int IoAdapter_clear(IoAdapter* self)
728+
{
729+
Py_CLEAR(self->handler);
730+
return 0;
731+
}
732+
719733
static void IoAdapter_dealloc(IoAdapter* self)
720734
{
721735
qdr_core_unsubscribe(self->sub);
722-
Py_DECREF(self->handler);
736+
PyObject_GC_UnTrack(self);
737+
IoAdapter_clear(self);
723738
Py_TYPE(self)->tp_free((PyObject*)self);
724739
}
725740

@@ -812,10 +827,13 @@ static PyTypeObject IoAdapterType = {
812827
.tp_name = DISPATCH_MODULE ".IoAdapter",
813828
.tp_doc = "Dispatch IO Adapter",
814829
.tp_basicsize = sizeof(IoAdapter),
830+
.tp_traverse = (traverseproc)IoAdapter_traverse,
831+
.tp_clear = (inquiry)IoAdapter_clear,
815832
.tp_dealloc = (destructor)IoAdapter_dealloc,
816-
.tp_flags = Py_TPFLAGS_DEFAULT,
833+
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
817834
.tp_methods = IoAdapter_methods,
818835
.tp_init = (initproc)IoAdapter_init,
836+
.tp_new = PyType_GenericNew,
819837
};
820838

821839

@@ -831,8 +849,6 @@ static void qd_register_constant(PyObject *module, const char *name, uint32_t va
831849

832850
static void qd_python_setup(void)
833851
{
834-
LogAdapterType.tp_new = PyType_GenericNew;
835-
IoAdapterType.tp_new = PyType_GenericNew;
836852
if ((PyType_Ready(&LogAdapterType) < 0) || (PyType_Ready(&IoAdapterType) < 0)) {
837853
qd_error_py();
838854
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
@@ -2153,12 +2153,12 @@ void qd_router_free(qd_router_t *router)
21532153

21542154
qd_container_set_default_node_type(router->qd, 0, 0, QD_DIST_BOTH);
21552155

2156+
qd_router_python_free(router);
21562157
qdr_core_free(router->router_core);
21572158
qd_tracemask_free(router->tracemask);
21582159
qd_timer_free(router->timer);
21592160
sys_mutex_free(router->lock);
21602161
qd_router_configure_free(router);
2161-
qd_router_python_free(router);
21622162

21632163
free(router);
21642164
free(node_id);

src/router_pynode.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ qd_error_t qd_router_python_setup(qd_router_t *router)
444444
// Instantiate the router
445445
//
446446
pyRouter = PyObject_CallObject(pClass, pArgs);
447+
Py_DECREF(pClass);
447448
Py_DECREF(pArgs);
448449
Py_DECREF(adapterType);
449450
QD_ERROR_PY_RET();
@@ -456,7 +457,12 @@ qd_error_t qd_router_python_setup(qd_router_t *router)
456457
}
457458

458459
void qd_router_python_free(qd_router_t *router) {
459-
// empty
460+
Py_XDECREF(pyRouter);
461+
Py_XDECREF(pyTick);
462+
Py_XDECREF(pySetMobileSeq);
463+
Py_XDECREF(pySetMyMobileSeq);
464+
Py_XDECREF(pyLinkLost);
465+
PyGC_Collect();
460466
}
461467

462468

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)