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

Process time out returns incorrect exception in pythonsdk e2b 1476 #550

Merged
Show file tree
Hide file tree
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
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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure the incorrect exception originated from here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a test, you can try. It's from the Generator, we have the same handling in watch handle

Without the try/except block, you can see it's raised in connect library in the generator
image

raise handle_rpc_exception(e)

def disconnect(self) -> None:
"""
Expand Down
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
Loading