Skip to content

Commit a03483d

Browse files
committed
Keep pop(args, None) for simplicity in multi-threaded environments
See the comment at https://github.com/python/cpython/pull/107613/files#r1284119710 and #107421 (comment) for more information.
1 parent 77f10b2 commit a03483d

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

Doc/library/collections.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,8 +1259,12 @@ variants of :func:`functools.lru_cache`:
12591259
self.requests.popitem(last=False)
12601260
else:
12611261
# no longer need to keep track of how many times this
1262-
# entry has been seen
1263-
self.requests.pop(args)
1262+
# entry has been seen; in a single-threaded environment,
1263+
# the `requests` OrderedDict will always contain the value
1264+
# being removed, but use `pop(args, None)` to reduce
1265+
# complications in a multi-threaded environment where
1266+
# a race condition may cause an exception
1267+
self.requests.pop(args, None)
12641268
if len(self.cache) == self.maxsize:
12651269
self.cache.popitem(last=False)
12661270
self.cache[args] = result

0 commit comments

Comments
 (0)