You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The patch creates a dictionary of reentrant locks keyed by module full name. Trying to import a module or package will first get the lock for that module (or, if necessary, create it) and then acquire it. This is done for any module type.
The global import lock is still there, but only used for two things:
serializing first time creation of module-specific locks
protection of imports based on import hooks, since we don't know whether third-party import hooks are themselves thread-safe
Semantics of the public C API are unchanged, because it is not clear whether they should be or not (concerns of usefulness vs. compatibility). For example, PyImport_ImportModuleNoBlock() still uses the global import lock but this could be relaxed in a later patch.
So I say we don't worry about loaders being thread-safe. If __import__ handles the locking for a specific module then it will hold the lock on behalf of the loader. Now if someone decides to call load_module on their own, that's there business, but they should be aware of what could happen if they do that without acquiring the lock themselves. Otherwise we just make sure to provide a context manager that takes the name of the module and people can use that when they make their call to loader.load_module.
So I say we don't worry about loaders being thread-safe. If __import__
handles the locking for a specific module then it will hold the lock
on behalf of the loader.
Yes but what happens if two different modules are imported from two
different threads, and handled by the same loader? The loader could have
global structures which rely on serialization of imports for
consistency.
> So I say we don't worry about loaders being thread-safe. If __import__
> handles the locking for a specific module then it will hold the lock
> on behalf of the loader.
Yes but what happens if two different modules are imported from two
different threads, and handled by the same loader? The loader could have
global structures which rely on serialization of imports for
consistency.
That's why I said we should supply a context decorator (or function) which
will handle the lock appropriately, taking the name of the module to import
as an argument so the locking is fine-grained.
That's why I said we should supply a context decorator (or function) which
will handle the lock appropriately, taking the name of the module to import
as an argument so the locking is fine-grained.
Ok, so what are you saying is that we can break compatibility for non
thread-safe import loaders which currently work fine?
(I have nothing against it, just trying to be sure we agree on the
implications)
What I'm saying is that loaders are quite possibly not thread-safe already, so we don't need to do any special for them. If you look at PEP-302 you will notice not a single mention of loaders needing to care about the import lock because there is no mention of the import lock! So changing the locking mechanism most likely won't break loaders because they are not using the current import lock anyway and so already have their own issues.
As long as __import__ does the proper locking on behalf of loaders and we provide a way for people to use the lock if they want to then I am not worried about the impact on loaders. For example, this will change the logic in importlib where the current import lock is grabbed, but otherwise won't change a thing in terms of the code for the various loaders it implements.
So changing the locking mechanism most likely won't break loaders
because they are not using the current import lock anyway and so
already have their own issues.
Are you sure they aren't using it implicitly?
In vanilla py3k, PyImport_ImportModuleLevel() takes the import lock
therefore it protects any inner code, including the various hooks.
grahamd commented on Jul 14, 2010
grahamdmannequin
Mannequin
More actions
How is this going to deal with cyclical imports where different threads could import at the same time different modules within that cycle? I need to look through the proposed patch and work out exactly what it does, but am concerned about whether this approach would cause the classic deadlock problem if not done right?
FWIW, this concept of a lock per module is what I used in the mod_python module importer when it was rewritten. I would have to go look back over that code and see how the way the concept is being implemented differs, but there was one remaining potential race condition in the mod_python code which could in rare instances cause a problem. I never did get around to fixing it. Anyway, what I did learn was that this approach isn't necessarily as simple as it may seem so it will need some really good analysis on whatever solution is developed to ensure subtle problems don't come up.
That's my point; loaders are using the lock implicitly so that's why we don't need to worry about the global import lock just for path hooks. It seems like you are suggesting using the global import lock purely for compatibility, and what I am saying is that loaders don't care so there is no compatibility to care about. I say only use the global import lock for serializing creation.
How is this going to deal with cyclical imports where different
threads could import at the same time different modules within that
cycle? I need to look through the proposed patch and work out exactly
what it does, but am concerned about whether this approach would cause
the classic deadlock problem if not done right?
You're right, I hadn't thought about that. Additional machinery will be
needed to detect potential deadlocks (and break them).
That's my point; loaders are using the lock implicitly so that's why
we don't need to worry about the global import lock just for path
hooks. It seems like you are suggesting using the global import lock
purely for compatibility, and what I am saying is that loaders don't
care so there is no compatibility to care about. I say only use the
global import lock for serializing creation.
What is your take on the threadimp2.patch in bpo-9251?
New prototype with per-module import locks and deadlock avoidance.
When a deadlock due to threaded circular imports is detected, the offending import returns the partially constructed module object (as would happen in single-threaded mode).
Now, if x and y are executed in separate threads - won't it deadlock?
Well, the patch has a deadlock avoidance mechanism, and it includes unit
tests for precisely this situation.
I cannot promise the algorithm is perfect (although there *are* a bunch
of tests), but it looks correct from here. :)
loewis commented on May 5, 2012
loewismannequin
Mannequin
More actions
Can you please elaborate in the patch what the deadlock avoidance does? AFAICT, the comment explains that it is able to detect deadlocks, but nowhere says what it does when it has detected a deadlock.
Also, please submit patches against default's head, or stop using git-style diffs, to enable Rietveld review.
Updated patch against tip. I also changed the internal API of module locks a bit (acquire() raises _DeadlockError instead of returning False, and deadlock detection is not optional anymore).
Activity
pitrou commentedon Jul 14, 2010
This is an implementation of the idea suggested in:
http://mail.python.org/pipermail/python-dev/2003-February/033445.html
The patch creates a dictionary of reentrant locks keyed by module full name. Trying to import a module or package will first get the lock for that module (or, if necessary, create it) and then acquire it. This is done for any module type.
The global import lock is still there, but only used for two things:
Semantics of the public C API are unchanged, because it is not clear whether they should be or not (concerns of usefulness vs. compatibility). For example, PyImport_ImportModuleNoBlock() still uses the global import lock but this could be relaxed in a later patch.
brettcannon commentedon Jul 14, 2010
So I say we don't worry about loaders being thread-safe. If __import__ handles the locking for a specific module then it will hold the lock on behalf of the loader. Now if someone decides to call load_module on their own, that's there business, but they should be aware of what could happen if they do that without acquiring the lock themselves. Otherwise we just make sure to provide a context manager that takes the name of the module and people can use that when they make their call to loader.load_module.
pitrou commentedon Jul 14, 2010
Yes but what happens if two different modules are imported from two
different threads, and handled by the same loader? The loader could have
global structures which rely on serialization of imports for
consistency.
brettcannon commentedon Jul 14, 2010
On Wed, Jul 14, 2010 at 12:34, Antoine Pitrou <report@bugs.python.org>wrote:
That's why I said we should supply a context decorator (or function) which
will handle the lock appropriately, taking the name of the module to import
as an argument so the locking is fine-grained.
pitrou commentedon Jul 14, 2010
Ok, so what are you saying is that we can break compatibility for non
thread-safe import loaders which currently work fine?
(I have nothing against it, just trying to be sure we agree on the
implications)
brettcannon commentedon Jul 14, 2010
What I'm saying is that loaders are quite possibly not thread-safe already, so we don't need to do any special for them. If you look at PEP-302 you will notice not a single mention of loaders needing to care about the import lock because there is no mention of the import lock! So changing the locking mechanism most likely won't break loaders because they are not using the current import lock anyway and so already have their own issues.
As long as __import__ does the proper locking on behalf of loaders and we provide a way for people to use the lock if they want to then I am not worried about the impact on loaders. For example, this will change the logic in importlib where the current import lock is grabbed, but otherwise won't change a thing in terms of the code for the various loaders it implements.
pitrou commentedon Jul 14, 2010
Are you sure they aren't using it implicitly?
In vanilla py3k, PyImport_ImportModuleLevel() takes the import lock
therefore it protects any inner code, including the various hooks.
grahamd commentedon Jul 14, 2010
How is this going to deal with cyclical imports where different threads could import at the same time different modules within that cycle? I need to look through the proposed patch and work out exactly what it does, but am concerned about whether this approach would cause the classic deadlock problem if not done right?
FWIW, this concept of a lock per module is what I used in the mod_python module importer when it was rewritten. I would have to go look back over that code and see how the way the concept is being implemented differs, but there was one remaining potential race condition in the mod_python code which could in rare instances cause a problem. I never did get around to fixing it. Anyway, what I did learn was that this approach isn't necessarily as simple as it may seem so it will need some really good analysis on whatever solution is developed to ensure subtle problems don't come up.
brettcannon commentedon Jul 14, 2010
That's my point; loaders are using the lock implicitly so that's why we don't need to worry about the global import lock just for path hooks. It seems like you are suggesting using the global import lock purely for compatibility, and what I am saying is that loaders don't care so there is no compatibility to care about. I say only use the global import lock for serializing creation.
pitrou commentedon Jul 15, 2010
You're right, I hadn't thought about that. Additional machinery will be
needed to detect potential deadlocks (and break them).
pitrou commentedon Jul 15, 2010
What is your take on the threadimp2.patch in bpo-9251?
brettcannon commentedon Jul 15, 2010
I'll have a look when I can (hopefully EuroPython).
pitrou commentedon Dec 28, 2011
New prototype with per-module import locks and deadlock avoidance.
When a deadlock due to threaded circular imports is detected, the offending import returns the partially constructed module object (as would happen in single-threaded mode).
Probably lacks a test and some cleanup.
9 remaining items
pitrou commentedon May 5, 2012
Updated patch also adds unit tests for the module locks and the deadlock avoidance algorithm.
pitrou commentedon May 5, 2012
Updated patch with a couple new tests.
loewis commentedon May 5, 2012
I still wonder whether Graham Dumpleton's observation has merits.
Suppose we have these modules
Now, if x and y are executed in separate threads - won't it deadlock?
pitrou commentedon May 5, 2012
Well, the patch has a deadlock avoidance mechanism, and it includes unit
tests for precisely this situation.
I cannot promise the algorithm is perfect (although there *are* a bunch
of tests), but it looks correct from here. :)
loewis commentedon May 5, 2012
Can you please elaborate in the patch what the deadlock avoidance does? AFAICT, the comment explains that it is able to detect deadlocks, but nowhere says what it does when it has detected a deadlock.
Also, please submit patches against default's head, or stop using git-style diffs, to enable Rietveld review.
pitrou commentedon May 5, 2012
Updated patch against tip, and with a comment of what deadlock avoidance does (in _ModuleLock.acquire's docstring).
loewis commentedon May 5, 2012
The patch parser of Rietveld actually choked on the git binary diff. It now skips over these chunks.
pitrou commentedon May 8, 2012
Updated patch against tip. I also changed the internal API of module locks a bit (acquire() raises _DeadlockError instead of returning False, and deadlock detection is not optional anymore).
pitrou commentedon May 10, 2012
I had forgotten to tackle threadless builds, this patch fixes it.
pitrou commentedon May 13, 2012
Does anyone else want to review this patch?
brettcannon commentedon May 13, 2012
I don't feel the need to, but I can in a few days if you want me to (just let me know if you do).
python-dev commentedon May 17, 2012
New changeset edb9ce3a6c2e by Antoine Pitrou in branch 'default':
Issue bpo-9260: A finer-grained import lock.
http://hg.python.org/cpython/rev/edb9ce3a6c2e
pitrou commentedon May 17, 2012
I have now pushed the patch.