Skip to content

Commit

Permalink
Process time out returns incorrect exception in pythonsdk e2b 1476 (#550
Browse files Browse the repository at this point in the history
)

Handle the exception properly
  • Loading branch information
jakubno authored Jan 28, 2025
2 parents 4fb4470 + ae61d83 commit fe7ed15
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
41 changes: 22 additions & 19 deletions packages/python-sdk/e2b/sandbox_sync/commands/command_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,28 @@ def _handle_events(
None,
None,
]:
for event in self._events:
if event.event.HasField("data"):
if event.event.data.stdout:
out = event.event.data.stdout.decode('utf-8', 'replace')
self._stdout += out
yield out, None, None
if event.event.data.stderr:
out = event.event.data.stderr.decode('utf-8', 'replace')
self._stderr += out
yield None, out, None
if event.event.data.pty:
yield None, None, event.event.data.pty
if event.event.HasField("end"):
self._result = CommandResult(
stdout=self._stdout,
stderr=self._stderr,
exit_code=event.event.end.exit_code,
error=event.event.end.error,
)
try:
for event in self._events:
if event.event.HasField("data"):
if event.event.data.stdout:
out = event.event.data.stdout.decode("utf-8", "replace")
self._stdout += out
yield out, None, None
if event.event.data.stderr:
out = event.event.data.stderr.decode("utf-8", "replace")
self._stderr += out
yield None, out, None
if event.event.data.pty:
yield None, None, event.event.data.pty
if event.event.HasField("end"):
self._result = CommandResult(
stdout=self._stdout,
stderr=self._stderr,
exit_code=event.event.end.exit_code,
error=event.event.end.error,
)
except Exception as e:
raise handle_rpc_exception(e)

def disconnect(self) -> None:
"""
Expand Down
13 changes: 11 additions & 2 deletions packages/python-sdk/tests/sync/sandbox_sync/commands/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ def test_run_with_special_characters(sandbox: Sandbox):
assert cmd.exit_code == 0
assert cmd.stdout == f"{text}\n"


def test_run_with_broken_utf8(sandbox: Sandbox):
# Create a string with 8191 'a' characters followed by the problematic byte 0xe2
long_str = 'a' * 8191 + '\\xe2'
long_str = "a" * 8191 + "\\xe2"
result = sandbox.commands.run(f'printf "{long_str}"')
assert result.exit_code == 0

# The broken UTF-8 bytes should be replaced with the Unicode replacement character
assert result.stdout == ('a' * 8191 + '\ufffd')
assert result.stdout == ("a" * 8191 + "\ufffd")


def test_run_with_multiline_string(sandbox):
text = "Hello,\nWorld!"
Expand All @@ -47,3 +49,10 @@ def test_run_with_timeout(sandbox):
def test_run_with_too_short_timeout(sandbox):
with pytest.raises(TimeoutException):
sandbox.commands.run("sleep 10", timeout=2)


def test_run_with_too_short_timeout_iterating(sandbox):
cmd = sandbox.commands.run("sleep 10", timeout=2, background=True)
with pytest.raises(TimeoutException):
for _ in cmd:
pass

0 comments on commit fe7ed15

Please sign in to comment.