Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
2a4d4f3
Add some implementation documentation
exarkun Jun 27, 2022
c9e33c7
Move _blocking_on management into a context manager
exarkun Jun 27, 2022
c5912bf
Support re-entrant imports in _BlockingOnManager
exarkun Jun 30, 2022
27ae2f0
Apply formatting and comment changes from review
exarkun Nov 28, 2022
1fcc78b
Merge remote-tracking branch 'origin/main' into 91351-importlib-reent…
exarkun Nov 28, 2022
5fd15d8
Rename _BlockingOnManager.tid as suggested by review
exarkun Nov 28, 2022
7a24f2c
flip the first two arguments to _has_deadlock as suggested by review
exarkun Nov 28, 2022
08892b4
Mark up parameters following PEP 257 as suggested by review
exarkun Nov 28, 2022
59b53c0
rename the `_blocking_on` parameter as suggested by review
exarkun Nov 28, 2022
20007c5
further document motivation for `blocking_on` parameter as suggested …
exarkun Nov 28, 2022
bad1d3c
Rename more _has_deadlocked parameters as suggested by review
exarkun Nov 28, 2022
2821fcf
Treat None and [] the same for this case as suggested by review
exarkun Nov 28, 2022
95c73cb
update old comment to refer to new names as suggested by review
exarkun Nov 28, 2022
49ff9dd
Make _ModuleLock.count a list of True as suggested by review
exarkun Nov 28, 2022
cd174a8
Adjust the check for a module lock being released as suggest by review
exarkun Nov 28, 2022
719b181
Finish the _BlockingOnManager.tid renaming
exarkun Nov 28, 2022
6e809cd
Fix renaming of `_blocking_on` parameter to `_has_deadlocked`
exarkun Nov 28, 2022
74cbccd
Apply review suggestion
exarkun Jan 6, 2023
c579533
Apply review suggestion
exarkun Jan 6, 2023
decb70b
Apply review suggestion
exarkun Jan 6, 2023
3c91cc3
Apply review suggestion
exarkun Jan 6, 2023
e032ae2
Apply review suggestion
exarkun Jan 6, 2023
dba393a
Apply review suggestion
exarkun Jan 6, 2023
25d554b
Apply review suggestion
exarkun Jan 6, 2023
44157a9
Apply review suggestion
exarkun Jan 6, 2023
f99ed46
Apply review suggestion
exarkun Jan 6, 2023
b6d21f8
Apply review suggestion
exarkun Jan 6, 2023
5643442
Apply review suggestion
exarkun Jan 6, 2023
92036a8
Apply review suggestion
exarkun Jan 6, 2023
bf14ce2
Apply review suggestion
exarkun Jan 6, 2023
1cc6033
Apply review suggestion
exarkun Jan 6, 2023
ab40737
Apply review suggestion
exarkun Jan 6, 2023
36082eb
news blurb
exarkun Jan 6, 2023
55fc599
Merge branch 'main' into 91351-importlib-reentrancy
exarkun Jan 6, 2023
b35f0a8
Apply suggestions from code review
exarkun Jan 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename more _has_deadlocked parameters as suggested by review
  • Loading branch information
exarkun committed Nov 28, 2022
commit bad1d3c5e703d8fe0490076ddcce2f518ee84470
43 changes: 22 additions & 21 deletions Lib/importlib/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,45 +92,46 @@ class _DeadlockError(RuntimeError):



def _has_deadlocked(subject, seen, tids, blocking_on):
"""Check if 'subject' is holding the same lock as another thread(s).
def _has_deadlocked(target_id, seen_ids, candidate_ids, blocking_on):
Comment thread
exarkun marked this conversation as resolved.
Outdated
"""Check if 'target_id' is holding the same lock as another thread(s).

The search within blocking_on starts with the threads listed in tids.
'seen' contains any threads that are considered already traversed in the search.
The search within 'blocking_on' starts with the threads listed in
'candidate_ids'. 'seen_ids' contains any threads that are considered
already traversed in the search.

Keyword arguments:
subject -- The thread id to try to reach.
seen -- A set of threads that have already been visited.
tids -- The thread ids from which to begin.
blocking_on -- A dict representing the thread/blocking-on graph.
This may be the same object as the global '_blocking_on'
but it is a parameter to reduce the impact that global
mutable state has on the result of this function.
target_id -- The thread id to try to reach.
seen_ids -- A set of threads that have already been visited.
candidate_ids -- The thread ids from which to begin.
blocking_on -- A dict representing the thread/blocking-on graph. This may
be the same object as the global '_blocking_on' but it is
a parameter to reduce the impact that global mutable
state has on the result of this function.
"""
if subject in tids:
# If we have already reached the subject, we're done - signal that it
if target_id in candidate_ids:
# If we have already reached the target_id, we're done - signal that it
# is reachable.
return True

# Otherwise, try to reach the subject from each of the given tids.
for tid in tids:
# Otherwise, try to reach the target_id from each of the given candidate_ids.
for tid in candidate_ids:
blocking_on = blocking_on.get(tid)
if blocking_on is None:
Comment thread
exarkun marked this conversation as resolved.
Outdated
# There are no edges out from this node, skip it.
continue

if tid in seen:
if tid in seen_ids:
# bpo 38091: the chain of tid's we encounter here
# eventually leads to a fixed point or a cycle, but
# does not reach 'me'. This means we would not
Comment thread
exarkun marked this conversation as resolved.
Outdated
# actually deadlock. This can happen if other
# threads are at the beginning of acquire() below.
return False
seen.add(tid)
seen_ids.add(tid)

# Follow the edges out from this thread.
edges = [lock.owner for lock in blocking_on]
if _has_deadlocked(subject, seen, edges, blocking_on):
if _has_deadlocked(target_id, seen_ids, edges, blocking_on):
Comment thread
exarkun marked this conversation as resolved.
Outdated
return True

return False
Expand Down Expand Up @@ -205,11 +206,11 @@ def has_deadlock(self):
# is held by this thread.
return _has_deadlocked(
# Try to find this thread
Comment thread
exarkun marked this conversation as resolved.
Outdated
subject=_thread.get_ident(),
seen=set(),
target_id=_thread.get_ident(),
seen_ids=set(),
# starting from the thread that holds the import lock for this
# module.
tids=[self.owner],
candidate_ids=[self.owner],
# using the global "blocking on" state.
Comment thread
exarkun marked this conversation as resolved.
Outdated
blocking_on=_blocking_on,
)
Expand Down