fractal-analytics-platform / fractal-server

Fractal backend
https://fractal-analytics-platform.github.io/fractal-server/
BSD 3-Clause "New" or "Revised" License
10 stars 3 forks source link

Setup correct cleanup of auxiliary threads in executors #1792

Closed tcompa closed 4 days ago

tcompa commented 1 week ago

The minimal example below shows that the __exit__ method is not called if an error is raised in __init__, and it shows a possibly pattern for always cleaning up.

class ContextManager():

    def __init__(self, raise_error: bool):
        try:
            print("init")
            if raise_error:
                raise RuntimeError()
        except Exception as e:
            self.cleanup()
            raise e

    def __enter__(self):
        print("enter")

    def cleanup(self):
        print("cleanup")

    def __exit__(self, a, b, c):
        self.cleanup()
        print("exit", a, b, c)

with ContextManager(raise_error=False):
    print("operations")
print()

with ContextManager(raise_error=True):
    print("operations")
print()

output

init
enter
operations
cleanup
exit None None None

init
cleanup
Traceback (most recent call last):
  File "/tmp/x.py", line 29, in <module>
    with ContextManager(raise_error=True):
  File "/tmp/x.py", line 12, in __init__
    raise e
  File "/tmp/x.py", line 9, in __init__
    raise RuntimeError()
RuntimeError
tcompa commented 5 days ago

Changes coming with https://github.com/fractal-analytics-platform/fractal-server/pull/1793:

tcompa commented 4 days ago

Closed with #1790. A different issue is due, re: whether failed paramiko connections leave a trace of spurious threads open - cc @mfranzon