| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import gc |
| 5 |
import threading |
| 6 |
import unittest |
| 7 |
from test import NativeResourceTest |
| 8 |
from http.server import HTTPServer, SimpleHTTPRequestHandler |
| 9 |
from awscrt.io import ClientBootstrap, DefaultHostResolver, EventLoopGroup |
| 10 |
from awscrt.http import HttpClientConnection, HttpRequest |
| 11 |
import awscrt.exceptions |
| 12 |
|
| 13 |
|
| 14 |
class SilentHandler(SimpleHTTPRequestHandler): |
| 15 |
def log_message(self, format, *args): |
| 16 |
pass |
| 17 |
|
| 18 |
def do_GET(self): |
| 19 |
self.send_response(200, 'OK') |
| 20 |
self.send_header('Content-Length', '5') |
| 21 |
self.end_headers() |
| 22 |
self.wfile.write(b'hello') |
| 23 |
|
| 24 |
|
| 25 |
class TestConnectionLifetime(NativeResourceTest): |
| 26 |
"""Tests for http_connection_binding ref-count based lifetime management. |
| 27 |
|
| 28 |
Under free-threaded Python (Py_GIL_DISABLED), the capsule destructor |
| 29 |
(application thread) and on_connection_shutdown (event-loop thread) can |
| 30 |
race. These tests exercise both orderings and a stress scenario. |
| 31 |
""" |
| 32 |
hostname = 'localhost' |
| 33 |
timeout = 10 |
| 34 |
|
| 35 |
def setUp(self): |
| 36 |
super().setUp() |
| 37 |
self.server = HTTPServer((self.hostname, 0), SilentHandler) |
| 38 |
self.port = self.server.server_address[1] |
| 39 |
self.server_thread = threading.Thread(target=self.server.serve_forever, daemon=True) |
| 40 |
self.server_thread.start() |
| 41 |
|
| 42 |
def tearDown(self): |
| 43 |
self.server.shutdown() |
| 44 |
self.server.server_close() |
| 45 |
self.server_thread.join() |
| 46 |
super().tearDown() |
| 47 |
|
| 48 |
def _new_connection(self): |
| 49 |
event_loop_group = EventLoopGroup() |
| 50 |
host_resolver = DefaultHostResolver(event_loop_group) |
| 51 |
bootstrap = ClientBootstrap(event_loop_group, host_resolver) |
| 52 |
future = HttpClientConnection.new( |
| 53 |
host_name=self.hostname, |
| 54 |
port=self.port, |
| 55 |
bootstrap=bootstrap) |
| 56 |
return future.result(self.timeout) |
| 57 |
|
| 58 |
def test_release_before_shutdown(self): |
| 59 |
"""Capsule destructor fires first, then shutdown callback.""" |
| 60 |
connection = self._new_connection() |
| 61 |
shutdown_future = connection.shutdown_future |
| 62 |
|
| 63 |
del connection |
| 64 |
gc.collect() |
| 65 |
|
| 66 |
shutdown_future.result(self.timeout) |
| 67 |
|
| 68 |
def test_shutdown_before_release(self): |
| 69 |
"""Shutdown callback fires first (via close), then capsule destructor.""" |
| 70 |
connection = self._new_connection() |
| 71 |
shutdown_future = connection.shutdown_future |
| 72 |
|
| 73 |
connection.close() |
| 74 |
shutdown_future.result(self.timeout) |
| 75 |
|
| 76 |
del connection |
| 77 |
gc.collect() |
| 78 |
|
| 79 |
def test_concurrent_release_and_shutdown_stress(self): |
| 80 |
"""Stress: race capsule destructor against shutdown from many threads. |
| 81 |
|
| 82 |
Under Py_GIL_DISABLED, the old two-bool approach would double-free. |
| 83 |
With atomic ref-counting, exactly one path destroys the binding. |
| 84 |
""" |
| 85 |
iterations = 50 |
| 86 |
errors = [] |
| 87 |
|
| 88 |
def release_connection(conn): |
| 89 |
try: |
| 90 |
del conn |
| 91 |
gc.collect() |
| 92 |
except Exception as e: |
| 93 |
errors.append(e) |
| 94 |
|
| 95 |
for i in range(iterations): |
| 96 |
try: |
| 97 |
connection = self._new_connection() |
| 98 |
except awscrt.exceptions.AwsCrtError as e: |
| 99 |
if e.name == 'AWS_IO_SOCKET_CONNECTION_REFUSED': |
| 100 |
continue |
| 101 |
raise |
| 102 |
|
| 103 |
shutdown_future = connection.shutdown_future |
| 104 |
|
| 105 |
connection.close() |
| 106 |
|
| 107 |
t = threading.Thread(target=release_connection, args=(connection,)) |
| 108 |
del connection |
| 109 |
t.start() |
| 110 |
|
| 111 |
shutdown_future.result(self.timeout) |
| 112 |
t.join(self.timeout) |
| 113 |
|
| 114 |
self.assertEqual([], errors) |
| 115 |
|
| 116 |
|
| 117 |
if __name__ == '__main__': |
| 118 |
unittest.main() |