Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG Account for other Possible unpickling errors #13

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions lute/execution/ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def read(self, proc: subprocess.Popen) -> Message:
if self._use_pickle:
try:
contents = pickle.loads(raw_contents)
except pickle.UnpicklingError as err:
except (pickle.UnpicklingError, ValueError, EOFError) as err:
logger.debug("PipeCommunicator (Executor) - Set _use_pickle=False")
self._use_pickle = False
contents = self._safe_unpickle_decode(raw_contents)
Expand All @@ -183,7 +183,7 @@ def read(self, proc: subprocess.Popen) -> Message:
contents = raw_contents.decode()
except UnicodeDecodeError as err:
logger.debug("PipeCommunicator (Executor) - Set _use_pickle=True")
# self._use_pickle = True
self._use_pickle = True
contents = self._safe_unpickle_decode(raw_contents)
else:
contents = None
Expand Down Expand Up @@ -237,9 +237,18 @@ def _safe_unpickle_decode(self, maybe_mixed: bytes) -> Optional[str]:
repickled: bytes = pickle.dumps(contents)
if len(repickled) < len(maybe_mixed):
# Successful unpickling, but pickle stops even if there are more bytes
additional_data: str = maybe_mixed[len(repickled) :].decode()
contents = f"{contents}{additional_data}"
except pickle.UnpicklingError as err:
try:
additional_data: str = maybe_mixed[len(repickled) :].decode()
contents = f"{contents}{additional_data}"
except UnicodeDecodeError:
# Can't decode the bytes left by pickle, so they are lost
missing_bytes: int = len(maybe_mixed) - len(repickled)
logger.debug(
f"PipeCommunicator has truncated message. Unable to retrieve {missing_bytes} bytes."
)
except (pickle.UnpicklingError, ValueError, EOFError) as err:
# Pickle may also throw a ValueError, e.g. this bytes: b"Found! \n"
# Pickle may also throw an EOFError, eg. this bytes: b"F0\n"
try:
contents = maybe_mixed.decode()
except UnicodeDecodeError as err2:
Expand All @@ -251,11 +260,6 @@ def _safe_unpickle_decode(self, maybe_mixed: bytes) -> Optional[str]:
f"PipeCommunicator unable to decode/parse data! {err3}"
)
contents = None
except UnicodeDecodeError as err3:
missing_bytes: int = len(maybe_mixed) - len(repickled)
logger.debug(
f"PipeCommunicator has truncated message. Unable to retrieve {missing_bytes} bytes."
)
return contents

def write(self, msg: Message) -> None:
Expand Down
Loading