TaskGroup: tg.cancel() shouldn't block create_task() allow cooperative cancellation
Ninguém assumiu esta issue ainda.
- Linguagem predominante
- Python
- Estrelas
- 77.2k
- Forks
- 35.9k
- Métricas de merge de PRs
- Métricas de PR pendentes
Descrição
Currently, when a TaskGroup is explicitly cancelled via tg.cancel(), any subsequent call to tg.create_task() raises:
RuntimeError: TaskGroup <TaskGroup cancelling> is shutting down
This contradicts the TODO in the test test_taskgroup_cancel_before_create_task, which states:
This behavior is not ideal. We'd rather have no exception raised, and the child task run until the first await.
Proposed change:
After an explicit tg.cancel() (not after a failure-induced abort), create_task() should succeed. The new task should run its synchronous code up to the first await, then receive CancelledError
For failure-driven aborts (e.g., an exception in another task), the existing RuntimeError is preserved to prevent unsafe task creation during error cleanup.
A PR implementing this change (with a new _explicitly_cancelled flag) is ready and passes all existing tests.
This resolves the TODO and makes TaskGroup behavior more intuitive when voluntarily cancelled.
Linked PRs
- gh-150357
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Comece pelo TODO test_taskgroup_cancel_before_create_task e pelos entry points cancel/create_task de TaskGroup descritos na issue. Compare tg.cancel() explícito com o comportamento de abort acionado por falha. Está concluído quando uma task criada após o cancelamento explícito executa seu código síncrono antes de receber CancelledError, enquanto aborts acionados por falha continuam gerando RuntimeError.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- python
- Domínio
- backend
- Tipo de issue
- Funcionalidade
- Dificuldade
- 3/5
- Tempo estimado
- 1-2 dias
- Status de atividade
- Estagnada
- Clareza
- Claramente especificada
- Facilidade para iniciantes
- 25/100