Skip to main content

Sessions

Sessions represent a persistent connection to a sandbox. All code executions within a session share the same VM, filesystem, and process state.

How sessions work

When you create an InstaVM client and call execute(), a session is automatically created. Subsequent calls to execute() on the same client reuse the same session, which means:

  • Variables, files, and installed packages persist between calls
  • Each call runs in the same VM
  • The session ends when you call kill(), exit a context manager, or the lifetime expires
# SSH provides a persistent session — state persists across commands
instavm create
instavm connect <vm_id>
# Inside the VM:
# python3 -c "x = 42; print(x)"

Session lifecycle

create session → VM boots (~200ms) → execute code → ... → terminate
  1. Created -- Session allocated, VM provisioning begins.
  2. Active -- VM is running. You can execute code, upload/download files, and manage egress.
  3. Terminated -- Session closed. VM and all its state are destroyed.

Automatic session management

Use a context manager to guarantee cleanup:

# Create with a timeout — VM auto-terminates when it expires
instavm create --timeout 3600
# ... use the VM ...
instavm rm <vm_id>

Manual session management

If you need more control:

# List active VMs and their session status
instavm ls --json

Session expiry

Sessions have a configurable lifetime. When the lifetime expires, the VM is terminated and all state is lost. Set the lifetime when creating a VM directly:

instavm create --timeout 3600

Multi-execute pattern

Sessions are designed for multi-step workflows. Install dependencies once, then run multiple scripts:

# Connect via SSH for interactive multi-step workflows
instavm create
instavm connect <vm_id>
# Inside the VM:
# pip install pandas
# python3 -c "import pandas as pd; df = pd.DataFrame({'a': [1,2,3]}); print(df.describe())"

Session info

Query the current session:

info = vm.get_session_info()
# Returns: {'session_id': '...', 'status': 'active', ...}

usage = vm.get_usage()
# Returns: {'cpu_time': ..., 'memory_used': ..., 'network_io': ...}

Next steps