pytorch - 💡(How to fix) Fix [BUG] [CRASH] Torch cause notebook crash on any exception (import torch) [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#178538Fetched 2026-04-08 01:35:41
View on GitHub
Comments
1
Participants
2
Timeline
66
Reactions
0
Author
Timeline (top)
mentioned ×25subscribed ×25labeled ×8renamed ×4

Error Message

from torch.utils.data import Dataset raise Exception()

Code Example

from torch.utils.data import Dataset
raise Exception()

---

import torch
raise Exception()

---

Package manager: UV
ipykernel 7.2.0
PyTorch version: 2.11.0+cu130
Is debug build: False
CUDA used to build PyTorch: 13.0
ROCM used to build PyTorch: N/A

OS:
{
    "Caption":  "Microsoft Windows 11 Enterprise",
    "OSArchitecture":  "64-bit",
    "Version":  "10.0.26200"
}
Expecting value: line 1 column 1 (char 0)
GCC version: Could not collect
Clang version: Could not collect
CMake version: Could not collect
Libc version: N/A

Python version: 3.11.15 (main, Mar  3 2026, 14:55:34) [MSC v.1944 64 bit (AMD64)] (64-bit runtime)
Python platform: Windows-10-10.0.26200-SP0
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: 
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3090
Nvidia driver version: 595.97
cuDNN version: Could not collect
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
Caching allocator config: N/A

CPU:
{
    "Name":  "AMD Ryzen 9 9900X 12-Core Processor            ",
    "Manufacturer":  "AuthenticAMD",
    "Family":  107,
    "Architecture":  9,
    "ProcessorType":  3,
    "DeviceID":  "CPU0",
    "CurrentClockSpeed":  4400,
    "MaxClockSpeed":  4400,
    "L2CacheSize":  12288,
    "L2CacheSpeed":  null,
    "Revision":  17408
}
Expecting value: line 1 column 1 (char 0)

Versions of relevant libraries:
[pip3] Could not collect
[conda] Could not collect
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

Running cell with next code will crash KERNEL of notebook (ipykernel implementation)

from torch.utils.data import Dataset
raise Exception()

Also import torch makes it crash too

import torch
raise Exception()

Result: kernel exited and you need to restart notebook Kernel is launched via python -m ipykernel_launcher ... <img width="1046" height="419" alt="Image" src="https://github.com/user-attachments/assets/5ffc0a34-f302-40f2-98eb-516cf9697d6e" /> Without torch exceptions will not crash notebook. <img width="690" height="378" alt="Image" src="https://github.com/user-attachments/assets/10f31dad-ff2c-45ad-b1a4-92a47bdaf25c" />

Versions

Package manager: UV
ipykernel 7.2.0
PyTorch version: 2.11.0+cu130
Is debug build: False
CUDA used to build PyTorch: 13.0
ROCM used to build PyTorch: N/A

OS:
{
    "Caption":  "Microsoft Windows 11 Enterprise",
    "OSArchitecture":  "64-bit",
    "Version":  "10.0.26200"
}
Expecting value: line 1 column 1 (char 0)
GCC version: Could not collect
Clang version: Could not collect
CMake version: Could not collect
Libc version: N/A

Python version: 3.11.15 (main, Mar  3 2026, 14:55:34) [MSC v.1944 64 bit (AMD64)] (64-bit runtime)
Python platform: Windows-10-10.0.26200-SP0
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: 
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3090
Nvidia driver version: 595.97
cuDNN version: Could not collect
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
Caching allocator config: N/A

CPU:
{
    "Name":  "AMD Ryzen 9 9900X 12-Core Processor            ",
    "Manufacturer":  "AuthenticAMD",
    "Family":  107,
    "Architecture":  9,
    "ProcessorType":  3,
    "DeviceID":  "CPU0",
    "CurrentClockSpeed":  4400,
    "MaxClockSpeed":  4400,
    "L2CacheSize":  12288,
    "L2CacheSpeed":  null,
    "Revision":  17408
}
Expecting value: line 1 column 1 (char 0)

Versions of relevant libraries:
[pip3] Could not collect
[conda] Could not collect

cc @peterjc123 @mszhanyi @skyline75489 @nbcsm @iremyux @Blackhex @andrewkho @divyanshk @SsnL @VitalyFedyunin @dzhulgakov @scotts

extent analysis

Fix Plan

The fix involves catching and handling exceptions properly in the ipykernel environment.

  • Step 1: Modify your code to catch exceptions before they reach the ipykernel level:

from torch.utils.data import Dataset try: # your code here raise Exception() except Exception as e: print(f"Caught exception: {e}")

*   **Step 2:** If using a Jupyter Notebook, ensure that the kernel is properly configured to handle exceptions. You can do this by adding the following code to a cell before running your torch code:
    ```python
import sys
def exception_handler(type, value, tb):
    print(f"Caught exception: {value}")
sys.excepthook = exception_handler
  • Step 3: Consider updating your ipykernel and PyTorch versions to the latest available, as there may be bug fixes related to exception handling.

Verification

To verify that the fix worked, run your code with the exception handling in place. If the kernel no longer crashes when an exception is raised, the fix has been successful.

Extra Tips

  • Always handle exceptions in your code to prevent unexpected crashes.
  • Keep your libraries and kernel up to date to ensure you have the latest bug fixes.
  • Consider using a try-except block around your entire code execution to catch any unexpected exceptions.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - 💡(How to fix) Fix [BUG] [CRASH] Torch cause notebook crash on any exception (import torch) [1 comments, 2 participants]