1

So I have an issue with the import of a driver, which apparently leads to almost all threads that I open to end in a Resourcewarning. (python version 3.9.16 on Windows in Anaconda environment; nidaqmx version 0.6.5)

After opening and closing multiple instances of threads with the import, the Kernel appears to die.

Here is the minimal reproducible example:

import threading 
import nidaqmx #This is the import that causes the problems


def idling():
   for i in range(10):
      print("still idling")

When I now try to run the idling method in a thread:

idling_thread = threading.Thread(target = idling)
idling_thread.start()

I can reproduce the warning message

sys:1: ResourceWarning: unclosed socket <zmq.Socket(zmq.PUSH) at 0x132d0955880>

I already tried finding the socket that is unclosed using tracemalloc, which showed me the following:

File "C:\Users\lib\site-packages\ipykernel\iostream.py", lineno 563
    self._schedule_flush()
File "C:\Users\lib\site-packages\ipykernel\iostream.py", lineno 469
    self.pub_thread.schedule(_schedule_in_thread)
File "C:\Users\lib\site-packages\ipykernel\iostream.py", lineno 210
    self._event_pipe.send(b"")
File "C:\Users\lib\site-packages\ipykernel\iostream.py", lineno 98
    event_pipe = ctx.socket(zmq.PUSH)
File "C:\Users\lib\site-packages\zmq\sugar\context.py", lineno 259
    s: ST = self._socket_class(  # set PYTHONTRACEMALLOC=2 to get the calling frame

So it seems to me as if the underlying cause is, that the import of the driver somehow messes up the communication with my thread. I could just ignore this warning, but as mentioned before, I noticed, that consecutively opening and closing the camera thread (usually around 4 times) leads to the Kernel dying and restarting.

Please note, that without the import of the driver nidaqmx, all threads work fine. With the nidaqmx driver imported, the threads work fine, up to the point where they would close.

I would be very happy if anyone has an idea how to fix the underlying problem.

1 Answers1

0

Are you creating your Context outside the def start_camera(..)?

If so then you should move it inside.

Code outside def start_camera(..) will be run on the main thread, but then when you call start it'll be on its own thread.

For that reason you need a new Context, per the docs:

When called in a subprocess after forking, a new global instance is created instead of inheriting a Context that won’t work from the parent process.

davetapley
  • 17,000
  • 12
  • 60
  • 86
  • Thanks a lot for your reply. So I did some more experimenting around and I managed to find the underlying cause. My question has been updated. As I'm relatively new to programming, I'm not sure I can quite follow your answer. However, with the minimal reproducible example I provided now, it seems that there are no variables or Context shared between the main thread and any side-threads I would open. – Toni Berger Mar 05 '23 at 08:35