MagicStack / MagicStack/uvloop

signal handler sockets error on close leaving loop in 'running' state indefinitely

Offen
#625 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Cython
Sterne
11.9k
Forks
616
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

  • uvloop version: 0.19.0 and 0.20.0
  • Python version: 3.12
  • Platform: linux/aws lambda
  • Can you reproduce the bug with PYTHONASYNCIODEBUG in env?:
  • Does uvloop behave differently from vanilla asyncio? How?: have observed the same error on asyncio

I have not been able to come up with a reproduction for this as I think it has something to do with how aws lambda freezes and thaws lambdas.

I have an aws lambda using uvloop that handles requests with using loop.run_until_complete. I can't use uvloop.run or aysncio.run since I need client connected to that loop to still be present on the next invocation of the lambda. Occassionally we have observed errors on the lambda with the following stack trace:

[ERROR] OSError: [Errno 9] Bad file descriptor
Traceback (most recent call last):
  File "/my_code.py", line 302, in request_handler
    self.loop.run_until_complete(_process_records())
  File "uvloop/loop.pyx", line 1511, in uvloop.loop.Loop.run_until_complete
  File "uvloop/loop.pyx", line 1504, in uvloop.loop.Loop.run_until_complete
  File "uvloop/loop.pyx", line 1377, in uvloop.loop.Loop.run_forever
  File "uvloop/loop.pyx", line 547, in uvloop.loop.Loop._run
  File "uvloop/loop.pyx", line 348, in uvloop.loop.Loop._pause_signals
  File "/root/.pyenv/versions/3.12.1/lib/python3.12/socket.py", line 504, in close
    self._real_close()
  File "/root/.pyenv/versions/3.12.1/lib/python3.12/socket.py", line 498, in _real_close
    _ss.close(self)

Because this error happens before the loop sets it's self._running field to 0 this means on the next invocation of the lambda the call to loop.run_until_complete errors with a loop is already running error.

I think the bad file descriptor is caused by some feature of the lambda environment or it could just be some kind of random chance since we have a lot of these running and this happens on very small fraction (<0.01%).

I wrote this patch to catch and ignore the Bad file descriptor error, but don't know if that's the right thing to do in the absence of real test case. If anyone has ideas about how I could produce a test case I am happy to try them out.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Lies zunächst in uvloop/loop.pyx den Bereich um Loop._pause_signals und Loop.run_until_complete, und prüfe dann den verknüpften catch-exception-on-pause-Patch. Reproduziere den bad-file-descriptor-Pfad, einschließlich wiederholter Aufrufe von run_until_complete, und füge einen Regressionstest hinzu; fertig ist es, wenn die Schleife nach dem Fehler nicht weiterhin als laufend markiert ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
backend, networking
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
20/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.