-
Notifications
You must be signed in to change notification settings - Fork 739
Add PTY reconnect #1053
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
Merged
Merged
Add PTY reconnect #1053
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
41ad11e
[WIP] Add pty reconnect
ValentaTomas 993d81c
[WIP] Add pty reconnect
ValentaTomas 9fd98e1
Merge branch 'pty-reconnect' of https://github.com/e2b-dev/E2B into p…
ValentaTomas ff2ea44
WIP
ValentaTomas b0f09f3
Fix lint
ValentaTomas c2ed51e
Fix lint
ValentaTomas ea00ddd
on_data should not be optional
mishushakov 3c02858
updated ts test
mishushakov 32beeec
updated python tests
mishushakov f7f80a6
fixes sync python tests
mishushakov e8df6f8
added changeset
mishushakov 539c4f5
lint
mishushakov 0e3f245
Merge branch 'main' into pty-reconnect
mishushakov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@e2b/python-sdk': minor | ||
| 'e2b': minor | ||
| --- | ||
|
|
||
| added option to connect to a running pty session |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { sandboxTest } from '../../setup' | ||
| import { assert } from 'vitest' | ||
|
|
||
| sandboxTest('pty connect/reconnect', async ({ sandbox }) => { | ||
| let output1 = '' | ||
| let output2 = '' | ||
| const decoder = new TextDecoder() | ||
|
|
||
| // First, create a terminal and disconnect the onData handler | ||
| const terminal = await sandbox.pty.create({ | ||
| cols: 80, | ||
| rows: 24, | ||
| onData: (data: Uint8Array) => { | ||
| output1 += decoder.decode(data) | ||
| }, | ||
| envs: { FOO: 'bar' }, | ||
| }) | ||
|
|
||
| await sandbox.pty.sendInput( | ||
| terminal.pid, | ||
| new Uint8Array(Buffer.from('echo $FOO\n')) | ||
| ) | ||
|
|
||
| // Give time for the command output in the first connection | ||
| await new Promise((r) => setTimeout(r, 300)) | ||
|
|
||
| await terminal.disconnect() | ||
|
|
||
| // Now connect again, with a new onData handler | ||
| const reconnectHandle = await sandbox.pty.connect(terminal.pid, { | ||
| onData: (data: Uint8Array) => { | ||
| output2 += decoder.decode(data) | ||
| }, | ||
| }) | ||
|
|
||
| await sandbox.pty.sendInput( | ||
| terminal.pid, | ||
| new Uint8Array(Buffer.from('echo $FOO\nexit\n')) | ||
| ) | ||
|
|
||
| await reconnectHandle.wait() | ||
|
|
||
| assert.equal(terminal.pid, reconnectHandle.pid) | ||
| assert.equal(reconnectHandle.exitCode, 0) | ||
|
|
||
| assert.include(output1, 'bar') | ||
| assert.include(output2, 'bar') | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/python-sdk/tests/async/sandbox_async/pty/test_pty_connect.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import asyncio | ||
|
|
||
| from e2b import AsyncSandbox | ||
| from e2b.sandbox.commands.command_handle import PtySize | ||
|
|
||
|
|
||
| async def test_connect_to_pty(async_sandbox: AsyncSandbox): | ||
| output1 = [] | ||
| output2 = [] | ||
|
|
||
| def append_data(data: list, x: bytes): | ||
| data.append(x.decode("utf-8")) | ||
|
|
||
| # First, create a terminal and disconnect the on_data handler | ||
| terminal = await async_sandbox.pty.create( | ||
| PtySize(80, 24), | ||
| on_data=lambda x: append_data(output1, x), | ||
| envs={"FOO": "bar"}, | ||
| ) | ||
|
|
||
| await async_sandbox.pty.send_stdin(terminal.pid, b"echo $FOO\n") | ||
|
|
||
| # Give time for the command output in the first connection | ||
| await asyncio.sleep(0.3) | ||
|
|
||
| await terminal.disconnect() | ||
|
|
||
| # Now connect again, with a new on_data handler | ||
| reconnect_handle = await async_sandbox.pty.connect( | ||
| terminal.pid, on_data=lambda x: append_data(output2, x) | ||
| ) | ||
|
|
||
| await async_sandbox.pty.send_stdin(terminal.pid, b"echo $FOO\nexit\n") | ||
|
|
||
| await reconnect_handle.wait() | ||
|
|
||
| assert terminal.pid == reconnect_handle.pid | ||
| assert reconnect_handle.exit_code == 0 | ||
|
|
||
| assert "bar" in "".join(output1) | ||
| assert "bar" in "".join(output2) |
30 changes: 30 additions & 0 deletions
30
packages/python-sdk/tests/sync/sandbox_sync/pty/test_pty_connect.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import pytest | ||
|
|
||
| from e2b.sandbox.commands.command_handle import PtySize | ||
|
|
||
|
|
||
| @pytest.mark.skip_debug() | ||
| def test_connect_to_pty(sandbox_factory): | ||
| sandbox = sandbox_factory(timeout=100) | ||
| output = [] | ||
|
|
||
| def append_data(data: list, x: bytes): | ||
| data.append(x.decode("utf-8")) | ||
|
|
||
| terminal = sandbox.pty.create(PtySize(80, 24), envs={"FOO": "bar"}) | ||
|
|
||
| sandbox.pty.send_stdin(terminal.pid, b"echo $FOO\n") | ||
|
|
||
| terminal.disconnect() | ||
|
|
||
| # Now connect again, with a new on_pty handler | ||
| reconnect_handle = sandbox.pty.connect(terminal.pid) | ||
|
|
||
| sandbox.pty.send_stdin(terminal.pid, b"echo $FOO\nexit\n") | ||
|
|
||
| result = reconnect_handle.wait(on_pty=lambda x: append_data(output, x)) | ||
|
|
||
| assert terminal.pid == reconnect_handle.pid | ||
| assert result.exit_code == 0 | ||
|
|
||
| assert "bar" in "".join(output) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.