Code

Opened 17 months ago

Closed 11 months ago

Last modified 11 months ago

#19918 closed Bug (fixed)

select_for_update runs on db_for_read rather than db_for_write

Reported by: alexandruioan.cucu@… Owned by: kux
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Discussion on the developer group: https://groups.google.com/forum/?fromgroups=#!topic/django-developers/38gWxf5qfxg

This bug is quite easy to reproduce if you use mysql with a master-slave replication setup and start the slave db with a --read-only flag:

Traceback:
File "/home/kux/workspace/src/other/django/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/kux/workspace/src/other/django/django/contrib/admin/options.py" in wrapper
  366.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/kux/workspace/src/other/django/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/home/kux/workspace/src/other/django/django/views/decorators/cache.py" in _wrapped_view_func
  89.         response = view_func(request, *args, **kwargs)
File "/home/kux/workspace/src/other/django/django/contrib/admin/sites.py" in inner
  196.             return view(request, *args, **kwargs)
File "/home/kux/workspace/src/other/django/django/db/transaction.py" in inner
  209.                 return func(*args, **kwargs)
File "/home/kux/workspace/src/other/django-cms/cms/admin/pageadmin.py" in wrap
  154.             Page.objects.select_for_update()
File "/home/kux/workspace/src/other/django/django/db/models/query.py" in exists
  562.             return self.query.has_results(using=self.db)
File "/home/kux/workspace/src/other/django/django/db/models/sql/query.py" in has_results
  441.         return bool(compiler.execute_sql(SINGLE))
File "/home/kux/workspace/src/other/django/django/db/models/sql/compiler.py" in execute_sql
  818.         cursor.execute(sql, params)
File "/home/kux/workspace/src/other/django/django/db/backends/util.py" in execute
  40.             return self.cursor.execute(sql, params)
File "/home/kux/workspace/src/other/django/django/db/backends/mysql/base.py" in execute
  114.             return self.cursor.execute(query, args)
File "/home/kux/workspace/envs/hb23/local/lib/python2.7/site-packages/MySQLdb/cursors.py" in execute
  174.             self.errorhandler(self, exc, value)
File "/home/kux/workspace/envs/hb23/local/lib/python2.7/site-packages/MySQLdb/connections.py" in defaulterrorhandler
  36.     raise errorclass, errorvalue

Exception Type: DatabaseError at /admin/cms/page/add/
Exception Value: (1290, 'The MySQL server is running with the --read-only option so it cannot execute this statement')

Attachments (0)

Change History (9)

comment:1 Changed 17 months ago by kux

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to kux
  • Patch needs improvement unset
  • Status changed from new to assigned

comment:2 Changed 17 months ago by kux

For patch see pull request: https://github.com/django/django/pull/852

comment:3 Changed 17 months ago by akaariai

  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 15 months ago by kux

  • Triage Stage changed from Accepted to Ready for checkin

comment:5 Changed 15 months ago by claudep

  • Triage Stage changed from Ready for checkin to Accepted

Sorry, but the "Ready for checkin" flag should be set by another reviewer, not by the patch author.

comment:6 Changed 14 months ago by anonymous coward

Nifty. I'm glad to see a patch for this. I was having the same issue, and decided to search for an existing ticket before adding excessive bytes to the Interwebz.

comment:7 Changed 14 months ago by anonymous

The fact that the db was running with --read-only flag is a fortunate case. Otherwise this bug would cause subtle concurrency issues since all exclusive locks would be held on the slave db instead of master.

comment:8 Changed 11 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 1c64a0f29e47a3da1dfac06b04ef84280bd809a7:

Fixed #19918 -- Modified select_for_update to run on the write database.

comment:9 Changed 11 months ago by Tim Graham <timograham@…>

In 80b95a2b1fc8c8425a81a23706af971061dc238f:

[1.6.x] Fixed #19918 -- Modified select_for_update to run on the write database.

Backport of 1c64a0f29e from master

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.