How to unexport my Pytango device servers

Hi,

Contrary to my C++ device servers, my Pytango device servers are never unexported when they terminate. This is causing problems when I use Database.get_device_exported: it returns all my Pytango device servers even if they are not running. I tried calling unexport_server() explicitly from del and from a signal handler for SIGINT but in both cases, the code does not appear to be executed.

How do I make sure that a Pytango device server unexports itself when it terminates?

Thanks,
Andre

Hey! Welcome @aglas!

You’re the first one to inaugurate the new version of our community forum. Congratulations. :slightly_smiling_face:
What version of pytango are you using?

Maybe @ymatveev or @ajoubert could give you some hints?

There’s a first time for everything! :slightly_smiling_face:

I am using Pytango 9.5.1 in combination with Tango 9.5.0 (on Rocky Linux release 9.6).

Hi @aglas

Yes, there are some issues with clean shutdown. (Hopefully fixed in the upcoming 10.1.0 release, thanks to recent change in cppTango).

The most common cause is starting a thread before the device server is started. The signals to interrupt and terminate the application, SIGINT and SIGTERM, could get handled by that user thread, instead of cppTango’s signal handling thread. If that is happening, there won’t be a clean shutdown, so the device server won’t get a chance to unexport itself.

If you aren’t creating your own threads, it could be a dependency like numpy that automatically starts some background threads (depending on configuration, and the underlying linear algebra libraries it is using, e.g., libblas).

There is some discussion here: Revise treatment of SIGTERM in Linux (#927) · Issues · tango-controls / cppTango · GitLab

That thread includes a suggestion about masking signals in the Python code, before importing anything else. Adding your own signal handler or __del__ method won’t help.

Some relevant PyTango issues:

Regards,
/Anton