Problem with plugin shutdown

Hi all,
I'm developing a plugin to implement some voice commands in OctoPrint. The plugin spin up a pyaudio stream (thread?) that continuously receives audio frames from a USB microphone.
The plugin works well, meaning it can run in parallel to the other tasks OctoPrint needs to do, is non-blocking and requires minimal resources.
However it doesn't stop gracefully for some reason and every other reboot OctoPrint start in safe mode with the plugin disabled. Rebooting from safe mode then brings the plugin back, and reboting again the cycle repeats itself.

During development, when OctoPrint is started manually from command line:

  • sending Ctrl+C has no apparent effect (well not in the logging at least)
  • sending Ctrl+C second time will terminate octoprint with an exception

In the on_shutdown hook of the plugin I call a method to "destroy" the audio stream and everything linked to it, which will eventually doesn't complete and stays hanging

Is there another way to terminate running threads inside a plugin?
I might be missing some fundamental aspect of the OctoPrint architecture here

Cheers
Stef

how do you start your thread? as a daemon?
Something like this:

thread = threading.Thread(name='ReadCurrentTemperatureThread',
					  target=self._readCurrentTemeratureFromPrinterAsync,
				  args=(self._printer, printJobModel, self._addTemperatureToPrintModel,))
thread.daemon = True
thread.start()

Hi Ollis,
no the task inside the plugin is not started as a thread.
Juast a question, isn't each plugin started as a thread by OctoPrint? If this is not the case I'll give it a try to your snippet above.

Cheers
Stef

Hi @stefanino,
one plugin has not a dedicated single thread.

I depends on what "service" do you want to consume.
E.g. if you want to manipulate the gcode during sending to the printer, a "communication-thread" is used. Advice: do not implement long running task in that thread :wink:
Or you just want to listen on progress-change, than a "monitoring-thread" is used.

BR
Olli