Skip to content

Commit

Permalink
WaitIterator: don't re-use _running_future
Browse files Browse the repository at this point in the history
When used with asyncio.Future, WaitIterator may skip indices in some
cases. This is caused by multiple _return_result calls after another,
without having the chain_future call finish in between. This is fixed
here by not hanging on to the _running_future anymore, which forces
subsequent _return_result calls to add to _finished, instead of causing
the previous result to be silently dropped.

Fixes tornadoweb#2034
  • Loading branch information
sk1p authored and jeyrce committed Aug 25, 2021
1 parent 7a83aed commit d11daaa
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion tornado/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def next(self) -> Future:
self._running_future = Future()

if self._finished:
self._return_result(self._finished.popleft())
return self._return_result(self._finished.popleft())

return self._running_future

Expand All @@ -418,9 +418,13 @@ def _return_result(self, done: Future) -> None:
raise Exception("no future is running")
chain_future(done, self._running_future)

res = self._running_future
self._running_future = None
self.current_future = done
self.current_index = self._unfinished.pop(done)

return res

def __aiter__(self) -> typing.AsyncIterator:
return self

Expand Down

0 comments on commit d11daaa

Please sign in to comment.