This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: imp.reload() doesn't take import lock
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.1, Python 3.2, Python 2.7, Python 2.6
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, eric.snow, ncoghlan, pitrou, roger.serwy
Priority: normal Keywords:

Created on 2010-07-13 13:38 by pitrou, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg110191 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-07-13 13:38
I am not sure this is important or now, but reload() (imp.reload() in 3.x) doesn't take the import lock when reloading:

$ echo 'import imp; print("lock held =", imp.lock_held())' > foo.py
$ ./python -c 'import imp, foo; imp.reload(foo)'
lock held = True
lock held = False
msg110218 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2010-07-13 19:00
So the import lock is to prevent trying to import the same module, right? If you are doing a reload, the module is basically already there. But what if you context switch while reloading? That would be bad as that would give you inconsistent state.

So it probably should hold the lock (unless I am off about what the import lock should truly be used for).
msg162433 - (view) Author: Roger Serwy (roger.serwy) * (Python committer) Date: 2012-06-07 00:26
I just ran Antoine's test against the latest 3.3a4 build and received this:

lock held = False
lock held = False

Is this issue still relevant given Brett's work on re-implementing import in pure Python?
msg162445 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2012-06-07 01:37
Yeah, imp.reload() is pure Python now (Lib/imp.py), a simple wrapper around module.__loader__.load_module(), which does not make use of any import locks.  Having it do so, however, may not be feasible as I'd expect it to mean having load_module() handle the locking.  Not sure if it's worth it.

The other issue is that the True reported by Antoine turned into False for you under 3.3a4.  This is because of issue 9260 ("A finer grained import lock").  The global import lock represented in the imp module is now used only long enough to create a lock specific to the module currently being imported.  So it's been released by the time the module is actually imported.  (see Lib/importlib/_bootstrap.py)
msg162941 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2012-06-15 23:41
We have gone this long without a lock for reload(), I don't see a reason to start caring now.
History
Date User Action Args
2022-04-11 14:57:03adminsetgithub: 53493
2012-06-15 23:41:42brett.cannonsetstatus: open -> closed
resolution: works for me
messages: + msg162941
2012-06-07 01:37:47eric.snowsetstatus: pending -> open
nosy: + eric.snow
messages: + msg162445

2012-06-07 00:26:20roger.serwysetstatus: open -> pending
nosy: + roger.serwy
messages: + msg162433

2010-07-13 19:00:00brett.cannonsetmessages: + msg110218
2010-07-13 13:38:52pitroucreate