Skip to content

Commit

Permalink
🐛 Fix incompatible method signature HTTPConnectionPool._new_conn (#123)
Browse files Browse the repository at this point in the history
Close #122
  • Loading branch information
Ousret authored May 31, 2024
1 parent 7750ac3 commit 6f043de
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2.7.913 (2024-05-31)
====================

- Relaxed constraints around ``HTTPConnectionPool._new_conn`` private method in order to ensure a broader compatibility. (#122)

2.7.912 (2024-05-27)
====================

Expand Down
11 changes: 10 additions & 1 deletion src/urllib3/_async/connectionpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,16 @@ async def _get_conn(
log.debug("Resetting dropped connection: %s", self.host)
await conn.close()

return conn or await self._new_conn(heb_timeout=heb_timeout)
try:
return conn or await self._new_conn(heb_timeout=heb_timeout)
except (
TypeError
): # this branch catch overridden pool that don't support Happy-Eyeballs!
conn = await self._new_conn()
# as this branch is meant for people bypassing our main logic, we have to memorize the conn immediately
# into our pool of conn.
await self.pool.put(conn, immediately_unavailable=True)
return conn

async def _put_conn(self, conn: AsyncHTTPConnection) -> None:
"""
Expand Down
2 changes: 1 addition & 1 deletion src/urllib3/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file is protected via CODEOWNERS
from __future__ import annotations

__version__ = "2.7.912"
__version__ = "2.7.913"
4 changes: 4 additions & 0 deletions src/urllib3/backend/_async/hface.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,10 @@ async def endheaders( # type: ignore[override]
if self.sock is None:
await self.connect() # type: ignore[attr-defined]

# some libraries override connect(), thus bypassing our state machine initialization.
if self._protocol is None:
await self._post_conn()

assert self.sock is not None and self._protocol is not None

# only h2 and h3 support streams, it is faked/simulated for h1.
Expand Down
4 changes: 4 additions & 0 deletions src/urllib3/backend/hface.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,10 @@ def endheaders(
if self.sock is None:
self.connect() # type: ignore[attr-defined]

# some libraries override connect(), thus bypassing our state machine initialization.
if self._protocol is None:
self._post_conn()

assert self.sock is not None and self._protocol is not None

# only h2 and h3 support streams, it is faked/simulated for h1.
Expand Down
11 changes: 10 additions & 1 deletion src/urllib3/connectionpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,16 @@ def _get_conn(
log.debug("Resetting dropped connection: %s", self.host)
conn.close()

return conn or self._new_conn(heb_timeout=heb_timeout)
try:
return conn or self._new_conn(heb_timeout=heb_timeout)
except (
TypeError
): # this branch catch overridden pool that don't support Happy-Eyeballs!
conn = self._new_conn()
# as this branch is meant for people bypassing our main logic, we have to memorize the conn immediately
# into our pool of conn.
self.pool.put(conn, immediately_unavailable=True)
return conn

def _put_conn(self, conn: HTTPConnection) -> None:
"""
Expand Down

0 comments on commit 6f043de

Please sign in to comment.