#34091: Invalid SQL: FROM clauses can be omitted when QuerySet is accessed from
multiple threads
-------------------------------------+-------------------------------------
Reporter: Marti | Owner: nobody
Raudsepp |
Type: Bug | Status: new
Component: Database | Version: 4.1
layer (models, ORM) | Keywords: race-condition,
Severity: Normal | orm, threading, SQLCompiler
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I am experiencing very rare cases, where Django generates invalid SQL for
`select_related()` tables. This occurs in a place where multiple threads
are trying to execute the same QuerySet at the same time. (The access from
multiple threads was unintentional, but I believe this deserves to be
fixed in Django anyway)
When this occurs, tables/joins in the FROM clause can be missing entirely,
but they are still referenced in the SELECT clause, and the query fails,
for example `sqlite3.OperationalError: no such column: app_fk18.id` or
`ORA-00904: "xxx"."xxx": invalid identifier`
I could not reproduce this with vanilla Django, but after patching Django
to add `time.sleep(0)` in `SQLCompiler.compile()`, I can reliably
reproduce this.
The reproducer and longer explanation is here: https://github.com/intgr
/bug-reports/tree/main/django-query-race-condition
I suspect some mutation of the `Query` object is going on during SQL
compilation, but by efforts to narrow down the mutation have been
unsuccessful so far.
--
Ticket URL: <https://code.djangoproject.com/ticket/34091>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070183d171abb2-a4922361-96cd-4821-a897-284b426c269c-000000%40eu-central-1.amazonses.com.