Skip to content

Commit

Permalink
Fix race condition in ThreadedHistory.
Browse files Browse the repository at this point in the history
The Lock in `ThreadedHistory` was not always properly released, and because of
that, in situations where the user was pasting enormous amounts of text, the
application could freeze at the point where lines were added to the history.
  • Loading branch information
jonathanslenders committed Jan 22, 2021
1 parent e58bd7c commit d5e58a8
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion prompt_toolkit/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,13 @@ async def load(self) -> AsyncGenerator[str, None]:
continue

# Read new items (in lock).
await loop.run_in_executor(None, self._lock.acquire)
# (Important: acquiring the lock should happen *in* the try
# block. Otherwise it's not guaranteed it will ever be
# released. This can happen when this coroutine is cancelled at
# an "await" point. This did actually happen when continuously
# pasting huge amounts of text in ptpython.)
try:
await loop.run_in_executor(None, self._lock.acquire)
new_items = self._loaded_strings[items_yielded:]
done = self._loaded
event.clear()
Expand Down

0 comments on commit d5e58a8

Please sign in to comment.