kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Potential UnboundLocalError in ClassroomNotificationViewset.list() when filter_queryset() raises DB exception

Open TheGreatPratyush opened this issue 9 months ago • 4 comments

❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.

Target branch: develop

Observed behavior

In ClassroomNotificationViewset.list() which is inside kolibri/plugins/coach/api.py . If filter_queryset() raises a DatabaseError or OperationalError the exception is caught and repair_sqlite_db() is executed . While reviewing the error-handling path, I simulated a DatabaseError inside filter_queryset() to test the recovery behavior. In this scenario, queryset is not reassigned before being used later in the method. This results in:

UnboundLocalError: local variable 'queryset' referenced before assignment Because of this, the request to /coach/api/notifications/ returns a 500 Internal Server Error.

Image

Errors and logs

ERROR Internal Server Error: /coach/api/notifications/
Traceback (most recent call last):
  File ".../rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File ".../kolibri/plugins/coach/api.py", line 227, in list
    "results": self.serialize(queryset),
UnboundLocalError: local variable 'queryset' referenced before assignment

INFO 127.0.0.1 - "GET /coach/api/notifications/" 500

…

Expected behavior

When a DatabaseError or OperationalError happens inside filter_queryset(), the endpoint currently returns a 500 error. After repair_sqlite_db() is triggered, queryset is not reassigned, and later in the list() method it is still used. Because of that, it crashes with:

UnboundLocalError: local variable 'queryset' referenced before assignment

Instead of crashing, the endpoint should recover properly and return a valid JSON response.

User-facing consequences

When this happens, the Coach page triggers repeated 500 errors for the notifications endpoint. Because of this, classroom notifications do not load properly. The browser console keeps showing failed requests, and the page continues sending repeated requests to the notifications endpoint. For coaches, this means recent learner progress notifications are not visible

…

Steps to reproduce

  1. Start Kolibri locally from source.
  2. Temporarily modify filter_queryset() to raise a DatabaseError (to test the error-handling path).
  3. Restart the server.
  4. Log in as a user with Coach permissions.
  5. Navigate to the Coach page.
  6. Observe the request to /coach/api/notifications/ returning 500 Internal Server Error. …

Context

Application version: Kolibri develop (running from source) OS: macOS Python: 3.10 Browser: Chrome 145.0.7632.117 (arm64)

TheGreatPratyush avatar Mar 03 '26 21:03 TheGreatPratyush

Hey @TheGreatPratyush , I would like to work on this issue. Please assign me first, so I can get started!

prabhat-kumar96 avatar Mar 06 '26 19:03 prabhat-kumar96

Hi! 👋

Thanks so much for your interest! This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues. If there are no unassigned 'help wanted' issues available, please wait until new ones are added.

We really appreciate your willingness to help. 😊

Are you preparing for Google Summer of Code? See our GSoC guidelines.

learning-equality-bot avatar Mar 06 '26 19:03 learning-equality-bot

I assume that @TheGreatPratyush wanted to fix this as he reported it, so am assigning him. If you do not want to work on it, let me know.

Please make sure to include Python regression test as part of your fix.

rtibbles avatar Mar 07 '26 04:03 rtibbles

Thankyou for assigning this issue . I will raise PR with the fix so that the team can review it

TheGreatPratyush avatar Mar 07 '26 04:03 TheGreatPratyush