Uncaught exception when connector is cancelled

Bug #1969817 reported by Olivier Gayot
10
This bug affects 1 person
Affects Status Importance Assigned to Milestone
python-aiohttp (Ubuntu)
Fix Released
Undecided
Unassigned
Focal
Fix Released
Undecided
Unassigned

Bug Description

[Impact]

 * When cancelling a connector task, the associated resolver task (if not finished) is not cancelled and continues running.

Unfortunately, if the resolver task eventually raises an exception (e.g., socket.gaierror), the exception will go directly to the exception handler because none will be awaiting the task anymore.

This results in applications crashing with exceptions such as:

Task exception was never retrieved
future: <Task finished name='Task-3' coro=<TCPConnector._resolve_host() done, defined at /usr/lib/python3/dist-packages/aiohttp/connector.py:774> exception=gaierror(-2, 'Name or service not known')>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/aiohttp/connector.py", line 829, in _resolve_host
    addrs = await \
  File "/usr/lib/python3/dist-packages/aiohttp/resolver.py", line 29, in resolve
    infos = await self._loop.getaddrinfo(
  File "/usr/lib/python3.8/asyncio/base_events.py", line 825, in getaddrinfo
    return await self.run_in_executor(
  File "/usr/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

 * This aiohttp bug is the root cause of a crash in Subiquity: https://bugs.launchpad.net/ubuntu-power-systems/+bug/1969393
Currently, we build the Subiquity snap based on deb packages (including python3-aiohttp) from focal. We temporarily moved from python3-aiohttp to python3-requests in Subiquity to work around this crash.
 * Other applications based on python3-aiohttp can be affected and crash despite the absence of other bug reports.

 * The patch in the debdiff makes sure that resolver tasks end up being awaited when the associated connector tasks gets cancelled.

[Test Plan]

 * The following chunk of code can be executed to reproduce the issue: https://paste.ubuntu.com/p/XrnfqVHtBh/ (it takes ~60 seconds to execute).
 * The bug is way easier to reproduce is there is some delay in the DNS resolution. One way to simulate some delay is to use a non-local DNS server and run:

# tc qdisc add dev eth0 root netem delay 200ms

where eth0 is the name of the interface used for DNS resolutions.
This can be reverted using:
# tc qdisc del dev eth0 root netem

    * On python3-aiohttp 3.6 (focal), the exception handler wakes up with:

    Task exception was never retrieved
    future: <Task finished name='Task-3' coro=<TCPConnector._resolve_host() done, defined at /usr/lib/python3/dist-packages/aiohttp/connector.py:774> exception=gaierror(-2, 'Name or service not known')>

    * When aiohttp is patched, nothing should happen.

 * Manually testing the patched library against Subiquity to make sure it solves https://bugs.launchpad.net/ubuntu-power-systems/+bug/1969393.
This was done on my end using this PPA: https://launchpad.net/~ogayot/+archive/ubuntu/focal-bugfix . Tests were green.

[Where problems could occur]

 * Since the patch affects a python library, any application that depends on this library (aka. python3-aiohttp) on focal would be affected by the upload.

 * In the unlikely event that this patch introduces a regression, applications that depend on python3-aiohttp (i.e., in focal/universe) can crash or raise exceptions.

 * If any package in focal/main has python3-aiohttp as a Build-Depends, a regression could cause said package to FTBFS.

[Other Info]

 * The debdiff brings a backport of an upstream patch that is present in aiohttp 3.7 and newer versions:

https://github.com/aio-libs/aiohttp/pull/5050

 * Upstream bug report: https://github.com/aio-libs/aiohttp/issues/4330

Revision history for this message
Olivier Gayot (ogayot) wrote :

Adding debdiff

Olivier Gayot (ogayot)
Changed in python-aiohttp (Ubuntu):
status: New → Incomplete
Olivier Gayot (ogayot)
description: updated
description: updated
Changed in python-aiohttp (Ubuntu):
status: Incomplete → New
Olivier Gayot (ogayot)
Changed in python-aiohttp (Ubuntu):
status: New → Confirmed
Olivier Gayot (ogayot)
tags: added: fr-2268
Olivier Gayot (ogayot)
description: updated
Revision history for this message
Gunnar Hjalmarsson (gunnarhj) wrote :

Sponsored to focal.

Changed in python-aiohttp (Ubuntu):
status: Confirmed → Fix Released
Changed in python-aiohttp (Ubuntu Focal):
status: New → In Progress
Revision history for this message
Brian Murray (brian-murray) wrote : Please test proposed package

Hello Olivier, or anyone else affected,

Accepted python-aiohttp into focal-proposed. The package will build now and be available at https://launchpad.net/ubuntu/+source/python-aiohttp/3.6.2-1ubuntu1 in a few hours, and then in the -proposed repository.

Please help us by testing this new package. See https://wiki.ubuntu.com/Testing/EnableProposed for documentation on how to enable and use -proposed. Your feedback will aid us getting this update out to other Ubuntu users.

If this package fixes the bug for you, please add a comment to this bug, mentioning the version of the package you tested, what testing has been performed on the package and change the tag from verification-needed-focal to verification-done-focal. If it does not fix the bug for you, please add a comment stating that, and change the tag to verification-failed-focal. In either case, without details of your testing we will not be able to proceed.

Further information regarding the verification process can be found at https://wiki.ubuntu.com/QATeam/PerformingSRUVerification . Thank you in advance for helping!

N.B. The updated package will be released to -updates after the bug(s) fixed by this package have been verified and the package has been in -proposed for a minimum of 7 days.

Changed in python-aiohttp (Ubuntu Focal):
status: In Progress → Fix Committed
tags: added: verification-needed verification-needed-focal
Olivier Gayot (ogayot)
description: updated
description: updated
Revision history for this message
Olivier Gayot (ogayot) wrote :

Tested on a lxd container configured with 8.8.8.8 as the nameserver and 200ms of network latency added with tc.

-----------------------------------------
Before upgrading to version in -proposed:
-----------------------------------------
$ dpkg-query --show python3-aiohttp
python3-aiohttp 3.6.2-1build1

$ python3 file.py
Task exception was never retrieved
future: <Task finished name='Task-3' coro=<TCPConnector._resolve_host() done, defined at /usr/lib/python3/dist-packages/aiohttp/connector.py:774> exception=gaierror(-2, 'Name or service not known')>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/aiohttp/connector.py", line 829, in _resolve_host
    addrs = await \
  File "/usr/lib/python3/dist-packages/aiohttp/resolver.py", line 29, in resolve
    infos = await self._loop.getaddrinfo(
  File "/usr/lib/python3.8/asyncio/base_events.py", line 825, in getaddrinfo
    return await self.run_in_executor(
  File "/usr/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

----------------------------------------
After upgrading to version in -proposed:
-----------------------------------------
$ dpkg-query --show python3-aiohttp
python3-aiohttp 3.6.2-1ubuntu1

$ python3 file.py
$

Also tested using a subiquity ISO based on the 22.04.1 snap version.
No crash when using the python3-aiohttp from proposed.

tags: added: verification-done-focal
removed: verification-needed-focal
Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package python-aiohttp - 3.6.2-1ubuntu1

---------------
python-aiohttp (3.6.2-1ubuntu1) focal; urgency=medium

  * Backport upstream patch to fix resolver tasks not awaited on cancellation
    (LP: #1969817)

 -- Olivier Gayot <email address hidden> Thu, 22 Apr 2022 18:25:22 +0200

Changed in python-aiohttp (Ubuntu Focal):
status: Fix Committed → Fix Released
Revision history for this message
Brian Murray (brian-murray) wrote : Update Released

The verification of the Stable Release Update for python-aiohttp has completed successfully and the package is now being released to -updates. Subsequently, the Ubuntu Stable Release Updates Team is being unsubscribed and will not receive messages about this bug report. In the event that you encounter a regression using the package from -updates please report a new bug using ubuntu-bug and tag the bug report regression-update so we can easily find any regressions.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.