Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/recreate-deleted-default-context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@e2b/code-interpreter-template': patch
---

Recreate a deleted default language context on the next execution, including implicit Python executions.
6 changes: 4 additions & 2 deletions template/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ async def post_execute(request: Request, exec_request: ExecutionRequest):
)

context_id = None
if exec_request.language:
if exec_request.language or not exec_request.context_id:
language = normalize_language(exec_request.language)

async with await default_websockets.get_lock(language):
context_id = default_websockets.get(language)

if not context_id:
if not context_id or context_id not in websockets:
try:
context = await create_context(
client, websockets, language, "/home/user"
Expand All @@ -105,6 +105,8 @@ async def post_execute(request: Request, exec_request: ExecutionRequest):

context_id = context.id
default_websockets[language] = context_id
if language == "python":
websockets["default"] = websockets[context_id]

elif exec_request.context_id:
context_id = exec_request.context_id
Expand Down
33 changes: 33 additions & 0 deletions tests/test_contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,36 @@ def test_contexts_secure_traffic(client_factory):

client.remove_context(context["id"])
assert context["id"] not in [ctx["id"] for ctx in client.list_contexts()]


def test_execute_default_context_after_restart(client: CodeInterpreter):
context = next(ctx for ctx in client.list_contexts() if ctx["language"] == "python")
client.run_code("restart_marker = 42")
client.restart_context(context["id"])

execution = client.run_code("restart_marker")

assert execution.error is not None
assert execution.error.name == "NameError"
assert client.run_code("1 + 1").text == "2"


def test_restart_other_context_preserves_default_context(client: CodeInterpreter):
client.run_code("default_marker = 42")
context = client.create_context()
client.run_code("other_marker = 7", context_id=context["id"])

client.restart_context(context["id"])

assert client.run_code("default_marker").text == "42"
execution = client.run_code("other_marker", context_id=context["id"])
assert execution.error is not None
assert execution.error.name == "NameError"


@pytest.mark.parametrize("options", [{}, {"language": "python"}])
def test_execute_recreates_removed_default_context(client: CodeInterpreter, options):
context = next(ctx for ctx in client.list_contexts() if ctx["language"] == "python")
client.remove_context(context["id"])

assert client.run_code("1 + 1", **options).text == "2"