microsoft / vscode-jupyter

VS Code Jupyter extension
https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter
MIT License
1.3k stars 294 forks source link

Kernel dies when running some code #9283

Closed wwdok closed 2 years ago

wwdok commented 2 years ago

Environment data

Expected behaviour

Expect the matplotlib can show the image

Actual behaviour

The kernel died

Steps to reproduce:

[NOTE: Self-contained, minimal reproducing code samples are extremely helpful and will expedite addressing your issue]

  1. Run the code:
    
    %matplotlib inline
    from matplotlib import pyplot as plt

img = plt.imread('../data/image/CLAHE_before.jpg') print("==>> type(img): ", type(img)) # success here print("==>> img.shape: ", img.shape) # success here plt.imshow(img) # error occur here


<!--
Note: If you think a GIF of what is happening would be helpful, consider tools like https://www.cockos.com/licecap/, https://github.com/phw/peek or https://www.screentogif.com/ .
-->

## Logs

<details>

<summary>Output for <code>Jupyter</code> in the <code>Output</code> panel (<code>View</code>→<code>Output</code>, change the drop-down the upper-right of the <code>Output</code> panel to <code>Jupyter</code>)
</summary>

<p>

Visual Studio Code (1.64.2, undefined, desktop) Jupyter Extension Version: 2022.3.1000691006. Python Extension Version: 2022.2.1924087327. User belongs to experiment group 'jupyterTestcf' User belongs to experiment group 'jupyterEnhancedDataViewer' Info 21:34:10: ZMQ install verified. Info 21:34:11: Preferred kernel connection found in cache .jvsc74a57bd0ecf5722fdaf1897a315d257d89d94520bfcaa453217d5becf09b39e73618b0de.C:\ProgramData\Miniconda3\python.exe.C:\ProgramData\Miniconda3\python.exe.-m#ipykernel_launcher Info 21:34:11: PreferredConnection: .jvsc74a57bd0ecf5722fdaf1897a315d257d89d94520bfcaa453217d5becf09b39e73618b0de.C:\ProgramData\Miniconda3\python.exe.C:\ProgramData\Miniconda3\python.exe.-m#ipykernel_launcher found for NotebookDocument: e:\Repo\PythonScripts\Learn OpenCV\CLAHE.ipynb Info 21:34:11: Early registration of controller for Kernel connection .jvsc74a57bd0ecf5722fdaf1897a315d257d89d94520bfcaa453217d5becf09b39e73618b0de.C:\ProgramData\Miniconda3\python.exe.C:\ProgramData\Miniconda3\python.exe.-m#ipykernel_launcher Info 21:34:11: TargetController found ID: .jvsc74a57bd0ecf5722fdaf1897a315d257d89d94520bfcaa453217d5becf09b39e73618b0de.C:\ProgramData\Miniconda3\python.exe.C:\ProgramData\Miniconda3\python.exe.-m#ipykernel_launcher for document e:\Repo\PythonScripts\Learn OpenCV\CLAHE.ipynb Info 21:34:11: Setting controller affinity for e:\Repo\PythonScripts\Learn OpenCV\CLAHE.ipynb .jvsc74a57bd0ecf5722fdaf1897a315d257d89d94520bfcaa453217d5becf09b39e73618b0de.C:\ProgramData\Miniconda3\python.exe.C:\ProgramData\Miniconda3\python.exe.-m#ipykernel_launcher Info 21:34:11: Interrupt requested & no kernel. Info 21:34:11: Setting setActiveController for e:\Repo\PythonScripts\Learn OpenCV\CLAHE.ipynb Info 21:34:11: KernelProvider switched kernel to id = .jvsc74a57bd0ecf5722fdaf1897a315d257d89d94520bfcaa453217d5becf09b39e73618b0de.C:\ProgramData\Miniconda3\python.exe.C:\ProgramData\Miniconda3\python.exe.-m#ipykernel_launcher Info 21:34:11: Starting Notebook in kernel.ts id = .jvsc74a57bd0ecf5722fdaf1897a315d257d89d94520bfcaa453217d5becf09b39e73618b0de.C:\ProgramData\Miniconda3\python.exe.C:\ProgramData\Miniconda3\python.exe.-m#ipykernel_launcher Info 21:34:11: Creating raw notebook for e:\Repo\PythonScripts\Learn OpenCV\CLAHE.ipynb Info 21:34:11: Getting preferred kernel for e:\Repo\PythonScripts\Learn OpenCV\CLAHE.ipynb Info 21:34:11: Computing working directory e:\Repo\PythonScripts\Learn OpenCV\CLAHE.ipynb Info 21:34:11: Experiment status for python is {"enabled":true,"optInto":[],"optOutFrom":[]} Info 21:34:11: Starting raw kernel base (Python 3.8.5) for interpreter C:\ProgramData\Miniconda3\python.exe Info 21:34:11: Kernel launching with ports 9000,9001,9002,9003,9004. Start port is 9000 Info 21:34:12: Creating controller for jupyter-notebook with interpreter C:\ProgramData\Miniconda3\python.exe Info 21:34:12: Registering dummy command feature Info 21:34:13: Got activated conda env vars ourselves for C:\ProgramData\Miniconda3\python.exe in 2103 Info 21:34:13: Process Execution: > C:\ProgramData\Miniconda3\python.exe -m pip list

C:\ProgramData\Miniconda3\python.exe -m pip list Info 21:34:13: Process Execution: > C:\ProgramData\Miniconda3\python.exe -c "import ipykernel; print(ipykernel.version); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.file)" C:\ProgramData\Miniconda3\python.exe -c "import ipykernel; print(ipykernel.version); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.file)" Info 21:34:13: Adding env Variable PYTHONNOUSERSITE to C:\ProgramData\Miniconda3\python.exe Info 21:34:13: Creating daemon process for C:\ProgramData\Miniconda3\python.exe with env variables count 96 Info 21:34:13: Process Execution: > C:\ProgramData\Miniconda3\python.exe -m vscode_datascience_helpers.daemon --daemon-module=vscode_datascience_helpers.kernel_interrupt_daemon -v --ppid 21248 C:\ProgramData\Miniconda3\python.exe -m vscode_datascience_helpers.daemon --daemon-module=vscode_datascience_helpers.kernel_interrupt_daemon -v --ppid 21248 Info 21:34:13: get interrupthandle daemon Info 21:34:13: Process Execution: > C:\ProgramData\Miniconda3\python.exe -m ipykernel_launcher --ip=127.0.0.1 --stdin=9003 --control=9001 --hb=9000 --Session.signature_scheme="hmac-sha256" --Session.key=b"0be60d2d-43ef-46f3-a77f-7def4e02e817" --shell=9002 --transport="tcp" --iopub=9004 --f=C:\Users\Pactera\AppData\Local\Temp\tmp-21248kGfOI2UGFq2g.json C:\ProgramData\Miniconda3\python.exe -m ipykernel_launcher --ip=127.0.0.1 --stdin=9003 --control=9001 --hb=9000 --Session.signature_scheme="hmac-sha256" --Session.key=b"0be60d2d-43ef-46f3-a77f-7def4e02e817" --shell=9002 --transport="tcp" --iopub=9004 --f=C:\Users\Pactera\AppData\Local\Temp\tmp-21248kGfOI2UGFq2g.json Info 21:34:13: Process Execution: cwd: e:\Repo\PythonScripts\Learn OpenCV cwd: e:\Repo\PythonScripts\Learn OpenCV Info 21:34:14: ipykernel version 6.4.1 for C:\ProgramData\Miniconda3\python.exe Info 21:34:14: ipykernel location C:\ProgramData\Miniconda3\lib\site-packages\ipykernel__init__.py for C:\ProgramData\Miniconda3\python.exe Info 21:34:18: Kernel Output: NOTE: When using the ipython kernel entry point, Ctrl-C will not work.

To exit, you will have to explicitly quit this process, by either sending "quit" from a client, or using Ctrl-\ in UNIX-like environments.

To read more about this, see https://github.com/ipython/ipython/issues/2049

To connect another client to this kernel, use: --existing C:\Users\Pactera\AppData\Local\Temp\tmp-21248kGfOI2UGFq2g.json

Info 21:34:18: Raw session started and connected Started kernel base (Python 3.8.5) Info 21:34:18: Finished connecting ed5130f7-e4a5-4941-92b5-9d2a6bd907ef Info 21:34:18: UpdateWorkingDirectoryAndPath in Kernel Info 21:34:18: Executing silently Code (idle) = import site\nsite.addsitedir(site.getusersitepackages())\nimport os\nimport sys\n%cd "e:\Repo\PythonScri Info 21:34:18: Executing silently Code (completed) = import site\nsite.addsitedir(site.getusersitepackages())\nimport os\nimport sys\n%cd "e:\Repo\PythonScri Info 21:34:18: Waiting for idle on (kernel): 36fa3e43-0903-4b08-bfb4-f082323a1955 -> idle Info 21:34:18: Finished waiting for idle on (kernel): 36fa3e43-0903-4b08-bfb4-f082323a1955 -> idle Info 21:34:25: Execute Cells request 0 Info 21:34:25: Execute Cell 0 e:\Repo\PythonScripts\Learn OpenCV\CLAHE.ipynb Error 21:34:27: Disposing session as kernel process died ExitCode: 3228369022, Reason: Info 21:34:27: Dispose Kernel process Info 21:34:27: kill daemon Error 21:34:27: Raw kernel process exited code: 3228369022 Info 21:34:27: Cancel all remaining cells due to dead kernel Info 21:34:27: Cancel pending cells Info 21:34:27: Cell 0 executed with state Success



</p>
</details>
rchiodo commented 2 years ago

Error 3228369022 or (C06D007E) sounds like matplotlib crashed. This should be a matplotlib problem.

I would guess your image is too large or something along those lines. The same code for me gives me this:

image

wwdok commented 2 years ago

@rchiodo It seems not the problem of matplotlib, today I encouter Error 3228369022 again when using numpy matrix multiplication @, below code will produces the error;

import numpy as np
a = np.array([[1., 0., 0.],
 [0., 1., 0.],
 [0., 0., 1.]])
print(a@a)

image

rchiodo commented 2 years ago

What do you have for this setting here:

"jupyter.runStartupCommands": []

It looks to me like that code is crashing, not the specific code you ran. Otherwise, you'd have this in your log:

Info 09:13:02: Execute Cells request 1
Info 09:13:02: Execute Cell 1 <path to notebook>
wwdok commented 2 years ago

Do you mean this: image Open the settings.json, this is its content: image

rchiodo commented 2 years ago

That looks like you're not using that setting so my guess was wrong.

The other thing that happens is we request the kernel information from the kernel as well. Not sure how that would cause a crash.

Can you ever run a kernel? Or do all kernels crash immediately?

rchiodo commented 2 years ago

My suspicion now is that something running in the kernel on startup fails and eventually causes it to crash. We might be able to get more information by running this:

C:\ProgramData\Miniconda3\python.exe -m ipykernel_launcher --ip=127.0.0.1 --stdin=9003 --control=9001 --hb=9000 --Session.signature_scheme="hmac-sha256" --Session.key=b"0be60d2d-43ef-46f3-a77f-7def4e02e817" --shell=9002 --transport="tcp" --iopub=9004 --f=C:\Users\Pactera\AppData\Local\Temp\tmp-21248kGfOI2UGFq2g.json

That should start the kernel the same way we are.

wwdok commented 2 years ago

@rchiodo Usually I can successfully start the jupyter kernel and execute the code, just only in a few cases (such as the above two) will the jupyter kernel die. For example, I execute the following code:

import numpy as np
a = np.array([[1., 0., 0.],
 [0., 1., 0.],
 [0., 0., 1.]])

The kernel will not die, its log is

``` Info 10:10:00: Execute Cells request 0 Info 10:10:00: Execute Cell 0 e:\Repo\PythonScripts\temp.ipynb Info 10:10:02: Restart requested file:///e%3A/Repo/PythonScripts/temp.ipynb Info 10:10:02: Restart kernel execution Info 10:10:02: Cancel pending cells Info 10:10:02: Starting raw kernel base (Python 3.8.5) for interpreter C:\ProgramData\Miniconda3\python.exe Info 10:10:02: Restarting undefined Info 10:10:02: Kernel launching with ports 9019,9020,9021,9022,9023. Start port is 9000 Info 10:10:02: Process Execution: > C:\ProgramData\Miniconda3\python.exe -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)" > C:\ProgramData\Miniconda3\python.exe -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)" Info 10:10:02: Adding env Variable PYTHONNOUSERSITE to C:\ProgramData\Miniconda3\python.exe Info 10:10:02: Creating daemon process for C:\ProgramData\Miniconda3\python.exe with env variables count 98 Info 10:10:02: Process Execution: > C:\ProgramData\Miniconda3\python.exe -m vscode_datascience_helpers.daemon --daemon-module=vscode_datascience_helpers.kernel_interrupt_daemon -v --ppid 35396 > C:\ProgramData\Miniconda3\python.exe -m vscode_datascience_helpers.daemon --daemon-module=vscode_datascience_helpers.kernel_interrupt_daemon -v --ppid 35396 Info 10:10:02: get interrupthandle daemon Info 10:10:02: Process Execution: > C:\ProgramData\Miniconda3\python.exe -m ipykernel_launcher --ip=127.0.0.1 --stdin=9022 --control=9020 --hb=9019 --Session.signature_scheme="hmac-sha256" --Session.key=b"a0b4b23b-5b65-4095-8018-afb01dd4ece7" --shell=9021 --transport="tcp" --iopub=9023 --f=C:\Users\Pactera\AppData\Local\Temp\tmp-35396E1P1Px7jrHan.json > C:\ProgramData\Miniconda3\python.exe -m ipykernel_launcher --ip=127.0.0.1 --stdin=9022 --control=9020 --hb=9019 --Session.signature_scheme="hmac-sha256" --Session.key=b"a0b4b23b-5b65-4095-8018-afb01dd4ece7" --shell=9021 --transport="tcp" --iopub=9023 --f=C:\Users\Pactera\AppData\Local\Temp\tmp-35396E1P1Px7jrHan.json Info 10:10:02: Process Execution: cwd: e:\Repo\PythonScripts cwd: e:\Repo\PythonScripts Info 10:10:02: ipykernel version 6.4.1 for C:\ProgramData\Miniconda3\python.exe Info 10:10:02: ipykernel location C:\ProgramData\Miniconda3\lib\site-packages\ipykernel\__init__.py for C:\ProgramData\Miniconda3\python.exe Info 10:10:03: Kernel Output: NOTE: When using the `ipython kernel` entry point, Ctrl-C will not work. To exit, you will have to explicitly quit this process, by either sending "quit" from a client, or using Ctrl-\ in UNIX-like environments. To read more about this, see https://github.com/ipython/ipython/issues/2049 To connect another client to this kernel, use: --existing C:\Users\Pactera\AppData\Local\Temp\tmp-35396E1P1Px7jrHan.json Info 10:10:03: Got new session c9abc9e5-09ad-45e7-8899-ec10ffd8ae54 Info 10:10:03: Started new restart session Info 10:10:03: Executing silently Code (idle) = import sys\nprint(sys.executable) Info 10:10:04: Executing silently Code (completed) = import sys\nprint(sys.executable) Info 10:10:04: UpdateWorkingDirectoryAndPath in Kernel Info 10:10:04: Executing silently Code (idle) = import site\nsite.addsitedir(site.getusersitepackages())\nimport os\nimport sys\n%cd "e:\Repo\PythonScri Info 10:10:04: Executing silently Code (completed) = import site\nsite.addsitedir(site.getusersitepackages())\nimport os\nimport sys\n%cd "e:\Repo\PythonScri Info 10:10:04: Waiting for idle on (kernel): c9abc9e5-09ad-45e7-8899-ec10ffd8ae54 -> idle Info 10:10:04: Finished waiting for idle on (kernel): c9abc9e5-09ad-45e7-8899-ec10ffd8ae54 -> idle Info 10:10:04: Cell 0 executed with state Success ```

rchiodo commented 2 years ago

Hmm. Very weird that that other code crashes the kernel. The only difference being the print statement.

You might try running this first:

import logging
logger = logging.getLogger()
fhandler = logging.FileHandler(filename='mylog.log', mode='a')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fhandler.setFormatter(formatter)
logger.addHandler(fhandler)
logger.setLevel(logging.DEBUG)

That gets the kernel to log to a file called 'mylog.log'.

The only other thing I can think of is to debug the kernel itself.

It'd probably be easier to just upgrade your python environment though.

wwdok commented 2 years ago

Yes, this is a rare issue, if I switch to another python virtual env, there is no problem. I am curious what ExitCode: 3228369022 mean

rchiodo commented 2 years ago

I am curious what ExitCode: 3228369022 mean

This error code stands for 'external exception' as far as I know. This generally means some part of the app called into another dll that caused a crash/exit. Without debugging the call that caused the problem, it's really hard to tell why.

DonJayamanne commented 2 years ago

Closing so that we can handle this better and provide a more contextual error message, see here https://github.com/microsoft/vscode-jupyter/issues/9375