Skip to content

Commit ca7fe77

Browse files
committed
fix Python implementation
1 parent 016442c commit ca7fe77

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

Lib/asyncio/futures.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,11 @@ def remove_done_callback(self, fn):
234234
235235
Returns the number of callbacks removed.
236236
"""
237+
count = len(self._callbacks) # since filtering may mutate the list
237238
filtered_callbacks = [(f, ctx)
238239
for (f, ctx) in self._callbacks
239240
if f != fn]
240-
removed_count = len(self._callbacks) - len(filtered_callbacks)
241+
removed_count = count - len(filtered_callbacks)
241242
if removed_count:
242243
self._callbacks[:] = filtered_callbacks
243244
return removed_count

Lib/test/test_asyncio/test_futures.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,7 @@ def test_use_after_free_fixed(self):
933933
# Special thanks to Nico-Posada for the original PoC.
934934
# See https://github.com/python/cpython/issues/125789.
935935

936+
asserter = self
936937
fut = self._new_future()
937938

938939
class cb_pad:
@@ -941,11 +942,13 @@ def __eq__(self, other):
941942

942943
class evil(cb_pad):
943944
def __eq__(self, other):
944-
fut.remove_done_callback(None)
945+
removed = fut.remove_done_callback(None)
946+
asserter.assertEqual(removed, 1)
945947
return NotImplemented
946948

947949
fut.add_done_callback(cb_pad())
948-
fut.remove_done_callback(evil())
950+
removed = fut.remove_done_callback(evil())
951+
self.assertEqual(removed, 1)
949952

950953

951954
@unittest.skipUnless(hasattr(futures, '_CFuture'),

0 commit comments

Comments
 (0)