Skip to content

Commit 8e32e60

Browse files
Raise exception for shutdown on a connection already released to the pool (#3601)
Co-authored-by: Quentin Pradet <[email protected]>
1 parent 9996e0f commit 8e32e60

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

changelog/3581.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Raise exception for HTTPResponse.shutdown on a connection already released to the pool.

src/urllib3/response.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,10 @@ def readable(self) -> bool:
11001100
def shutdown(self) -> None:
11011101
if not self._sock_shutdown:
11021102
raise ValueError("Cannot shutdown socket as self._sock_shutdown is not set")
1103+
if self._connection is None:
1104+
raise RuntimeError(
1105+
"Cannot shutdown as connection has already been released to the pool"
1106+
)
11031107
self._sock_shutdown(socket.SHUT_RD)
11041108

11051109
def close(self) -> None:

test/with_dummyserver/test_connectionpool.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,18 @@ def test_cleanup_on_connection_error(self) -> None:
912912
# the pool should still contain poolsize elements
913913
assert http.pool.qsize() == http.pool.maxsize
914914

915+
def test_shutdown_on_connection_released_to_pool(self) -> None:
916+
with HTTPConnectionPool(self.host, self.port) as pool:
917+
resp = pool.urlopen("GET", "/", preload_content=False)
918+
resp.drain_conn()
919+
resp.release_conn()
920+
921+
with pytest.raises(
922+
RuntimeError,
923+
match="Cannot shutdown as connection has already been released to the pool",
924+
):
925+
resp.shutdown()
926+
915927
def test_mixed_case_hostname(self) -> None:
916928
with HTTPConnectionPool("LoCaLhOsT", self.port) as pool:
917929
response = pool.request("GET", f"http://LoCaLhOsT:{self.port}/")

0 commit comments

Comments
 (0)