How to stop a cell from running in Google Colab
Codemia Knowledge Hub ·
Click the stop button next to the running cell, or press Ctrl+M I to interrupt the kernel. Colab runs every cell in one shared Python runtime, so the interrupt halts whatever the kernel is executing right now. You cannot kill one cell while keeping another running in the same kernel.
Why you interrupt the kernel, not the cell
Colab is a hosted Jupyter notebook environment. Every cell runs inside the same Python kernel process: Colab sends the cell's code to the kernel, which executes it sequentially in the main thread.
| Aspect | Behavior |
| Cell execution | Sequential in the main thread by default |
| Interrupt target | The kernel's current execution (not a specific cell) |
| Shared state | All cells share the same variables, imports, and memory |
| Background threads | Not stopped by a cell interrupt unless explicitly handled |
| Multiple queued cells | Interrupting stops the currently running cell; queued cells may still execute |
Cells are code blocks, not isolated processes, so "stopping a particular cell" means interrupting what the kernel is doing at that moment.
Interrupting the running cell
In the UI:
- Click the stop icon (square button) next to the running cell.
- Or choose Runtime > Interrupt execution in the menu bar.
With the keyboard:
- Press
Ctrl+M I(orCmd+M Ion macOS).
This raises a KeyboardInterrupt exception in the currently executing code. If your code catches exceptions too broadly, the interrupt can be swallowed.
In Python 3, KeyboardInterrupt inherits from BaseException, not Exception, so except Exception does not catch it. except BaseException or a bare except: does, and that stops the interrupt from working:
Skipping a cell with a guard flag
To keep a cell from running at all, put a boolean flag at the top:
This is handy with Runtime > Run all when you want to skip expensive steps such as model training, data downloads or GPU-heavy computations.
You can also read the flag from an environment variable:
Set it in an early cell:
Cooperative cancellation in long loops
Build a cancellation check into the loop:
To cancel, create the file while the loop runs, for example with New file in the Files panel (named STOP in /content), or from a terminal:
A flag variable set from another cell does not work here: Colab queues cells, so that cell would only run after the loop has finished. The file is checked on every iteration, so the loop stops cleanly at the next epoch.
Stopping a cell from inside with an exception
When a cell should abort on a condition, raise an exception:
That beats silently continuing with bad data and finding errors many cells later.
To stop all downstream cells after a failure (useful with Run all), you can kill the runtime:
This is a last resort. It disconnects the notebook from the runtime and you must reconnect manually.
Stopping background threads and subprocesses
Interrupting a cell does not clean up threads, subprocesses or async tasks it started. Stop them explicitly:
For threads:
Which method to use
| Method | When to Use | Stops Current Execution | Prevents Future Runs |
Interrupt button / Ctrl+M I | Cell is actively running and you want it to stop now | Yes | No |
Guard flags (if RUN_X) | Cell is expensive and you want to skip it during Run All | No (skips entirely) | Yes (until flag is changed) |
| Cooperative cancellation | Long loop where you want a clean stop at the next iteration | Yes (at next check) | No |
| Raise exception | Cell should abort if a precondition is not met | Yes | No |
| Runtime restart | Everything is stuck and nothing else works | Yes (kills runtime) | No |
Mistakes when stopping cells
- Assuming each cell is its own process. All cells share one kernel.
- Bare
except:clauses that catchKeyboardInterrupt. The stop button stops working. Useexcept Exception:or re-raiseKeyboardInterrupt. - Run all without guard flags. Training, large downloads and GPU work will all execute.
- Expecting the interrupt to stop background work. It only affects the main thread. Background work continues until stopped or the runtime restarts.
- Rerunning downstream cells without checking state. An interrupted cell may have left DataFrames half-populated, counters wrong or file handles open. Validate state first.
- Restarting the runtime when an interrupt would do. A restart clears all variables and means rerunning imports and setup cells.
Related reading
- How can I strip the whitespace from Pandas DataFrame headers?
- How can I use an Artificial Neural Network that was trained with 10 parameters to classify an instance with 3 parameters?
- How can I use pandas.read_sql on an async connection?
- How can I use tensorboard with tf.estimator.Estimator
- How can I tell if a string repeats itself in Python?
- How can I test lambda in local using python?
- How can I use the apply function for a single column?
- How can I view Tensor as an image?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.