Django

Code

Ticket #3279 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

python-mysql-1.2.0 has a bug with threading, raising 'ReferenceError: weakly-referenced object no longer exists'

Reported by: wbyoung@mcdonogh.org Assigned to: adrian
Milestone: Component: Documentation
Version: SVN Keywords:
Cc: dcramer@gmail.com Triage Stage: Design decision needed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

I'm getting the following error every so often from a revision checked out on Jan 02, 2007. Sorry... I updated before checking the revision number. Jan 02 puts it somewhere between r4270 and r4274.

2007-01-10 16:36:40: (mod_fastcgi.c.2502) FastCGI-stderr: Traceback (most recent call last):
  File "build/bdist.linux-i686/egg/flup/server/fcgi_base.py", line 558, in run
  File "build/bdist.linux-i686/egg/flup/server/fcgi_base.py", line 1112, in handler
  File "/usr/lib/python2.4/site-packages/django/core/handlers/wsgi.py", line 193, in __call__
    response = middleware_method(request, response)
  File "/usr/lib/python2.4/site-packages/django/contrib/sessions/middleware.py", line 81, in process_response
    session_key = request.session.session_key or Session.objects.get_new_session_key()
  File "/usr/lib/python2.4/site-packages/django/contrib/sessions/models.py", line 21, in get_new_session_key
    self.get(session_key=session_key)
  File "/usr/lib/python2.4/site-packages/django/db/models/manager.py", line 67, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/usr/lib/python2.4/site-packages/django/db/models/query.py", line 211, in get
    obj_list = list(clone)
  File "/usr/lib/python2.4/site-packages/django/db/models/query.py", line 103, in __iter__
    return iter(self._get_data())
  File "/usr/lib/python2.4/site-packages/django/db/models/query.py", line 430, in _get_data
    self._result_cache = list(self.iterator())
  File "/usr/lib/python2.4/site-packages/django/db/models/query.py", line 172, in iterator
    cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params)
  File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 137, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 33, in defaulterrorhandler
    raise errorclass, errorvalue
ReferenceError: weakly-referenced object no longer exists

Let me know if there's any more info you'd like.

Attachments

install.diff (0.7 kB) - added by Marc Fargas <telenieko@telenieko.com> on 01/24/07 18:07:24.
Adds a small note on install.txt on the DB bindings section.

Change History

01/18/07 16:35:15 changed by mir@noris.de

  • status changed from new to closed.
  • resolution set to worksforme.

Hi,

the problem with this bug is, if we cannot reproduce the bug, we cannot fix it. And I use mysql and fastcgi myself all the time, and it just works!

Can you please give us some details how to reproduce the bug? I have some feeling that it's also related to fastcgi, but I cannot reproduce it.

  • Is it reproduceable on your side?
  • Are you using fastcgi with different processes, or with threading?
  • What's your version of the mysql python module, and what mysql version do you use?
  • Version of flup?
  • Anything else that could help?

I'm closing the bug in the meantime. Please reopen it when you reply!

- Michael

01/18/07 20:35:45 changed by wbyoung@mcdonogh.org

Info:

  • flup revision 2305
  • fcgi-2.4.0 with threading
  • mysql Ver 14.12 Distrib 5.0.27, for redhat-linux-gnu (i686) using readline 5.0
  • mysql-python 1.2.1_p2

Here are some time stamps for frequency:

2007-01-17 10:27:03:
2007-01-17 11:55:23:
2007-01-17 13:26:54:
2007-01-17 14:13:51:
2007-01-17 16:47:06:
2007-01-17 18:13:44:
2007-01-18 03:19:09:
2007-01-18 05:01:25:
2007-01-18 06:43:56:
2007-01-18 06:48:57:
2007-01-18 08:15:19:
2007-01-18 08:28:43:
2007-01-18 09:59:43:
2007-01-18 13:40:09:
2007-01-18 13:44:30:
2007-01-18 16:03:45:

So yes it's reproducable... it happens quite a bit, but the only times that I've seen an error it only takes reloading the page to stop the error. I don't know steps to reproduce it. And it happens on multiple pages.... not just one.

There are actually two other weak references that seem to be popping up.... they seem to be related so I included occurrences of those in the timestamps.

  File "build/bdist.linux-i686/egg/flup/server/fcgi_base.py", line 558, in run
  File "build/bdist.linux-i686/egg/flup/server/fcgi_base.py", line 1112, in handler
  File "/usr/lib/python2.4/site-packages/django/core/handlers/wsgi.py", line 193, in __call__
    response = middleware_method(request, response)
  File "/usr/lib/python2.4/site-packages/django/contrib/sessions/middleware.py", line 89, in process_response
    datetime.datetime.now() + datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE))
  File "/usr/lib/python2.4/site-packages/django/contrib/sessions/models.py", line 29, in save
    s.save()
  File "/usr/lib/python2.4/site-packages/django/db/models/base.py", line 175, in save
    (backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.pk.column)), [pk_val])
  File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 137, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 33, in defaulterrorhandler
    raise errorclass, errorvalue
ReferenceError: weakly-referenced object no longer exists


  File "build/bdist.linux-i686/egg/flup/server/fcgi_base.py", line 558, in run
  File "build/bdist.linux-i686/egg/flup/server/fcgi_base.py", line 1112, in handler
  File "/usr/lib/python2.4/site-packages/django/core/handlers/wsgi.py", line 193, in __call__
    response = middleware_method(request, response)
  File "/usr/lib/python2.4/site-packages/django/contrib/sessions/middleware.py", line 89, in process_response
    datetime.datetime.now() + datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE))
  File "/usr/lib/python2.4/site-packages/django/contrib/sessions/models.py", line 29, in save
    s.save()
  File "/usr/lib/python2.4/site-packages/django/db/models/base.py", line 204, in save
    ','.join(placeholders)), db_values)
  File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 137, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 33, in defaulterrorhandler
    raise errorclass, errorvalue
ReferenceError: weakly-referenced object no longer exists

Thanks for looking into it.

01/19/07 03:42:49 changed by anonymous

Hi, thanks for the fast reply!

Could you try to switch to non-threading fastcgi with multiple processes? This just *looks* to be a threading bug. It might be that threading is not supported with this mysql lib. With the logs you see, I'd strongly advise not to use this setup in production.

Please don't mind the closed status of this ticket, this is only due to the reopen problem. I'm currently keeping a watch list of tickets ...

01/19/07 03:43:29 changed by mir@noris.de

Args, that previous Mr. Anonymous was me.

01/19/07 08:27:30 changed by wbyoung@mcdonogh.org

I'm running it preforked now. I'll let you know if I get the same errors or not.

I'm on a production server with very little resources, though, and would prefer to run the fcgi as threaded instead of forked. Do you think that there might not be this same issue with PostgreSQL?

01/19/07 14:07:10 changed by mir@noris.de

  • component changed from Admin interface to Core framework.
  • summary changed from MySQL connections.py ReferenceError: weakly-referenced object no longer exists to threading fastcgi does not work with mysql, raising 'ReferenceError: weakly-referenced object no longer exists'.

If django with threading-flup uses one global connection object and multiple cursors created from the global connection object, this is not going to work, see:

http://forums.mysql.com/read.php?50,104391,104688#msg-104688

Regarding threading and PostgreSQL, there's a lot to say why you shouldn't do threading with python on Linux, but please ask on the user list. I'd like to keep the ticket clean and separate from user support, as a long ticket with sidelines will be difficult to read.

Before I put this ticket forward, I'm going to find out about whether there's a global connection object or whether I can verify this, but this might take until Monday or Tuesday.

01/19/07 14:37:23 changed by wbyoung@mcdonogh.org

FYI: No errors with the preforked method.

01/24/07 10:08:05 changed by anonymous

  • cc set to dcramer@gmail.com.
  • stage changed from Unreviewed to Accepted.

#2919 marked as duplicate. #2919 is about lighthttp/fastcgi/threaded/mysql and has a similar backtrace. I'm putting this now to 'Accepted'. I have not yet verified it, but with multiple sources under different http servers, this seems to be serious with mysql and threading.

01/24/07 10:15:45 changed by mir@noris.de

  • summary changed from threading fastcgi does not work with mysql, raising 'ReferenceError: weakly-referenced object no longer exists' to python-mysql-1.2.0 has a bug with threading, raising 'ReferenceError: weakly-referenced object no longer exists'.
  • component changed from Core framework to Documentation.
  • stage changed from Accepted to Design decision needed.

Aha! Found this in the mailing list archives: http://wolfram.kriesing.de/blog/index.php/2006/multithreading-with-mysqldb-and-weakrefs

python-mysql-1.2.0 has a problem with multithreading. Please upgrade. 1.2.1p2 has been reported to solve this. Thanks, Wolfram Kriesing!

Are you two able to test this?

@core: Should we add a warning to the docs?

01/24/07 10:16:37 changed by mir@noris.de

  • keywords set to reopen.

(follow-up: ↓ 12 ) 01/24/07 11:11:36 changed by wbyoung@mcdonogh.org

Sorry, I never specified a web server, but I'm on lighttpd, too.

And my error was with mysql-python 1.2.1p2, so maybe it's still a problem with 1.2.1p2.

(in reply to: ↑ 11 ) 01/24/07 14:48:19 changed by mir@noris.de

Replying to wbyoung@mcdonogh.org:

Sorry, I never specified a web server, but I'm on lighttpd, too. And my error was with mysql-python 1.2.1p2, so maybe it's still a problem with 1.2.1p2.

Args, what a strange bug.

Are you absolutely 120 % sure that you are actually using the 1.2.1p2 module? The blog entry from Wolfram points to exactly the line that, according to your logs, is responsible for the error message. I find this hard to believe. Perhaps we can check this:

  • Where is python-mysql installed?
  • What are the contents of lines 35-45 in the cursors.py file within MySQLdb?
  • Please give us the output of (please follow exactly!)
python manage.py shell
import MySQLdb
MySQLdb.version_info
MySQLdb.__path__
  • Another detail that might be interesting: What's your operating system?

01/24/07 16:24:12 changed by wbyoung@mcdonogh.org

nevermind. you're right. i did have a previous version of python-mysql installed. i updated and set things up as threaded again. i'll let you know if i have any more problems, but you can probably close this now.

01/24/07 16:53:44 changed by Marc Fargas <telenieko@telenieko.com>

  • keywords deleted.
  • stage changed from Design decision needed to Accepted.

For the record:

Already closed, removing "reopen" keyword.

01/24/07 17:04:41 changed by anonymous

  • keywords set to reopen.
  • stage changed from Accepted to Design decision needed.

Marc, Please read comment #9.

I'd still like to see it documented that you shouldn't use python-mysql-1.2.0 with threading, so I hope for Jacob's magic to reopen this ticket.

01/24/07 17:05:39 changed by mir@noris.de

the last one was me. I promise to start using trac's session thingie from now on ;-)

01/24/07 18:07:24 changed by Marc Fargas <telenieko@telenieko.com>

  • attachment install.diff added.

Adds a small note on install.txt on the DB bindings section.

01/24/07 18:09:26 changed by Marc Fargas <telenieko@telenieko.com>

Sorry mir, forgot about comment 9 while reading the ticket :) [I've got the memory of a mosquito!]
Added a patch with a comment for that on install.txt hope it's enough if documentation about that is to be added ;)

01/24/07 18:41:16 changed by adrian

  • status changed from closed to reopened.
  • resolution deleted.

01/24/07 18:41:17 changed by anonymous

  • has_patch set to 1.

01/24/07 18:44:00 changed by Michael Radziej <mir@noris.de>

  • keywords deleted.

02/26/07 15:25:22 changed by jacob

  • status changed from reopened to closed.
  • resolution set to fixed.

(In [4621]) Fixed #3279: Added a note about using MySQLdb 1.2.1 or later. Thanks, Marc Fargas.


Add/Change #3279 (python-mysql-1.2.0 has a bug with threading, raising 'ReferenceError: weakly-referenced object no longer exists')




Change Properties
Action