Unable to reconnect to sandboxes `WebSocket failed to start`
Describe the bug Sandboxes failing to reconnect via python client.
To Reproduce
from e2b import Sandbox
boxes = Sandbox.list()
print([f"{b.sandbox_id}-{b.client_id}" for b in boxes])
for b in boxes:
id = f"{b.sandbox_id}-{b.client_id}"
print(f"Attempting to close: {id}")
try:
s = Sandbox.reconnect(id, timeout=10)
s.close()
del s
except Exception as e:
print(e)
print('Failed to reconnect to', id)
continue
Output:
All result in websocket errors or 404 errors (404 is probably fine, I assume the sandbox timed out naturally before I reached it).
['ifp8vcv9053yly3i41u6y-4ce3e393', 'i6cyc8zwzb8f2kkjwhhrr-4ce3e393', 'idd0if6yc2g9gy4oxbgxm-4ce3e393', 'ia1aqgm6usdu17mb2uh14-4ce3e393', 'i9du80g3wt829jgzg2hea-4ce3e393', 'iwqjv9qtfby61jxu3e2tv-4ce3e393', 'ik3n4rel2uudt2fxxed45-4ce3e393', 'i3r0jmx6v4wm222knnll3-4ce3e393', 'iav7kto44sij8kkmb1zwu-4ce3e393', 'io2xdqy7deqnxzgdf7wcd-4ce3e393', 'i2rgtmhqs82nb5nll4i0d-4ce3e393', 'ifssu9ydior6nb8e1j628-4ce3e393', 'it5mt2eh9u1qguek68k0c-4ce3e393', 'it7u42tmwccjzi3ohuryn-4ce3e393']
Attempting to close: ia1aqgm6usdu17mb2uh14-4ce3e393
WebSocket failed to start
WebSocket failed to start: WebSocket failed to start
WebSocket failed to start: WebSocket failed to start
Failed to reconnect to ia1aqgm6usdu17mb2uh14-4ce3e393
Attempting to close: idd0if6yc2g9gy4oxbgxm-4ce3e393
WebSocket failed to start
WebSocket failed to start: WebSocket failed to start
WebSocket failed to start: WebSocket failed to start
Failed to reconnect to idd0if6yc2g9gy4oxbgxm-4ce3e393
Attempting to close: i6cyc8zwzb8f2kkjwhhrr-4ce3e393
e2b.api.v1.client.exceptions.NotFoundException: (404)
Reason: Not Found
HTTP response headers: HTTPHeaderDict({'content-type': 'application/json; charset=utf-8', 'date': 'Thu, 08 Feb 2024 19:43:35 GMT', 'Content-Length': '97', 'via': '1.1 google', 'Alt-Svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000'})
HTTP response body: {"code":404,"message":"Error refreshing sandbox - sandbox 'ifssu9ydior6nb8e1j628' was not found"}
...
Expected behavior All of my existing sandboxes should be closed, but instead, many fail to reconnect.
System stats: pythonsdk-e2b 14.0.1
Hey @KastanDay, I'll check this out and try to reproduce it.
@KastanDay Can you give me a little more context about the existing sandboxes?
Were any of them being used in some running code a the time you were trying to close them?
Are you using keep_alive anywhere?
By the way, why do you want to close all existing sandboxes?
Thanks for the support @ValentaTomas
- I don't think they were running any code at the time I was trying to close them, unless maybe I had a background process hanging.
- I did use keepalive. The first few had an hour, then I reduced it to 2 minutes.
sandbox.keep_alive(2 * 60) # 2 minutes for now.andsandbox.keep_alive(60 * 60 * 1) # 1 hour max - I was trying to close them because I was hitting the 20 simultaneous boxes limit, and I just didn’t want to waste your server budget.
Now the next day, I still have 5 sandboxes running that are unnecessary.
❯ e2b sandboxes
Running sandboxes
┌────────────────────────────────┬──────────────────────┬───────┬──────────────────────┬──────────┐
│ Sandbox ID │ Template ID │ Alias │ Started at │ Metadata │
├────────────────────────────────┼──────────────────────┼───────┼──────────────────────┼──────────┤
│ it5mt2eh9u1qguek68k0c-4ce3e393 │ rki5dems9wqfm4r03t7g │ base │ 2/8/2024, 2:33:18 PM │ │
│ iav7kto44sij8kkmb1zwu-4ce3e393 │ rki5dems9wqfm4r03t7g │ base │ 2/8/2024, 2:33:18 PM │ │
│ idd0if6yc2g9gy4oxbgxm-4ce3e393 │ rki5dems9wqfm4r03t7g │ base │ 2/8/2024, 2:33:18 PM │ │
│ ia1aqgm6usdu17mb2uh14-4ce3e393 │ rki5dems9wqfm4r03t7g │ base │ 2/8/2024, 2:33:18 PM │ │
│ i6cyc8zwzb8f2kkjwhhrr-4ce3e393 │ rki5dems9wqfm4r03t7g │ base │ 2/8/2024, 2:33:18 PM │ │
└────────────────────────────────┴──────────────────────┴───────┴──────────────────────┴──────────┘
Thanks again for a very useful product.
I don't think they were running any code at the time I was trying to close them, unless maybe I had a background process hanging.
Sorry I think my description wasn't clear here — I wanted to ask if there was some code of yours (server, script) where the Sandbox object still existed. When you create a sandbox (sandbox = Sandbox()) it spawns a thread in the background that will ping the sandbox to keep it alive for the duration of the object's existence or until you call sandbox.close.
Thanks again for a very useful product.
Great to hear this! Can I ask what are you using the sandboxes for in your product?
We now have a new beta SDK (https://e2b.dev/docs/guide/beta-migration) that is not using websockets at all — this bug should be fixed by it.