diff --git a/aiohttp/client.py b/aiohttp/client.py index 05ef05bb368..a97190d078a 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -531,9 +531,9 @@ async def _request( raise except ClientError: raise - except asyncio.TimeoutError: - raise except OSError as exc: + if exc.errno is None and isinstance(exc, asyncio.TimeoutError): + raise raise ClientOSError(*exc.args) from exc self._cookie_jar.update_cookies(resp.cookies, resp.url) diff --git a/aiohttp/client_reqrep.py b/aiohttp/client_reqrep.py index 689d4b7563f..9478e8d36c5 100644 --- a/aiohttp/client_reqrep.py +++ b/aiohttp/client_reqrep.py @@ -536,15 +536,16 @@ async def write_bytes( await writer.write(chunk) # type: ignore[arg-type] await writer.write_eof() - except asyncio.TimeoutError: - raise except OSError as exc: - new_exc = ClientOSError( - exc.errno, "Can not write request body for %s" % self.url - ) - new_exc.__context__ = exc - new_exc.__cause__ = exc - protocol.set_exception(new_exc) + if exc.errno is None and isinstance(exc, asyncio.TimeoutError): + protocol.set_exception(exc) + else: + new_exc = ClientOSError( + exc.errno, "Can not write request body for %s" % self.url + ) + new_exc.__context__ = exc + new_exc.__cause__ = exc + protocol.set_exception(new_exc) except asyncio.CancelledError as exc: if not conn.closed: protocol.set_exception(exc) diff --git a/aiohttp/connector.py b/aiohttp/connector.py index cdbc7c3261b..7dbbf4f2d80 100644 --- a/aiohttp/connector.py +++ b/aiohttp/connector.py @@ -969,9 +969,9 @@ async def _wrap_create_connection( raise ClientConnectorCertificateError(req.connection_key, exc) from exc except ssl_errors as exc: raise ClientConnectorSSLError(req.connection_key, exc) from exc - except asyncio.TimeoutError: - raise except OSError as exc: + if exc.errno is None and isinstance(exc, asyncio.TimeoutError): + raise raise client_error(req.connection_key, exc) from exc def _warn_about_tls_in_tls( @@ -1050,9 +1050,9 @@ async def _start_tls_connection( raise ClientConnectorCertificateError(req.connection_key, exc) from exc except ssl_errors as exc: raise ClientConnectorSSLError(req.connection_key, exc) from exc - except asyncio.TimeoutError: - raise except OSError as exc: + if exc.errno is None and isinstance(exc, asyncio.TimeoutError): + raise raise client_error(req.connection_key, exc) from exc except TypeError as type_err: # Example cause looks like this: @@ -1103,9 +1103,9 @@ def drop_exception(fut: "asyncio.Future[List[Dict[str, Any]]]") -> None: host_resolved.add_done_callback(drop_exception) raise - except asyncio.TimeoutError: - raise except OSError as exc: + if exc.errno is None and isinstance(exc, asyncio.TimeoutError): + raise # in case of proxy it is not ClientProxyConnectionError # it is problem of resolving proxy ip itself raise ClientConnectorError(req.connection_key, exc) from exc @@ -1298,9 +1298,9 @@ async def _create_connection( _, proto = await self._loop.create_unix_connection( self._factory, self._path ) - except asyncio.TimeoutError: - raise except OSError as exc: + if exc.errno is None and isinstance(exc, asyncio.TimeoutError): + raise raise UnixClientConnectorError(self.path, req.connection_key, exc) from exc return cast(ResponseHandler, proto) @@ -1365,9 +1365,9 @@ async def _create_connection( await asyncio.sleep(0) # other option is to manually set transport like # `proto.transport = trans` - except asyncio.TimeoutError: - raise except OSError as exc: + if exc.errno is None and isinstance(exc, asyncio.TimeoutError): + raise raise ClientConnectorError(req.connection_key, exc) from exc return cast(ResponseHandler, proto)