asyncio: SSL transport raises AttributeError after close() in connection_lost()

Geschlossen
#160,964 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
25/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Veraltet
Tech-Stack
python
Bereich
networking

Rechercherichtung

Start by locating _SSLProtocolTransport.close() and the asyncio SSL transport tests; the issue identifies that method as the source and includes a reproduction. Check the behavior of write(), set_protocol(), get_write_buffer_size(), get_extra_info() and the sendfile() fallback after close; done means these paths no longer raise the reported AttributeError and the fallback reports the expected connection error.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

stdlib topic-asyncio type-bug

Bug report

If a protocol closes its SSL transport in connection_lost(), which is a common pattern, later calls on the transport fail:

import asyncio
from test.test_asyncio import utils as tu

class Proto(asyncio.Protocol):
    def connection_made(self, tr):
        self.tr = tr
    def connection_lost(self, exc):
        self.tr.close()
        self.lost.set_result(None)

async def main():
    async def handler(reader, writer):
        writer.close()
    srv = await asyncio.start_server(handler, '127.0.0.1', 0,
                                     ssl=tu.simple_server_sslcontext())
    port = srv.sockets[0].getsockname()[1]
    loop = asyncio.get_running_loop()
    proto = Proto()
    proto.lost = loop.create_future()
    tr, _ = await loop.create_connection(
        lambda: proto, '127.0.0.1', port, ssl=tu.simple_client_sslcontext())
    await proto.lost
    tr.write(b'x')  # AttributeError: 'NoneType' object has no attribute '_write_appdata'
    srv.close()

asyncio.run(main())

set_protocol(), get_write_buffer_size() and get_extra_info() fail the same way. Plain TCP transports don't have this problem.

It also breaks the loop.sendfile() fallback over SSL: if the peer disconnects mid-transfer, restore() calls set_protocol() and raises AttributeError instead of the expected connection error.

The cause is in _SSLProtocolTransport.close(). When it's called on a transport already marked closed, it sets _ssl_protocol = None. That was added in gh-98539 so the transport doesn't keep the SSL context alive, but it leaves the transport unusable.

Affects 3.11 and later.

Linked PRs
  • gh-160965
Vorherrschende Sprache
Python
Sterne
77.8k
Forks
37.7k
Ø Merge
17 Std. 24 Min.
Gemergte PRs (30 T.)
716

Entwicklungsumgebung

In Codespaces öffnen

Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus python/cpython

Alle Issues in python/cpython

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

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