| 1 |
# |
| 2 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
# or more contributor license agreements. See the NOTICE file |
| 4 |
# distributed with this work for additional information |
| 5 |
# regarding copyright ownership. The ASF licenses this file |
| 6 |
# to you under the Apache License, Version 2.0 (the |
| 7 |
# "License"); you may not use this file except in compliance |
| 8 |
# with the License. You may obtain a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, |
| 13 |
# software distributed under the License is distributed on an |
| 14 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
# KIND, either express or implied. See the License for the |
| 16 |
# specific language governing permissions and limitations |
| 17 |
# under the License |
| 18 |
# |
| 19 |
|
| 20 |
""" |
| 21 |
PROTON-2121 python-qpid-proton 0.28 BlockingConnection leaks connections (does not close file descriptors) |
| 22 |
""" |
| 23 |
|
| 24 |
from __future__ import absolute_import |
| 25 |
from __future__ import division |
| 26 |
from __future__ import print_function |
| 27 |
|
| 28 |
import contextlib |
| 29 |
import gc |
| 30 |
import os |
| 31 |
import socket |
| 32 |
import subprocess |
| 33 |
import threading |
| 34 |
import unittest |
| 35 |
import warnings |
| 36 |
|
| 37 |
import cproton |
| 38 |
|
| 39 |
import proton.handlers |
| 40 |
import proton.utils |
| 41 |
import proton.reactor |
| 42 |
|
| 43 |
|
| 44 |
def get_fd_set(): |
| 45 |
# type: () -> set[str] |
| 46 |
return set(os.listdir('/proc/self/fd/')) |
| 47 |
|
| 48 |
|
| 49 |
@contextlib.contextmanager |
| 50 |
def no_fd_leaks(test): |
| 51 |
# type: (unittest.TestCase) -> None |
| 52 |
with warnings.catch_warnings(record=True) as ws: |
| 53 |
before = get_fd_set() |
| 54 |
yield |
| 55 |
delta = get_fd_set().difference(before) |
| 56 |
if len(delta) != 0: |
| 57 |
subprocess.check_call("ls -lF /proc/{0}/fd/".format(os.getpid()), shell=True) |
| 58 |
test.fail("Found {0} new fd(s) after the test".format(delta)) |
| 59 |
|
| 60 |
if len(ws) > 0: |
| 61 |
test.fail([w.message for w in ws]) |
| 62 |
|
| 63 |
|
| 64 |
class Broker(proton.handlers.MessagingHandler): |
| 65 |
def __init__(self, acceptor_url): |
| 66 |
# type: (str) -> None |
| 67 |
super(Broker, self).__init__() |
| 68 |
self.acceptor_url = acceptor_url |
| 69 |
|
| 70 |
self.acceptor = None |
| 71 |
self._acceptor_opened_event = threading.Event() |
| 72 |
|
| 73 |
def get_acceptor_sockname(self): |
| 74 |
# type: () -> (str, int) |
| 75 |
self._acceptor_opened_event.wait() |
| 76 |
if hasattr(self.acceptor, '_selectable'): # proton 0.30.0+ |
| 77 |
sockname = self.acceptor._selectable._delegate.getsockname() |
| 78 |
else: # works in proton 0.27.0 |
| 79 |
selectable = cproton.pn_cast_pn_selectable(self.acceptor._impl) |
| 80 |
fd = cproton.pn_selectable_get_fd(selectable) |
| 81 |
s = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM) |
| 82 |
sockname = s.getsockname() |
| 83 |
return sockname[:2] |
| 84 |
|
| 85 |
def on_start(self, event): |
| 86 |
self.acceptor = event.container.listen(self.acceptor_url) |
| 87 |
self._acceptor_opened_event.set() |
| 88 |
|
| 89 |
def on_link_opening(self, event): |
| 90 |
if event.link.is_sender: |
| 91 |
assert not event.link.remote_source.dynamic, "This cannot happen" |
| 92 |
event.link.source.address = event.link.remote_source.address |
| 93 |
elif event.link.remote_target.address: |
| 94 |
event.link.target.address = event.link.remote_target.address |
| 95 |
|
| 96 |
|
| 97 |
@contextlib.contextmanager |
| 98 |
def test_broker(): |
| 99 |
broker = Broker('localhost:0') |
| 100 |
container = proton.reactor.Container(broker) |
| 101 |
threading.Thread(target=container.run).start() |
| 102 |
|
| 103 |
try: |
| 104 |
yield broker |
| 105 |
finally: |
| 106 |
container.stop() |
| 107 |
|
| 108 |
|
| 109 |
PROC_SELF_FD_EXISTS = os.path.exists("/proc/self/fd"), "Skipped: Directory /proc/self/fd does not exist" |
| 110 |
|
| 111 |
|
| 112 |
class BlockingConnectionFDLeakTests(unittest.TestCase): |
| 113 |
@unittest.skipUnless(*PROC_SELF_FD_EXISTS) |
| 114 |
@unittest.expectedFailure |
| 115 |
def test_just_start_stop_test_broker(self): |
| 116 |
with no_fd_leaks(self): |
| 117 |
with test_broker() as broker: |
| 118 |
broker.get_acceptor_sockname() # wait for acceptor to open |
| 119 |
|
| 120 |
gc.collect() |
| 121 |
|
| 122 |
@unittest.skipUnless(*PROC_SELF_FD_EXISTS) |
| 123 |
@unittest.expectedFailure |
| 124 |
def test_connection_close_all(self): |
| 125 |
with no_fd_leaks(self): |
| 126 |
with test_broker() as broker: |
| 127 |
c = proton.utils.BlockingConnection("{0}:{1}".format(*broker.get_acceptor_sockname())) |
| 128 |
c.close() |
| 129 |
|
| 130 |
gc.collect() |
| 131 |
|
| 132 |
@unittest.skipUnless(*PROC_SELF_FD_EXISTS) |
| 133 |
def test_connection_close_all__do_not_check_test_broker(self): |
| 134 |
with test_broker() as broker: |
| 135 |
acceptor_sockname = broker.get_acceptor_sockname() |
| 136 |
with no_fd_leaks(self): |
| 137 |
c = proton.utils.BlockingConnection("{0}:{1}".format(*acceptor_sockname)) |
| 138 |
c.close() |
| 139 |
|
| 140 |
gc.collect() |
| 141 |
|
| 142 |
@unittest.skipUnless(*PROC_SELF_FD_EXISTS) |
| 143 |
@unittest.expectedFailure |
| 144 |
def test_connection_sender_close_all(self): |
| 145 |
with no_fd_leaks(self): |
| 146 |
with test_broker() as broker: |
| 147 |
c = proton.utils.BlockingConnection("{0}:{1}".format(*broker.get_acceptor_sockname())) |
| 148 |
s = c.create_sender("anAddress") |
| 149 |
s.close() |
| 150 |
c.close() |
| 151 |
|
| 152 |
gc.collect() |
| 153 |
|
| 154 |
@unittest.skipUnless(*PROC_SELF_FD_EXISTS) |
| 155 |
@unittest.expectedFailure |
| 156 |
def test_connection_receiver_close_all(self): |
| 157 |
with no_fd_leaks(self): |
| 158 |
with test_broker() as broker: |
| 159 |
c = proton.utils.BlockingConnection("{0}:{1}".format(*broker.get_acceptor_sockname())) |
| 160 |
s = c.create_receiver("anAddress") |
| 161 |
s.close() |
| 162 |
c.close() |
| 163 |
|
| 164 |
gc.collect() |