From 99092a8c6d4b411645ac4b84d504e5226e7eebb8 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 22 Jan 2021 12:34:27 +0100 Subject: [PATCH] Fixup for previous commit. Lock implementation in ThreadedHistory was not correct. --- prompt_toolkit/history.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/prompt_toolkit/history.py b/prompt_toolkit/history.py index 0179c4ff1..5c1caa7af 100644 --- a/prompt_toolkit/history.py +++ b/prompt_toolkit/history.py @@ -12,7 +12,7 @@ import os import threading from abc import ABCMeta, abstractmethod -from typing import AsyncGenerator, Iterable, List, Optional, Sequence +from typing import AsyncGenerator, Iterable, List, Optional, Sequence, Tuple __all__ = [ "History", @@ -158,18 +158,14 @@ async def load(self) -> AsyncGenerator[str, None]: continue # Read new items (in lock). - # (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() - finally: - self._lock.release() + def in_executor() -> Tuple[List[str], bool]: + with self._lock: + new_items = self._loaded_strings[items_yielded:] + done = self._loaded + event.clear() + return new_items, done + + new_items, done = await loop.run_in_executor(None, in_executor) items_yielded += len(new_items)