From cf5471fa5a9a4ae7a092467a4e4cd28214ff3724 Mon Sep 17 00:00:00 2001 From: Apeksha_2022 <70309060+Apeksha9389@users.noreply.github.com> Date: Tue, 20 Oct 2026 17:10:05 +0530 Subject: [PATCH] fix(template): recreate deleted default language contexts --- .../recreate-deleted-default-context.md | 5 +++ template/server/main.py | 6 ++-- tests/test_contexts.py | 33 +++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 .changeset/recreate-deleted-default-context.md diff --git a/.changeset/recreate-deleted-default-context.md b/.changeset/recreate-deleted-default-context.md new file mode 100644 index 00000000..2e785ab0 --- /dev/null +++ b/.changeset/recreate-deleted-default-context.md @@ -0,0 +1,5 @@ +--- +'@e2b/code-interpreter-template': patch +--- + +Recreate a deleted default language context on the next execution, including implicit Python executions. diff --git a/template/server/main.py b/template/server/main.py index a8d43b74..3617e3c7 100644 --- a/template/server/main.py +++ b/template/server/main.py @@ -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" @@ -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 diff --git a/tests/test_contexts.py b/tests/test_contexts.py index 8c2c4764..a9a41123 100644 --- a/tests/test_contexts.py +++ b/tests/test_contexts.py @@ -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"