Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#25824 closed Uncategorized (invalid)

Assertions in libmemcached when multiple django.core.cache.backends.memcached.PyLibMCCache backends used

Reported by: Michał Łowicki Owned by: nobody
Component: Uncategorized Version: 1.8
Severity: Normal Keywords:
Cc: Florian Apolloner Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Michał Łowicki)

We're running Django 1.8.3 via uWSGI with threads enabled (uWSGI 2.0.11.5, configuration - https://gist.github.com/mlowicki/9a5042b5411537a51bbf).

When two backends using 'django.core.cache.backends.memcached.PyLibMCCache' are added and are used (each box is handling at least hundreds requests per second) then in uWSGI logs we're seeing lots of https://gist.github.com/mlowicki/a8b3dbd98f32dc40c4eb and application is misbehaving throwing 500s. When launched in second datacenter with much less threads then problem is much smaller but still exists (uWSGI configuration - https://gist.github.com/mlowicki/cbd18c689c3e2f838bf0)

Tried with pylibmc 1.4.1 and 1.5.0. Running on Debian Wheezy.

Change History (16)

comment:1 by Michał Łowicki, 9 years ago

Description: modified (diff)

comment:2 by Tim Graham, 9 years ago

It's not clear to me that Django is at fault. Can you provide any details about that?

comment:3 by Michał Łowicki, 9 years ago

Description: modified (diff)

comment:4 by Michał Łowicki, 9 years ago

I'm not 100% sure it's Django's fault but according to https://docs.djangoproject.com/en/1.8/topics/cache/#django.core.cache.caches it should be thread-safe.

comment:5 by Tim Graham, 9 years ago

Resolution: needsinfo
Status: newclosed

Okay, please use TicketClosingReasons/UseSupportChannels and reopen this ticket with more details if does turn out to be a bug in Django. Thanks!

comment:6 by Michał Łowicki, 9 years ago

FunkyBob told me 1h ago on IRC to file what I've but not it's closed within couple of minutes...

comment:7 by Simon Charette, 9 years ago

@mlowicki, I'm not familar with uWSGI's threading but Django doesn't reuse pylicmc.Client between threads.

Did you try using python-memcached instead?

comment:8 by Michał Łowicki, 9 years ago

Yes, for 2nd backend I'm using python-memcached (and it works) but pylimbc has much more options to configure and support for binary protocol. I'll try to debug in pylibmc to see if this will get me somewhere.

comment:9 by Simon Charette, 9 years ago

Alright, keep us updated.

FWIW Django stores aliased cache instances in a threading.local property and the wrapped pylibmc.Client instances are created in PyLibMCCache._cache.

You might want to investigate if pylibmc.ThreadMappedPool does anything different since it looks like it solved a similar issue.

#11331 and #5133 might also be related.

comment:10 by Curtis Maloney, 9 years ago

I believe the specific quirk of this case is using the same memcache driver for two separate cache backends ... so it's likely a problem independent of threading, but possibly that the lib isn't save to use on two connections concurrently in the same thread.

@mlowicki could you please try to replicate the issue without threads?

in reply to:  10 comment:11 by Florian Apolloner, 9 years ago

Replying to funkybob:

@mlowicki could you please try to replicate the issue without threads?

I agree on that. I see no reason why this would be a problem in Django, we properly use one Connection per thread per alias. Build your python/memcached/pylibmc with debug infos and throw GDB at it.

comment:12 by Florian Apolloner, 9 years ago

Cc: Florian Apolloner added

comment:13 by Michał Łowicki, 9 years ago

Just tried without threads and it works just fine.

comment:14 by Michał Łowicki, 9 years ago

It turned out we were assigning cache backend to variable at the top of the module so all threads were using the same instance.

comment:15 by Florian Apolloner, 9 years ago

Jupp, that certainly does cause such behavior.

comment:16 by Florian Apolloner, 9 years ago

Resolution: needsinfoinvalid
Note: See TracTickets for help on using tickets.
Back to Top