From 23779e49df509fd3aab33b4fc55ba2b0610f62ad Mon Sep 17 00:00:00 2001 From: Chris Burr Date: Fri, 24 Jul 2020 09:15:27 +0200 Subject: [PATCH] Prevent deadlock in Python bindings from XRootD.client.finalize.finalize --- bindings/python/libs/client/finalize.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/bindings/python/libs/client/finalize.py b/bindings/python/libs/client/finalize.py index 1be36245166..cf75c9a5f17 100644 --- a/bindings/python/libs/client/finalize.py +++ b/bindings/python/libs/client/finalize.py @@ -21,9 +21,14 @@ # granted to it by virtue of its status as an Intergovernmental Organization # or submit itself to any jurisdiction. #------------------------------------------------------------------------------ +from __future__ import absolute_import +import gc import atexit + from pyxrootd import client +from .file import File + @atexit.register def finalize(): @@ -32,4 +37,12 @@ def finalize(): no Python APIs are called after the Python Interpreter gets finalized. """ + # Ensure there are no files left open as calling their destructor will + # cause "close" commands to be sent. + # If this isn't done, there will be no running threads to process requests + # after this function returns so the interpreter will deadlock. + for obj in gc.get_objects(): + if isinstance(obj, File) and obj.is_open(): + obj.close() + client.__XrdCl_Stop_Threads()