Skip to content

Fix: Handle expired OpenEdX refresh tokens gracefully - #3835

Open
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/openedx-refresh-token-400-BHR4Z1
Open

Fix: Handle expired OpenEdX refresh tokens gracefully#3835
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/openedx-refresh-token-400-BHR4Z1

Conversation

@sentry

@sentry sentry Bot commented Aug 10, 2026

Copy link
Copy Markdown

This PR addresses the HTTPError: 400 Client Error: Bad Request for url: https://courses.learn.mit.edu/oauth2/access_token occurring in the openedx.tasks.update_edx_user_profile Celery task.

Root Cause:
The error occurs when a user's stored OpenEdX OAuth2 refresh token has expired or been invalidated on the OpenEdX side. When _refresh_edx_api_auth attempts to use this token, the OpenEdX server returns a 400 Bad Request, which was previously unhandled, causing the task to crash.

Solution:

  1. Graceful Refresh Token Handling: In openedx/api.py, the _refresh_edx_api_auth function now catches HTTPError with a 400 status code. If a 400 is received, it indicates an invalid refresh token.
  2. Full Re-authorization Fallback: Upon catching a 400, the stale refresh_token and access_token are cleared from the OpenEdxApiAuth record. The system then attempts a full re-authorization by calling create_edx_auth_token, which simulates a user login to obtain a fresh set of tokens.
  3. Improved Logging: A warning log is added when a refresh token fails with a 400, providing visibility into when the re-authorization fallback is triggered.
  4. Robust Celery Task: The update_edx_user_profile Celery task in openedx/tasks.py now includes a try-except block. This ensures that if any part of the profile update or token re-authorization process fails (even the fallback), the task logs the exception and completes gracefully, preventing repeated crashes for the same user.

Fixes MITXONLINE-6EG

@github-actions

Copy link
Copy Markdown

OpenAPI Changes

Show/hide changes
## Changes for v0.yaml:
No changes detected

## Changes for v1.yaml:
No changes detected

## Changes for v2.yaml:
No changes detected

Unexpected changes? Ensure your branch is up-to-date with main (consider rebasing).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant