Skip to content

Commit d46dec9

Browse files
miss-islingtonvsajip
authored andcommitted
bpo-39198: Ensure logging global lock is released on exception in isEnabledFor (GH-17689) (GH-17898)
(cherry picked from commit 950c679)
1 parent 72995c5 commit d46dec9

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

Lib/logging/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,12 +1619,15 @@ def isEnabledFor(self, level):
16191619
return self._cache[level]
16201620
except KeyError:
16211621
_acquireLock()
1622-
if self.manager.disable >= level:
1623-
is_enabled = self._cache[level] = False
1624-
else:
1625-
is_enabled = self._cache[level] = level >= self.getEffectiveLevel()
1626-
_releaseLock()
1627-
1622+
try:
1623+
if self.manager.disable >= level:
1624+
is_enabled = self._cache[level] = False
1625+
else:
1626+
is_enabled = self._cache[level] = (
1627+
level >= self.getEffectiveLevel()
1628+
)
1629+
finally:
1630+
_releaseLock()
16281631
return is_enabled
16291632

16301633
def getChild(self, suffix):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
If an exception were to be thrown in `Logger.isEnabledFor` (say, by asyncio timeouts or stopit) , the `logging` global lock may not be released appropriately, resulting in deadlock. This change wraps that block of code with `try...finally` to ensure the lock is released.

0 commit comments

Comments
 (0)