| 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-2116 Memory leak in python client |
| 22 |
PROTON-2192 Memory leak in Python client on Windows |
| 23 |
""" |
| 24 |
|
| 25 |
from __future__ import absolute_import |
| 26 |
from __future__ import division |
| 27 |
from __future__ import print_function |
| 28 |
|
| 29 |
import gc |
| 30 |
import logging |
| 31 |
import os |
| 32 |
import platform |
| 33 |
import subprocess |
| 34 |
import sys |
| 35 |
import threading |
| 36 |
import time |
| 37 |
import unittest |
| 38 |
import uuid |
| 39 |
|
| 40 |
import proton.handlers |
| 41 |
import proton.reactor |
| 42 |
import proton.utils |
| 43 |
|
| 44 |
|
| 45 |
logger = logging.getLogger(__name__) |
| 46 |
|
| 47 |
|
| 48 |
class ReconnectingTestClient: |
| 49 |
def __init__(self, hostport): |
| 50 |
# type: (str) -> None |
| 51 |
self.hostport = hostport |
| 52 |
|
| 53 |
self.object_counts = [] |
| 54 |
self.done = threading.Event() |
| 55 |
|
| 56 |
def count_objects(self, message): |
| 57 |
# type: (str) -> None |
| 58 |
gc.collect() |
| 59 |
n = len(gc.get_objects()) |
| 60 |
if message == "loop": |
| 61 |
self.object_counts.append(n) |
| 62 |
logger.debug("Message %s, Count %d", message, n) |
| 63 |
|
| 64 |
def run(self): |
| 65 |
ADDR = "testing123" |
| 66 |
HEARTBEAT = 5 |
| 67 |
SLEEP = 5 |
| 68 |
|
| 69 |
recv = None |
| 70 |
conn = None |
| 71 |
for _ in range(3): |
| 72 |
subscribed = False |
| 73 |
while not subscribed: |
| 74 |
try: |
| 75 |
conn = proton.utils.BlockingConnection(self.hostport, ssl_domain=None, heartbeat=HEARTBEAT) |
| 76 |
recv = conn.create_receiver(ADDR, name=str(uuid.uuid4()), dynamic=False, options=None) |
| 77 |
subscribed = True |
| 78 |
except Exception as e: |
| 79 |
logger.info("received exception %s on connect/subscribe, retry", e) |
| 80 |
time.sleep(0.5) |
| 81 |
|
| 82 |
self.count_objects("loop") |
| 83 |
logger.debug("connected") |
| 84 |
while subscribed: |
| 85 |
try: |
| 86 |
recv.receive(SLEEP) |
| 87 |
except proton.Timeout: |
| 88 |
pass |
| 89 |
except Exception as e: |
| 90 |
logger.info(e) |
| 91 |
try: |
| 92 |
recv.close() |
| 93 |
recv = None |
| 94 |
except: |
| 95 |
self.count_objects("link close() failed") |
| 96 |
pass |
| 97 |
try: |
| 98 |
conn.close() |
| 99 |
conn = None |
| 100 |
self.count_objects("conn closed") |
| 101 |
except: |
| 102 |
self.count_objects("conn close() failed") |
| 103 |
pass |
| 104 |
subscribed = False |
| 105 |
self.done.set() |
| 106 |
|
| 107 |
|
| 108 |
class Proton2116Test(unittest.TestCase): |
| 109 |
@unittest.skipIf(platform.system() == 'Windows', "PROTON-2192: The issue is not resolved on Windows") |
| 110 |
def test_blocking_connection_object_leak(self): |
| 111 |
"""Kills and restarts broker repeatedly, while client is reconnecting. |
| 112 |
|
| 113 |
The value of `gc.get_objects()` should not keep increasing in the client. |
| 114 |
|
| 115 |
These are the automated reproduction steps for PROTON-2116""" |
| 116 |
gc.collect() |
| 117 |
|
| 118 |
thread = None |
| 119 |
client = None |
| 120 |
|
| 121 |
host_port = "" # random on first broker startup |
| 122 |
broker_process = None |
| 123 |
|
| 124 |
while not client or not client.done.is_set(): |
| 125 |
try: |
| 126 |
params = [] |
| 127 |
if host_port: |
| 128 |
params = ['-b', host_port] |
| 129 |
cwd = os.path.dirname(__file__) |
| 130 |
broker_process = subprocess.Popen( |
| 131 |
args=[sys.executable, |
| 132 |
os.path.join(cwd, 'broker_PROTON_2116_blocking_connection_object_leak.py')] + params, |
| 133 |
stdout=subprocess.PIPE, |
| 134 |
universal_newlines=True, |
| 135 |
) |
| 136 |
host_port = broker_process.stdout.readline() |
| 137 |
|
| 138 |
if not client: |
| 139 |
client = ReconnectingTestClient(host_port) |
| 140 |
thread = threading.Thread(target=client.run) |
| 141 |
thread.start() |
| 142 |
|
| 143 |
time.sleep(3) |
| 144 |
finally: |
| 145 |
if broker_process: |
| 146 |
broker_process.kill() |
| 147 |
broker_process.wait() |
| 148 |
broker_process.stdout.close() |
| 149 |
time.sleep(0.3) |
| 150 |
|
| 151 |
thread.join() |
| 152 |
|
| 153 |
logger.info("client.object_counts:", client.object_counts) |
| 154 |
|
| 155 |
# drop first value, it is usually different (before counts settle) |
| 156 |
object_counts = client.object_counts[1:] |
| 157 |
|
| 158 |
diffs = [c - object_counts[0] for c in object_counts] |
| 159 |
self.assertEqual([0] * 2, diffs, "Object counts should not be increasing") |
| 160 |
|
| 161 |
|
| 162 |
if __name__ == '__main__': |
| 163 |
unittest.main() |