Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 52bd241

Browse files
committed
Switch back to synchronous close
There are arguments either way, but the arguments for async close are largely aesthetic, and the arguments against are that it creates some tricky problems with exception handling, and extra churn in urllib3 interfaces. For more details see: https://github.com/njsmith/urllib3/pull/4#issuecomment-354729966
1 parent 75784e0 commit 52bd241

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

urllib3/backends/trio_backend.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,23 @@ async def receiver():
6565
except LoopAbort:
6666
pass
6767

68-
async def forceful_close(self):
69-
await trio.aclose_forcefully(self._stream)
68+
# Pull out the underlying trio socket, because it turns out HTTP is not so
69+
# great at respecting abstraction boundaries.
70+
def _socket(self):
71+
stream = self._stream
72+
# Strip off any layers of SSLStream
73+
while hasattr(stream, "transport_stream"):
74+
stream = stream.transport_stream
75+
# Now we have a SocketStream
76+
return stream.socket
77+
78+
# We want this to be synchronous, and don't care about graceful teardown
79+
# of the SSL/TLS layer.
80+
def forceful_close(self):
81+
self._socket().close()
7082

7183
def is_readable(self):
72-
# This is a bit of a hack, but I can't think of a better API that trio
73-
# *could* provide, since what we want to check here is such an odd
74-
# thing.
75-
sock_stream = self._stream
76-
# Strip off SSLStream wrappings
77-
while hasattr(sock_stream, "transport_stream"):
78-
sock_stream = sock_stream.transport_stream
79-
sock = sock_stream.socket
80-
return is_readable(sock)
84+
return is_readable(self._socket())
8185

8286
def set_readable_watch_state(self, enabled):
8387
pass

urllib3/backends/twisted_backend.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ def receive_loop_allback(result):
245245
else:
246246
raise DoubleError(*failures)
247247

248+
def forceful_close(self):
249+
self._protocol.transport.abortConnection()
250+
248251
def is_readable(self):
249252
return self._protocol.is_readable()
250253

urllib3/connectionpool.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
123123
# Return False to re-raise any potential exceptions
124124
return False
125125

126-
async def close(self):
126+
def close(self):
127127
"""
128128
Close all pooled connections and disable the pool.
129129
"""
@@ -277,7 +277,7 @@ async def _get_conn(self, timeout=None):
277277
# If this is a persistent connection, check if it got disconnected
278278
if conn and is_connection_dropped(conn):
279279
log.debug("Resetting dropped connection: %s", self.host)
280-
await conn.close()
280+
conn.close()
281281

282282
return conn or self._new_conn()
283283

@@ -309,7 +309,7 @@ async def _put_conn(self, conn):
309309

310310
# Connection never got put back into the pool, close it.
311311
if conn:
312-
await conn.close()
312+
conn.close()
313313

314314
async def _start_conn(self, conn, connect_timeout):
315315
"""
@@ -421,7 +421,7 @@ async def _make_request(
421421
def _absolute_url(self, path):
422422
return Url(scheme=self.scheme, host=self.host, port=self.port, path=path).url
423423

424-
async def close(self):
424+
def close(self):
425425
"""
426426
Close all pooled connections and disable the pool.
427427
"""
@@ -432,7 +432,7 @@ async def close(self):
432432
while True:
433433
conn = old_pool.get(block=False)
434434
if conn:
435-
await conn.close()
435+
conn.close()
436436

437437
except queue.Empty:
438438
pass # Done.
@@ -618,7 +618,7 @@ async def urlopen(self, method, url, body=None, headers=None, retries=None,
618618
# to throw the connection away unless explicitly told not to.
619619
# Close the connection, set the variable to None, and make sure
620620
# we put the None back in the pool to avoid leaking it.
621-
conn = conn and await conn.close()
621+
conn = conn and conn.close()
622622
release_this_conn = True
623623

624624
if release_this_conn:

urllib3/sync_connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,14 +430,14 @@ async def connect(self, ssl_context=None,
430430
# XX We should pick one of these names and use it consistently...
431431
self._sock = conn
432432

433-
async def close(self):
433+
def close(self):
434434
"""
435435
Close this connection.
436436
"""
437437
if self._sock is not None:
438438
# Make sure self._sock is None even if closing raises an exception
439439
sock, self._sock = self._sock, None
440-
await sock.forceful_close()
440+
sock.forceful_close()
441441

442442
def is_dropped(self):
443443
"""

0 commit comments

Comments
 (0)