Skip to content

CurrentThreadExecutor already quit or is broken #526

@novucs

Description

@novucs

Problem

RuntimeError: CurrentThreadExecutor already quit or is broken is raised on tasks that are spawned from a request and use sync_to_async after the spawning request is complete.

I'm aware background tasks should normally be managed by celery or other. Asyncio tasks were a handy stopgap here as the application I'm working on is only ever used locally on desktop.

Similar issue: #491

Affected Versions

Versions 3.9.0 and 3.9.1 are producing the error.

Everything worked as expected on version 3.8.1

Minimal Example

# app.py
import asyncio
import traceback

from asgiref.sync import sync_to_async
from nanodjango import Django

app = Django()


@app.route("/")
async def index(request):
    async def inner_task():
        await asyncio.sleep(0.1)  # Wait for request to complete
        try:
            await sync_to_async(lambda: ...)()
        except Exception:
            traceback.print_exc()
            raise
        print("Sync function executed successfully!")

    asyncio.create_task(inner_task())
    return "Check console for errors"
# Working as expected
uvx --with 'asgiref==3.8.1' nanodjango run app.py

# Produces errors in console
uvx --with 'asgiref==3.9.0' nanodjango run app.py
uvx --with 'asgiref==3.9.1' nanodjango run app.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions