microsoft / iqsharp

Microsoft's IQ# Server.
https://docs.microsoft.com/quantum
MIT License
126 stars 56 forks source link

IQsharp zombie process(es) after exiting Jupyter notebook #335

Open guenp opened 3 years ago

guenp commented 3 years ago

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:

  1. Start Jupyter notebook with Python kernel
  2. Import qsharp
  3. Run a long quantum algorithm, such as Quantum Phase Estimation (see GetEnergyByTrotterization from https://github.com/microsoft/Quantum/blob/main/samples/chemistry/MolecularHydrogen/HydrogenSimulation.qs)
  4. Stop the Python kernel
  5. Exit the Jupyter notebook process with CTRL+C in console
  6. Run ps -ax | grep conda
  7. 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

Additional context @dgwvfxn experienced the issue before on a Linux machine but was not able to reproduce it later.

rmshaffer commented 3 years ago

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.