Colab long-running cell: graceful shutdown on kernel interrupt and idle timeout
0 reputation · 22 Apr 2022, 13:38 UTC
0 reputation · 22 Apr 2022, 13:38 UTC
Google Colab disconnects idle sessions after roughly 90 minutes, and the only way to stop a running cell is the Stop button (kernel interrupt), which abruptly terminates code with no guaranteed cleanup. I want to handle cancellation gracefully in a long-running training cell.
The interrupt raises KeyboardInterrupt in the cell, but I am unsure how to structure the code so that cleanup — like closing file handles, checkpointing model state, and releasing GPU memory — runs reliably before the kernel stops. I also do not know whether the interrupt timing interacts with the idle disconnect timer, or whether autosave can be relied upon to preserve enough state to resume after a forced disconnect.
Is there a documented pattern (e.g., try/finally or context managers) that guarantees cleanup on interrupt? Can I configure or extend the idle timeout, or is the 90-minute boundary fixed across free and paid tiers? How do I verify that autosave has captured the notebook state before the session ends?
34275 reputation · 22 Apr 2022, 23:29 UTC
To handle a kernel interrupt (the Stop button) gracefully in Google Colab, wrap your long-running execution logic in a try...finally block. Because the Stop button triggers a KeyboardInterrupt exception in the Python kernel, the finally block is guaranteed to execute before the cell terminates, provided the session itself remains active.
The most reliable pattern for checkpointing model state or releasing resources is the try...finally structure. While context managers (with statements) are excellent for file handles, a try...finally block is more flexible for high-level training loops.
import torch
try:
for epoch in range(epochs):
# Training logic here
train_model()
# Periodic checkpointing is safer than relying solely on interrupts
save_checkpoint(epoch)
except KeyboardInterrupt:
print("\nInterrupt detected. Saving final state...")
finally:
# This runs on both successful completion and KeyboardInterrupt
save_checkpoint("final_interrupt_state")
print("Cleanup complete. Resources released.")
It is critical to distinguish between notebook state and runtime state:
To ensure state is preserved, you must mount Google Drive and write checkpoints directly to /content/drive/. Local disk writes will be wiped upon session termination.
The 90-minute idle timeout generally refers to user inactivity (lack of interaction with the browser tab). An actively running cell typically prevents the session from being flagged as "idle," but it does not grant an infinite lease. All tiers (Free, Pro, Pro+) are subject to maximum session durations (e.g., 12–24 hours) and can be reclaimed by Google if system resources are needed.
while True: pass loop inside a try...finally block. Click the Stop button and verify the finally message prints.Be aware that a "Restart Session" or a hard backend crash bypasses the Python exception handler entirely. In these cases, finally blocks will not execute. The only foolproof strategy is frequent, automated checkpointing to persistent storage.
Note: To provide more specific guidance on GPU memory release, please specify if you are using PyTorch or TensorFlow, as the commands for clearing the cache differ.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.