Skip to content

Commit aacf272

Browse files
committed
DISPATCH-1962 Fix leak of IoAdapter_init
1 parent b207af3 commit aacf272

File tree

7 files changed

+60
-14
lines changed

7 files changed

+60
-14
lines changed

.travis.yml

+18
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ jobs:
7474
- sudo add-apt-repository 'deb http://apt.llvm.org/focal/ llvm-toolchain-focal-12 main' -y
7575
- sudo apt-get update -q
7676
- sudo apt-get install -y clang-12 llvm-12-dev -o Debug::pkgProblemResolver=yes
77+
# Python 3.8.2 in Ubuntu LTS has ASan issues
78+
- sudo add-apt-repository -y ppa:deadsnakes/ppa
79+
- sudo apt-get update
80+
- sudo apt-get install python3.9 python3.9-dev python3.9-distutils
81+
- sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 10
82+
- sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 10
7783
# https://github.com/pypa/virtualenv/issues/1740
7884
# https://github.com/pypa/virtualenv/issues/1873
7985
- python -m pip install --user --upgrade pip
@@ -121,6 +127,13 @@ jobs:
121127
compiler: clang
122128
before_install:
123129
- sudo apt-get install clang-11 llvm-11-dev
130+
# Python 3.8.2 in Ubuntu LTS has ASan issues
131+
- sudo apt install -y python3-pip
132+
- sudo add-apt-repository -y ppa:deadsnakes/ppa
133+
- sudo apt-get update
134+
- sudo apt-get install python3.9 python3.9-dev python3.9-distutils
135+
- sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 10
136+
- sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 10
124137
# Install and use the latest Node.js LTS version
125138
- nvm install "lts/*"
126139
# https://github.com/pypa/virtualenv/issues/1740
@@ -148,6 +161,11 @@ jobs:
148161
os: linux
149162
dist: focal
150163
before_install:
164+
- sudo add-apt-repository -y ppa:deadsnakes/ppa
165+
- sudo apt-get update
166+
- sudo apt-get install python3.9 python3.9-dev python3.9-distutils
167+
- sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 10
168+
- sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 10
151169
# https://github.com/pypa/virtualenv/issues/1740
152170
# https://github.com/pypa/virtualenv/issues/1873
153171
- python -m pip install --user --upgrade pip

src/dispatch.c

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

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
@@ -2154,12 +2154,12 @@ void qd_router_free(qd_router_t *router)
21542154

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

2157+
qd_router_python_free(router);
21572158
qdr_core_free(router->router_core);
21582159
qd_tracemask_free(router->tracemask);
21592160
qd_timer_free(router->timer);
21602161
sys_mutex_free(router->lock);
21612162
qd_router_configure_free(router);
2162-
qd_router_python_free(router);
21632163

21642164
free(router);
21652165
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

-3
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

tests/system_test.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import shutil
4040
import socket
4141
import subprocess
42+
import sys
43+
import time
4244
from copy import copy
4345
from datetime import datetime
4446
from subprocess import PIPE, STDOUT
@@ -191,10 +193,13 @@ def port_available(port, protocol_family='IPv4'):
191193
def wait_port(port, protocol_family='IPv4', **retry_kwargs):
192194
"""Wait up to timeout for port (on host) to be connectable.
193195
Takes same keyword arguments as retry to control the timeout"""
196+
194197
def check(e):
195198
"""Only retry on connection refused"""
196-
if not isinstance(e, socket.error) or not e.errno == errno.ECONNREFUSED:
197-
raise
199+
# TODO(DISPATCH-1539): in Python 3.3+ it is sufficient to catch only OSError
200+
if isinstance(e, (socket.error, IOError, OSError)) and e.errno == errno.ECONNREFUSED:
201+
return
202+
raise
198203

199204
host = None
200205

@@ -338,7 +343,11 @@ def __init__(self, name=None, listen_port=None, wait=True,
338343
self.args += self.cl_args
339344
super(Http2Server, self).__init__(self.args, name=name, expect=expect)
340345
if wait:
341-
self.wait_ready()
346+
try:
347+
self.wait_ready()
348+
except Exception:
349+
self.teardown()
350+
raise
342351

343352
def wait_ready(self, **retry_kwargs):
344353
"""
@@ -463,7 +472,6 @@ def __init__(self, name=None, config=Config(), pyinclude=None, wait=True,
463472
elif env_home:
464473
args += ['-I', os.path.join(env_home, 'python')]
465474

466-
args = os.environ.get('QPID_DISPATCH_RUNNER', '').split() + args
467475
super(Qdrouterd, self).__init__(args, name=name, expect=expect)
468476
self._management = None
469477
self._wait_ready = False

0 commit comments

Comments
 (0)