Skip to content

Commit 74340cb

Browse files
committed
wrap real OSError asyncio.TimeoutErrors in a ClientOSError
1 parent e5789af commit 74340cb

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

aiohttp/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,9 @@ async def _request(
531531
raise
532532
except ClientError:
533533
raise
534-
except asyncio.TimeoutError:
535-
raise
536534
except OSError as exc:
535+
if exc.errno is None and isinstance(exc, asyncio.TimeoutError):
536+
raise
537537
raise ClientOSError(*exc.args) from exc
538538

539539
self._cookie_jar.update_cookies(resp.cookies, resp.url)

aiohttp/client_reqrep.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -536,15 +536,16 @@ async def write_bytes(
536536
await writer.write(chunk) # type: ignore[arg-type]
537537

538538
await writer.write_eof()
539-
except asyncio.TimeoutError:
540-
raise
541539
except OSError as exc:
542-
new_exc = ClientOSError(
543-
exc.errno, "Can not write request body for %s" % self.url
544-
)
545-
new_exc.__context__ = exc
546-
new_exc.__cause__ = exc
547-
protocol.set_exception(new_exc)
540+
if exc.errno is None and isinstance(exc, asyncio.TimeoutError):
541+
protocol.set_exception(exc)
542+
else:
543+
new_exc = ClientOSError(
544+
exc.errno, "Can not write request body for %s" % self.url
545+
)
546+
new_exc.__context__ = exc
547+
new_exc.__cause__ = exc
548+
protocol.set_exception(new_exc)
548549
except asyncio.CancelledError as exc:
549550
if not conn.closed:
550551
protocol.set_exception(exc)

aiohttp/connector.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -969,9 +969,9 @@ async def _wrap_create_connection(
969969
raise ClientConnectorCertificateError(req.connection_key, exc) from exc
970970
except ssl_errors as exc:
971971
raise ClientConnectorSSLError(req.connection_key, exc) from exc
972-
except asyncio.TimeoutError:
973-
raise
974972
except OSError as exc:
973+
if exc.errno is None and isinstance(exc, asyncio.TimeoutError):
974+
raise
975975
raise client_error(req.connection_key, exc) from exc
976976

977977
def _warn_about_tls_in_tls(
@@ -1050,9 +1050,9 @@ async def _start_tls_connection(
10501050
raise ClientConnectorCertificateError(req.connection_key, exc) from exc
10511051
except ssl_errors as exc:
10521052
raise ClientConnectorSSLError(req.connection_key, exc) from exc
1053-
except asyncio.TimeoutError:
1054-
raise
10551053
except OSError as exc:
1054+
if exc.errno is None and isinstance(exc, asyncio.TimeoutError):
1055+
raise
10561056
raise client_error(req.connection_key, exc) from exc
10571057
except TypeError as type_err:
10581058
# Example cause looks like this:
@@ -1103,9 +1103,9 @@ def drop_exception(fut: "asyncio.Future[List[Dict[str, Any]]]") -> None:
11031103

11041104
host_resolved.add_done_callback(drop_exception)
11051105
raise
1106-
except asyncio.TimeoutError:
1107-
raise
11081106
except OSError as exc:
1107+
if exc.errno is None and isinstance(exc, asyncio.TimeoutError):
1108+
raise
11091109
# in case of proxy it is not ClientProxyConnectionError
11101110
# it is problem of resolving proxy ip itself
11111111
raise ClientConnectorError(req.connection_key, exc) from exc
@@ -1298,9 +1298,9 @@ async def _create_connection(
12981298
_, proto = await self._loop.create_unix_connection(
12991299
self._factory, self._path
13001300
)
1301-
except asyncio.TimeoutError:
1302-
raise
13031301
except OSError as exc:
1302+
if exc.errno is None and isinstance(exc, asyncio.TimeoutError):
1303+
raise
13041304
raise UnixClientConnectorError(self.path, req.connection_key, exc) from exc
13051305

13061306
return cast(ResponseHandler, proto)
@@ -1365,9 +1365,9 @@ async def _create_connection(
13651365
await asyncio.sleep(0)
13661366
# other option is to manually set transport like
13671367
# `proto.transport = trans`
1368-
except asyncio.TimeoutError:
1369-
raise
13701368
except OSError as exc:
1369+
if exc.errno is None and isinstance(exc, asyncio.TimeoutError):
1370+
raise
13711371
raise ClientConnectorError(req.connection_key, exc) from exc
13721372

13731373
return cast(ResponseHandler, proto)

0 commit comments

Comments
 (0)