IQsharp zombie process(es) after exiting Jupyter notebook
Describe the bug
Both @dgwvfxn and I noticed that when running a Jupyter notebook that is using the qsharp Python package, and after running a quantum algorithm in the notebook, in some situations an IQ# zombie process sticks around after exiting the Jupyter notebook.
To Reproduce Steps to reproduce the behavior:
- Start Jupyter notebook with Python kernel
- Import qsharp
- Run a long quantum algorithm, such as Quantum Phase Estimation (see
GetEnergyByTrotterizationfrom https://github.com/microsoft/Quantum/blob/main/samples/chemistry/MolecularHydrogen/HydrogenSimulation.qs) - Stop the Python kernel
- Exit the Jupyter notebook process with CTRL+C in console
- Run
ps -ax | grep conda - In my case this shows two 🧟 processes:
$ ps -ax | grep conda
7988 ? Ssl 424:57 /home/guen/anaconda3/opt/iqsharp/Microsoft.Quantum.IQSharp kernel --log-level Error /tmp/tmp1msrltp2.json --user-agent qsharp.py
8542 ? Ssl 42:31 /home/guen/anaconda3/opt/iqsharp/Microsoft.Quantum.IQSharp kernel --log-level Error /tmp/tmplux1kp8n.json --user-agent qsharp.py
Expected behavior When killing the Python kernel, I expect the IQ# kernel to also stop.
System information
- OS: Ubuntu 20.04.1 running in WSL2
- Conda environment
Additional context @dgwvfxn experienced the issue before on a Linux machine but was not able to reproduce it later.
After some investigation, I find that I can reproduce this on both Windows and Linux.
The qsharp Python package starts the IQ# kernel process and registers an atexit handler to shut down the process when the Python interpreter exits. However, if the Python process is killed in any "unclean" way, e.g., via an OS signal, the atexit handler does not get called, and the IQ# kernel process remains running.
One way to fix this may be to have the IQ# kernel periodically check to see if it is still connected to a client, and exit gracefully if a client is not connected for some period of time.