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

Fechada
#160,964 1 comentário 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
4/5
Tempo estimado
3-5 dias
Facilidade para iniciantes
25/100
Tipo de issue
Bug
Clareza
Razoavelmente clara
Status de atividade
Estagnada
Stack de tecnologia
python
Domínio
networking

Direção de pesquisa

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.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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
Linguagem predominante
Python
Estrelas
77.8k
Forks
37.7k
Merge médio
17h 24min
PRs com merge (30d)
716

Preparar o ambiente

Abrir no Codespaces

Inicia o contêiner de desenvolvimento do projeto no navegador, com a sua própria conta do GitHub.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de python/cpython

Todas as issues de python/cpython

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.