goauthentik / goauthentik/authentik

CancelledError exception in shielded future causing Gunicorn worker to exit

Open
#23,197 0 comments 1 reaction 0 assignees View on GitHub
bug status/reviewing
Dominant language
Python
Stars
25.6k
Forks
2k
Avg merge
1d 2h
Merged PRs (30d)
659

Description

### Describe the bug

The Authentik server container is continuously throwing a `CancelledError exception in shielded future` within the Django middleware stack (wrapped by `asgiref/sync.py`). Immediately following these exceptions, Gunicorn throws an `[Errno 9] Bad file descriptor` error, causing the worker to exit and reboot. There is also a timezone warning regarding `CacheEntry.expires` receiving a naive datetime.

### How to reproduce

1. See error in the server logs.

### Expected behavior

The server should handle asynchronous tasks gracefully without dropping connections, and Gunicorn workers should remain stable without restarting.

### Screenshots

_No response_

### Additional context

Deployed a PostgreSQL Cluster

### Deployment Method

Docker

### Version

2026.5.3

### Relevant log output

```shell
{"event": "CancelledError exception in shielded future\nfuture: ", "exception": [...], "frames": [{"filename": "/usr/local/lib/python3.14/concurrent/futures/thread.py", "lineno": 86, "name": "run"}, ..., {"filename": "/authentik/root/middleware.py", "lineno": 331, "name": "__call__"}, ..., {"filename": "/ak-root/.venv/lib/python3.14/site-packages/asgiref/sync.py", "lineno": 523, "name": "__call__"}], "level": "error", "logger": "asyncio", "timestamp": "2026-06-19T09:15:08.050540"}

{"event": "CancelledError exception in shielded future\nfuture: ", "exception": [...], "level": "error", "logger": "asyncio", "timestamp": "2026-06-19T09:15:12.832582"}

{"event": "Error while closing socket [Errno 9] Bad file descriptor", "level": "info", "logger": "gunicorn.error", "timestamp": "2026-06-19T09:15:12.901919"}
{"event": "Worker exiting (pid: 135942)", "level": "info", "logger": "gunicorn.error", "timestamp": "2026-06-19T09:15:13.003582"}
{"event": "Booting worker with pid: 149643", "level": "info", "logger": "gunicorn.error", "timestamp": "2026-06-19T09:15:14.575071"}

/ak-root/.venv/lib/python3.14/site-packages/django/db/models/fields/__init__.py:1671: RuntimeWarning: DateTimeField CacheEntry.expires received a naive datetime (9999-12-31 23:59:59) while time zone support is active.
```

Contributor guide

Open the contributing guide

Research direction

Start with authentik/root/middleware.py around line 331 and trace the asynchronous call through asgiref/sync.py, correlating the cancellation with Gunicorn's socket-close and worker-exit logs. Check the CacheEntry.expires timezone warning separately. Done means the reported cancellation no longer causes worker restarts or dropped connections, with the warning's status explicitly verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, docker, postgresql, python
Domain
authentication, backend, devops
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.