Skip to content

fix: UnboundLocalError in BulkRoleCache.prefetch for authz compat roles - #39251

Open
efortish wants to merge 1 commit into
openedx:masterfrom
eduNEXT:ks/issue-514-bulkrolecache-prefetch
Open

efortish wants to merge 1 commit into
openedx:masterfrom
eduNEXT:ks/issue-514-bulkrolecache-prefetch

Conversation

@efortish

@efortish efortish commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes [openedx-authz#514]

BulkRoleCache.prefetch() contains two loops: one processes legacy CourseAccessRole records and builds a compat_role object for each row, while the other processes Open edX AuthZ compatibility roles. However, the AuthZ loop iterates over role but mistakenly adds compat_role to the cache instead.

# openedx-authz roles
for user in users:
    compat_roles = get_authz_compat_course_access_roles_for_user(user)
    for role in compat_roles:
        course_id = get_role_cache_key_for_course(role.course_id)
        user_roles_set_for_course = roles_by_user[user.id][course_id]
        user_roles_set_for_course.add(compat_role)  # Should be `role`

This caused two distinct issues:

  • UnboundLocalError: If no legacy roles were processed for any user in the batch, compat_role was never assigned, causing prefetch() to crash.
  • Incorrect role caching: If the legacy loop ran, the AuthZ loop silently cached the last legacy role processed instead of the actual AuthZ role.

This bug affected the Gradebook because bulk_gradebook_view_context() calls BulkRoleCache.prefetch(). As a result, the Gradebook could fail for courses where a listed user had an AuthZ compatibility role but no legacy CourseAccessRole. Any other caller of prefetch() was susceptible to the same issue.

The fix replaces compat_role with role when adding the AuthZ role to the cache, ensuring that the correct role is stored regardless of whether legacy roles were processed.

Testing

  • Added BulkRoleCachePrefetchTestCase to test_roles.py, covering two scenarios:
    • A user with only an AuthZ compatibility role, which previously triggered UnboundLocalError.
    • A batch containing legacy and AuthZ roles across different users, which previously caused the wrong role to be cached for the AuthZ user.
  • Verified that both tests fail against the original implementation and pass with the fix.
  • Confirmed that ruff, pylint, and lint-imports pass.
  • Ran the full common/djangoapps/student test suite: 431 passed, 284 skipped.

Deadline

None.

BulkRoleCache.prefetch has two loops: one over legacy CourseAccessRole
rows, which builds and caches a `compat_role` object, and one over
openedx-authz compat roles, which iterates with `role` but was adding
`compat_role` to the set instead. If the legacy loop never ran for any
user, compat_role was unbound and the whole call raised
UnboundLocalError. If it did run, the authz loop silently cached the
last legacy role instead of the actual authz one.

This broke the Gradebook (BulkRoleCache.prefetch is called from
bulk_gradebook_view_context) for any course where a listed user had an
authz compat role but no legacy CourseAccessRole, and any other caller
of prefetch was equally affected.

Add it instead of compat_role, and add regression tests covering a
user with only an authz role and a mix of legacy/authz roles across
different users in the same prefetch call.

Fixes openedx-authz#514.
@openedx-webhooks

Copy link
Copy Markdown

Thanks for the pull request, @efortish!

This repository is currently maintained by @openedx/wg-maintenance-openedx-platform-oncall.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Oct 17, 2026
@github-project-automation github-project-automation Bot moved this to Needs Triage in Contributions Oct 17, 2026
@rafeehcp

Copy link
Copy Markdown

We hit this on Verawood (release/verawood.1) and can confirm the fix.

We moved course roles into openedx-authz with migrate_legacy_course_roles_to_authz(..., delete_after_migration=True) and turned on authz.enable_course_authoring. Then we ran the grade report's _CourseGradeBulkContext and bulk_gradebook_view_context against that database:

  • If no user in the batch has a CourseAccessRole row (the normal state once the migration deletes them), prefetch raises UnboundLocalError: cannot access local variable 'compat_role'.
  • If the batch also includes one user with a legacy row (an org_course_creator_group row, which has no openedx-authz form), the staff, instructor and beta tester users get no course role in the cache.

With the one-line change in this PR, both bulk contexts cache the same roles as each user's own RoleCache. release/verawood and master still have the bug, so a backport to release/verawood would help Verawood operators who turn on course authoring.

This branch has not been deployed

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

Labels

open-source-contribution PR author is not from Axim or 2U

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

3 participants