Skip to content

Internal Server Error in Airflow API server with Keycloak provider when token is not active #59359

Closed

Description

@dabla

Apache Airflow Provider(s)

keycloak

Versions of Apache Airflow Providers

We are using the latest airflow providers shipped with Airflow 3.1.4 constraints.

The issue arised with Airflow 3.1.4 and apache-airflow-providers-keycloak 0.3.0.

Apache Airflow version

3.1.4

Operating System

Linux

Deployment

Other 3rd-party Helm chart

Deployment details

No response

What happened

When we keep a browser open for a long time, like overnight, and we want to refresh the Airflow dags page, the Airflow api server crashes with following exception:

INFO:     172.31.52.95:0 - "GET /favicon.ico HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
  + Exception Group Traceback (most recent call last):
  |   File "/usr/local/lib/python3.13/site-packages/starlette/_utils.py", line 79, in collapse_excgroups
  |     yield
  |   File "/usr/local/lib/python3.13/site-packages/starlette/middleware/base.py", line 183, in __call__
  |     async with anyio.create_task_group() as task_group:
  |                ~~~~~~~~~~~~~~~~~~~~~~~^^
  |   File "/usr/local/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 783, in __aexit__
  |     raise BaseExceptionGroup(
  |         "unhandled errors in a TaskGroup", self._exceptions
  |     ) from None
  | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "/usr/local/lib/python3.13/site-packages/uvicorn/protocols/http/httptools_impl.py", line 409, in run_asgi
    |     result = await app(  # type: ignore[func-returns-value]
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |         self.scope, self.receive, self.send
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |     )
    |     ^
    |   File "/usr/local/lib/python3.13/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
    |     return await self.app(scope, receive, send)
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "/usr/local/lib/python3.13/site-packages/fastapi/applications.py", line 1082, in __call__
    |     await super().__call__(scope, receive, send)
    |   File "/usr/local/lib/python3.13/site-packages/starlette/applications.py", line 113, in __call__
    |     await self.middleware_stack(scope, receive, send)
    |   File "/usr/local/lib/python3.13/site-packages/starlette/middleware/errors.py", line 186, in __call__
    |     raise exc
    |   File "/usr/local/lib/python3.13/site-packages/starlette/middleware/errors.py", line 164, in __call__
    |     await self.app(scope, receive, _send)
    |   File "/usr/local/lib/python3.13/site-packages/starlette/middleware/gzip.py", line 29, in __call__
    |     await responder(scope, receive, send)
    |   File "/usr/local/lib/python3.13/site-packages/starlette/middleware/gzip.py", line 130, in __call__
    |     await super().__call__(scope, receive, send)
    |   File "/usr/local/lib/python3.13/site-packages/starlette/middleware/gzip.py", line 46, in __call__
    |     await self.app(scope, receive, self.send_with_compression)
    |   File "/usr/local/lib/python3.13/site-packages/starlette/middleware/cors.py", line 85, in __call__
    |     await self.app(scope, receive, send)
    |   File "/usr/local/lib/python3.13/site-packages/starlette/middleware/base.py", line 182, in __call__
    |     with recv_stream, send_stream, collapse_excgroups():
    |                                    ~~~~~~~~~~~~~~~~~~^^
    |   File "/usr/lib64/python3.13/contextlib.py", line 162, in __exit__
    |     self.gen.throw(value)
    |     ~~~~~~~~~~~~~~^^^^^^^
    |   File "/usr/local/lib/python3.13/site-packages/starlette/_utils.py", line 85, in collapse_excgroups
    |     raise exc
    |   File "/usr/local/lib/python3.13/site-packages/starlette/middleware/base.py", line 184, in __call__
    |     response = await self.dispatch_func(request, call_next)
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "/usr/local/lib/python3.13/site-packages/airflow/api_fastapi/auth/middlewares/refresh_token.py", line 45, in dispatch
    |     new_user = await self._refresh_user(current_token)
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "/usr/local/lib/python3.13/site-packages/airflow/api_fastapi/auth/middlewares/refresh_token.py", line 68, in _refresh_user
    |     return get_auth_manager().refresh_user(user=user)
    |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
    |   File "/usr/local/lib/python3.13/site-packages/airflow/providers/keycloak/auth_manager/keycloak_auth_manager.py", line 121, in refresh_user
    |     tokens = client.refresh_token(user.refresh_token)
    |   File "/usr/local/lib/python3.13/site-packages/keycloak/keycloak_openid.py", line 410, in refresh_token
    |     return raise_error_from_response(data_raw, KeycloakPostError)
    |   File "/usr/local/lib/python3.13/site-packages/keycloak/exceptions.py", line 195, in raise_error_from_response
    |     raise error(
    |     ...<3 lines>...
    |     )
    | keycloak.exceptions.KeycloakPostError: 400: b'{"error":"invalid_grant","error_description":"Token is not active"}'
    +------------------------------------

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.13/site-packages/uvicorn/protocols/http/httptools_impl.py", line 409, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        self.scope, self.receive, self.send
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/usr/local/lib/python3.13/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/fastapi/applications.py", line 1082, in __call__
    await super().__call__(scope, receive, send)
  File "/usr/local/lib/python3.13/site-packages/starlette/applications.py", line 113, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.13/site-packages/starlette/middleware/errors.py", line 186, in __call__
    raise exc
  File "/usr/local/lib/python3.13/site-packages/starlette/middleware/errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.13/site-packages/starlette/middleware/gzip.py", line 29, in __call__
    await responder(scope, receive, send)
  File "/usr/local/lib/python3.13/site-packages/starlette/middleware/gzip.py", line 130, in __call__
    await super().__call__(scope, receive, send)
  File "/usr/local/lib/python3.13/site-packages/starlette/middleware/gzip.py", line 46, in __call__
    await self.app(scope, receive, self.send_with_compression)
  File "/usr/local/lib/python3.13/site-packages/starlette/middleware/cors.py", line 85, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.13/site-packages/starlette/middleware/base.py", line 182, in __call__
    with recv_stream, send_stream, collapse_excgroups():
                                   ~~~~~~~~~~~~~~~~~~^^
  File "/usr/lib64/python3.13/contextlib.py", line 162, in __exit__
    self.gen.throw(value)
    ~~~~~~~~~~~~~~^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/starlette/_utils.py", line 85, in collapse_excgroups
    raise exc
  File "/usr/local/lib/python3.13/site-packages/starlette/middleware/base.py", line 184, in __call__
    response = await self.dispatch_func(request, call_next)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/airflow/api_fastapi/auth/middlewares/refresh_token.py", line 45, in dispatch
    new_user = await self._refresh_user(current_token)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/airflow/api_fastapi/auth/middlewares/refresh_token.py", line 68, in _refresh_user
    return get_auth_manager().refresh_user(user=user)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/airflow/providers/keycloak/auth_manager/keycloak_auth_manager.py", line 121, in refresh_user
    tokens = client.refresh_token(user.refresh_token)
  File "/usr/local/lib/python3.13/site-packages/keycloak/keycloak_openid.py", line 410, in refresh_token
    return raise_error_from_response(data_raw, KeycloakPostError)
  File "/usr/local/lib/python3.13/site-packages/keycloak/exceptions.py", line 195, in raise_error_from_response
    raise error(
    ...<3 lines>...
    )
keycloak.exceptions.KeycloakPostError: 400: b'{"error":"invalid_grant","error_description":"Token is not active"}'

What you think should happen instead

When the KeycloakAuthManager tries to refresh the user and thus calls the refresh_token method from the Keycloak client and the later one fails with an KeycloakPostError, it should catch that exception and return None instead of propagating that exception as this makes the API server crash with HTTP 500 Internal Server Error.

Without that patch, the only way to fix this problem is to clear the cookies related to the API server and then refresh the page in the browser.

The above fix or clearing the cookies as work around will force Keycloak to redo an authentication and thus refresh the token successfully instead of relying on an expired token in the cookie.

How to reproduce

Just keep the Airflow DAG's page open for a long time until the token expires on Keycloak side, then refresh it and you'll get an Internal Server Error page from the Airflow API server.

Image

Anything else

Before the fix

When the API server received any request (e.g., GET /dags), Airflow’s refresh-token middleware tried to refresh the Keycloak token:

tokens = client.refresh_token(user.refresh_token)

If Keycloak returned invalid_grant (expired/invalid token), the python-keycloak client raised:

KeycloakPostError: 400: {"error":"invalid_grant"...}

This exception bubbled up unhandled, breaking the ASGI task group, causing:

500 Internal Server Error
ExceptionGroup: unhandled errors in a TaskGroup

So instead of prompting a re-auth, the whole API crashed.

After the fix

You wrapped client.refresh_token(...) in a try/except KeycloakPostError:

Catch the error

Log a warning

Return None instead of raising

Because of that:

✔ The middleware no longer throws
✔ The API request is allowed to proceed
✔ A new login will be triggered on the next request (because None means “user is not authenticated anymore”)
✔ No more 500 errors from Uvicorn/Starlette

In short:

The fix prevents an unhandled Keycloak refresh error from crashing the API server. Instead, it logs the issue and gracefully forces re-authentication.

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

  1. added
    kind:bug
    needs-triage
    on Dec 12, 2025
  2. changed the title [-]Internal Server Error in Airflow API server with keycloak provider when token is not active[/-] [+]Internal Server Error in Airflow API server with Keycloak provider when token is not active[/+] on Dec 12, 2025
  3. removed
    needs-triage
    on Dec 13, 2025
  4. dabla commented on Dec 13, 2025

    @dabla
    ContributorAuthor

    There is still an issue with the logout route which is overriden in the keycloak provider, I'm also fixing it and create a PR for it.

  5. nassereddinebelghith commented on Jan 4, 2026

    @nassereddinebelghith

    I am facing the same issue with airflow 3.1.5 and keycloak auth manager 0.4.1 , I tried to override the refresh tokens method and the refresh user but I am getting 403 instead of 500 error , the issue is that refresh token method is returning an empty dict {}.

  6. dabla commented on Jan 13, 2026

    @dabla
    ContributorAuthor

    I am facing the same issue with airflow 3.1.5 and keycloak auth manager 0.4.1 , I tried to override the refresh tokens method and the refresh user but I am getting 403 instead of 500 error , the issue is that refresh token method is returning an empty dict {}.

    You have this error I suppose?

    Image

    I think we need to refactor how the id_token_hint is stored today in keycloack provider, I was afraid that was going to be an issue. Will reopen this one.

  7. reopened this on Jan 13, 2026
  8. nassereddinebelghith commented on Jan 13, 2026

    @nassereddinebelghith

    I was able to manage the access token / refresh token expiration through nginx as I have a L7 layer but I am not so fan of my solution as it is a workaround. My issue was that when the refresh token is expired I got a 403 (after changing the 500 internal server error) that means that airflow is not launching the auth mechanism after an expired refresh token.

  9. Ilardi commented on Jan 14, 2026

    @Ilardi

    I just wanted to add that I have the same issue in airflow 3.1.4 but it only seems to happen when the refresh_token is expired. If the access_token is expired but the refresh_token is valid, upon reloading the page, a new airflow token is created so it appears to work correctly.

  10. vincbeck commented on Jan 15, 2026

    @vincbeck
    Contributor

    Multiple things in this issue:

    • As mentioned by @Ilardi, the refresh token works as long as the refresh token is valid. That's how the refresh token flow works in general. You have a JWT token that expires, when this token expires it is automatically refreshed using another token: refresh_token. However, when this token expire, there is not much we can do, the session expired and as a result you get some access denied exceptions. If you want to increase the refresh token lifetime you can do it in Keycloak by configuring Client Session Idle
    • We should handle that scenario in the refresh_token middleware so that we no longer have 500s. I'll take that one
    • We should also probably redirect automatically the user to the login page when that happens. I'll take that one too
  11. vincbeck commented on Jan 16, 2026

    @vincbeck
    Contributor

    I'll create multiple PRs related to this issue. The first one is fixing the logout flow in Keycloak. Currently, you need to have an active session (an active refresh_token) to logout. It should not be. See #60649

  12. vincbeck commented on Jan 19, 2026

    @vincbeck
    Contributor

    Fix: #60781

  13. ecodina commented on Jan 20, 2026

    @ecodina
    Contributor

    It's great to see this is fixed! Many thanks @vincbeck

    Right now we are clearing cookies twice a day when the token expires. Is the fix planned for the next release of KC provider + 3.1.7, or will it be available on 3.2? I see some changes in airflow-core

  14. vincbeck commented on Jan 20, 2026

    @vincbeck
    Contributor

    We probably should release it as well in 3.1.7. I'll backport it. And yes, changes in Keycloak provider will be released in the next provider release

  15. vincbeck commented on Jan 21, 2026

    @vincbeck
    Contributor

    Backport: #60881

  16. vincbeck commented on Jan 21, 2026

    @vincbeck
    Contributor

    Everything has been backported so I can confirm this will be available at the next Airflow version 3.1.7 and next Keycloak provider version

  17. ecodina commented on Jan 21, 2026

    @ecodina
    Contributor

    Many thanks!

  18. nassereddinebelghith commented on Jan 22, 2026

    @nassereddinebelghith

    Which version of the provider will include these changes

  19. vincbeck commented on Jan 22, 2026

    @vincbeck
    Contributor

    Next one (that is released next week). So given the current one is 0.5.0, it will be either 0.5.1 or 0.6.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:APIarea:autharea:providerskind:bugprovider:keycloak

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions