Closed
Description
Describe the bug
When an exception is thrown during creation of the server app (for example the port is already in use), an exception error message is always printed.
This doesn't happen in asyncio itself, but on line 451 in web.py and in what hides behind it.
This exception is also raised, and so I could handle it silently (or not, and have its error message printed -- arguably with too much traceback) which is how it should be.
Very similar to issue #3497
To Reproduce
You can run the following MWEs.
async def async_error():
raise EOFError()
import asyncio
try:
asyncio.get_event_loop().run_until_complete(async_error())
except EOFError:
print("----- CAUGHT EXCEPTION NORMALLY -----")
from aiohttp import web
try:
web.run_app(async_error())
except EOFError:
print("----- CAUGHT EXCEPTION, BUT SEE ABOVE -----")
from aiohttp import web
try:
# run this example twice. It will throw an OS Error because of the blocked port 8080
web.run_app(web.Application())
except OSError:
print("----- CAUGHT EXCEPTION, BUT SEE ABOVE -----")
Expected behavior
I want to catch exceptions during startup silently:
python test.py
----- CAUGHT EXCEPTION NORMALLY -----
----- CAUGHT EXCEPTION, BUT SEE ABOVE -----
----- CAUGHT EXCEPTION, BUT SEE ABOVE -----
Logs/tracebacks
python test.py
----- CAUGHT EXCEPTION NORMALLY -----
unhandled exception during asyncio.run() shutdown
task: <Task finished name='Task-2' coro=<_run_app() done, defined at C:\Python38\lib\site-packages\aiohttp\web.py:287> exception=EOFError()>
Traceback (most recent call last):
File "C:\Python38\lib\site-packages\aiohttp\web.py", line 514, in run_app
loop.run_until_complete(main_task)
File "C:\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "C:\Python38\lib\site-packages\aiohttp\web.py", line 308, in _run_app
app = await app # type: ignore[misc]
File "test.py", line 5, in async_error
raise EOFError()
EOFError
----- CAUGHT EXCEPTION, BUT SEE ABOVE -----
unhandled exception during asyncio.run() shutdown
task: <Task finished name='Task-4' coro=<_run_app() done, defined at C:\Python38\lib\site-packages\aiohttp\web.py:287> exception=OSError(10048, "error while attempting to bind on address ('::', 8080, 0, 0): normalerweise darf jede socketadresse (protokoll, netzwerkadresse oder anschluss) nur jeweils einmal verwendet werden")>
Traceback (most recent call last):
File "C:\Python38\lib\site-packages\aiohttp\web.py", line 514, in run_app
loop.run_until_complete(main_task)
File "C:\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "C:\Python38\lib\site-packages\aiohttp\web.py", line 413, in _run_app
await site.start()
File "C:\Python38\lib\site-packages\aiohttp\web_runner.py", line 121, in start
self._server = await loop.create_server(
File "C:\Python38\lib\asyncio\base_events.py", line 1463, in create_server
raise OSError(err.errno, 'error while attempting '
OSError: [Errno 10048] error while attempting to bind on address ('::', 8080, 0, 0): normalerweise darf jede socketadresse (protokoll, netzwerkadresse oder anschluss) nur jeweils einmal verwendet werden
----- CAUGHT EXCEPTION, BUT SEE ABOVE -----Python Version
Python 3.8.2aiohttp Version
3.8.1multidict Version
6.0.2yarl Version
1.7.2OS
Windows 10 21H2
Related component
Server
Additional context
No response
Code of Conduct
- I agree to follow the aio-libs Code of Conduct