File tree 2 files changed +31
-1
lines changed
2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -1143,7 +1143,10 @@ async def _wrap_create_connection(
1143
1143
# Will be hit if an exception is thrown before the event loop takes the socket.
1144
1144
# In that case, proactively close the socket to guard against event loop leaks.
1145
1145
# For example, see https://github.com/MagicStack/uvloop/issues/653.
1146
- sock .close ()
1146
+ try :
1147
+ sock .close ()
1148
+ except OSError as exc :
1149
+ raise client_error (req .connection_key , exc ) from exc
1147
1150
1148
1151
def _warn_about_tls_in_tls (
1149
1152
self ,
Original file line number Diff line number Diff line change @@ -669,6 +669,33 @@ async def test_tcp_connector_closes_socket_on_error(
669
669
await conn .close ()
670
670
671
671
672
+ async def test_tcp_connector_closes_socket_on_error_results_in_another_error (
673
+ loop : asyncio .AbstractEventLoop , start_connection : mock .AsyncMock
674
+ ) -> None :
675
+ """Test that when error occurs while closing the socket."""
676
+ req = ClientRequest ("GET" , URL ("https://127.0.0.1:443" ), loop = loop )
677
+ start_connection .return_value .close .side_effect = OSError (
678
+ 1 , "error from closing socket"
679
+ )
680
+
681
+ conn = aiohttp .TCPConnector ()
682
+ with (
683
+ mock .patch .object (
684
+ conn ._loop ,
685
+ "create_connection" ,
686
+ autospec = True ,
687
+ spec_set = True ,
688
+ side_effect = ValueError ,
689
+ ),
690
+ pytest .raises (aiohttp .ClientConnectionError , match = "error from closing socket" ),
691
+ ):
692
+ await conn .connect (req , [], ClientTimeout ())
693
+
694
+ assert start_connection .return_value .close .called
695
+
696
+ await conn .close ()
697
+
698
+
672
699
async def test_tcp_connector_server_hostname_default (
673
700
loop : asyncio .AbstractEventLoop , start_connection : mock .AsyncMock
674
701
) -> None :
You can’t perform that action at this time.
0 commit comments