﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26888	RegexURLResolver doesn't work in threaded environments	Markus Holtermann	Marten Kenbeek	"As reported on [https://groups.google.com/forum/#!topic/django-developers/D_bIeinKHjE django-developers]:

I'm using django (1, 10, 0, u'beta', 1).

When I try to reverse url in shell everything goes fine.

When under nginx/uwsgi with many concurrent request I get

{{{#!python
... /local/lib/python2.7/site-packages/django/urls/resolvers.py"", line 241,
in reverse_dict
    return self._reverse_dict[language_code]
KeyError: 'it'
}}}

After a wile I figured out that RegexURLResolver is memoized by `get_resolver` and so it acts like a singleton for a certain number of requests.

Analyzing the code of  RegexURLResolver I found that the method `_poupulate` will return directly if it has been called before and not yet finished.

{{{#!python
...
def _populate(self):
    if self._populating:
        return
    self._populating = True
...
}}}

if used for recursive call in a single thread this will not hurt, but in my case in uwsgi multi thread mode I got the error.

here is my quick and dirty fix:

{{{#!python
class RegexURLResolver(LocaleRegexProvider):
    def __init__(self, regex, urlconf_name, default_kwargs=None, app_name=None, namespace=None):

        ...

        self._populating = False
        self.RLock = threading.RLock()

        ...

    def _populate(self):
        if self._populating:
            self.RLock.acquire()
            self.RLock.release()
            return
        self._populating = True
        self.RLock.acquire()

        ...

        self._populating = False
        self.RLock.release()
}}}"	Bug	closed	Core (URLs)	1.10	Release blocker	fixed			Ready for checkin	1	0	0	0	0	0
