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
|
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License
#
"""
PROTON-2121 python-qpid-proton 0.28 BlockingConnection leaks connections (does not close file descriptors)
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import contextlib
import gc
import os
import socket
import subprocess
import threading
import unittest
import warnings
import cproton
import proton.handlers
import proton.utils
import proton.reactor
def get_fd_set():
# type: () -> set[str]
return set(os.listdir('/proc/self/fd/'))
@contextlib.contextmanager
def no_fd_leaks(test):
# type: (unittest.TestCase) -> None
with warnings.catch_warnings(record=True) as ws:
before = get_fd_set()
yield
delta = get_fd_set().difference(before)
if len(delta) != 0:
subprocess.check_call("ls -lF /proc/{0}/fd/".format(os.getpid()), shell=True)
test.fail("Found {0} new fd(s) after the test".format(delta))
if len(ws) > 0:
test.fail([w.message for w in ws])
class Broker(proton.handlers.MessagingHandler):
def __init__(self, acceptor_url):
# type: (str) -> None
super(Broker, self).__init__()
self.acceptor_url = acceptor_url
self.acceptor = None
self._acceptor_opened_event = threading.Event()
def get_acceptor_sockname(self):
# type: () -> (str, int)
self._acceptor_opened_event.wait()
if hasattr(self.acceptor, '_selectable'): # proton 0.30.0+
sockname = self.acceptor._selectable._delegate.getsockname()
else: # works in proton 0.27.0
selectable = cproton.pn_cast_pn_selectable(self.acceptor._impl)
fd = cproton.pn_selectable_get_fd(selectable)
s = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
sockname = s.getsockname()
return sockname[:2]
def on_start(self, event):
self.acceptor = event.container.listen(self.acceptor_url)
self._acceptor_opened_event.set()
def on_link_opening(self, event):
if event.link.is_sender:
assert not event.link.remote_source.dynamic, "This cannot happen"
event.link.source.address = event.link.remote_source.address
elif event.link.remote_target.address:
event.link.target.address = event.link.remote_target.address
@contextlib.contextmanager
def test_broker():
broker = Broker('localhost:0')
container = proton.reactor.Container(broker)
threading.Thread(target=container.run).start()
try:
yield broker
finally:
container.stop()
PROC_SELF_FD_EXISTS = os.path.exists("/proc/self/fd"), "Skipped: Directory /proc/self/fd does not exist"
class BlockingConnectionFDLeakTests(unittest.TestCase):
@unittest.skipUnless(*PROC_SELF_FD_EXISTS)
@unittest.expectedFailure
def test_just_start_stop_test_broker(self):
with no_fd_leaks(self):
with test_broker() as broker:
broker.get_acceptor_sockname() # wait for acceptor to open
gc.collect()
@unittest.skipUnless(*PROC_SELF_FD_EXISTS)
@unittest.expectedFailure
def test_connection_close_all(self):
with no_fd_leaks(self):
with test_broker() as broker:
c = proton.utils.BlockingConnection("{0}:{1}".format(*broker.get_acceptor_sockname()))
c.close()
gc.collect()
@unittest.skipUnless(*PROC_SELF_FD_EXISTS)
def test_connection_close_all__do_not_check_test_broker(self):
with test_broker() as broker:
acceptor_sockname = broker.get_acceptor_sockname()
with no_fd_leaks(self):
c = proton.utils.BlockingConnection("{0}:{1}".format(*acceptor_sockname))
c.close()
gc.collect()
@unittest.skipUnless(*PROC_SELF_FD_EXISTS)
@unittest.expectedFailure
def test_connection_sender_close_all(self):
with no_fd_leaks(self):
with test_broker() as broker:
c = proton.utils.BlockingConnection("{0}:{1}".format(*broker.get_acceptor_sockname()))
s = c.create_sender("anAddress")
s.close()
c.close()
gc.collect()
@unittest.skipUnless(*PROC_SELF_FD_EXISTS)
@unittest.expectedFailure
def test_connection_receiver_close_all(self):
with no_fd_leaks(self):
with test_broker() as broker:
c = proton.utils.BlockingConnection("{0}:{1}".format(*broker.get_acceptor_sockname()))
s = c.create_receiver("anAddress")
s.close()
c.close()
gc.collect()
|