Repository: Parsl/parsl

Issue:
Non-fully initialised MonitoringHub throws exception from __del__ finalizer
When a MonitoringHub has not been fully initialised (in my case due to a missing parameter), the `_-del__` finalizer throws an error as it incorrectly assumes that it will be called against a fully initialised object.

This creates a spurious stack trace.

```
E   TypeError: __init__() missing 1 required positional argument: 'hub_address'

parsl/tests/configs/htex_local.py:25: TypeError
----------------------------------------- Captured stderr setup ------------------------------------------
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
  File "/home/benc/parsl/src/parsl/parsl/monitoring/monitoring.py", line 250, in __del__
    self.close()
  File "/home/benc/parsl/src/parsl/parsl/monitoring/monitoring.py", line 234, in close
    if self.logger:
AttributeError: 'MonitoringHub' object has no attribute 'logger'
```


Discussion:
I was seeing a number of `__del__` related error messages on @ZhuozhaoLi 's console yesterday that were different to this one - maybe interesting to record those here too.
I've also seen this, at the end of a test session, when running monitoring in pytest:

```
Exception ignored in: <bound method MonitoringHub.__del__ of MonitoringHub(
    'localhost',
    client_address='127.0.0.1', 
    client_port_range=(55000, 56000), 
    hub_port=55055, 
    hub_port_range=(55050, 56000), 
    logdir='/home/benc/parsl/src/parsl/runinfo/2569', 
    logging_endpoint='sqlite:///monitoring.db', 
    logging_level=20, 
    resource_monitoring_enabled=True, 
    resource_monitoring_interval=3, 
    workflow_name=None, 
    workflow_version=None
)>
Traceback (most recent call last):
  File "/home/benc/parsl/src/parsl/parsl/monitoring/monitoring.py", line 283, in __del__
  File "/home/benc/parsl/src/parsl/parsl/monitoring/monitoring.py", line 268, in close
  File "/usr/lib/python3.5/logging/__init__.py", line 1279, in info
  File "/usr/lib/python3.5/logging/__init__.py", line 1520, in isEnabledFor
TypeError: unorderable types: int() >= NoneType()
```

Here's another one:

```
parsl/tests/test_checkpointing/test_regression_233.py ...                                          [100%]

============================================ warnings summary ============================================
test_checkpointing/test_regression_233.py::test_slower_apps
  /home/benc/parsl/src/parsl/parsl/dataflow/dflow.py:968: DeprecationWarning: The 'warn' method is deprecated, use 'warning' instead
    logger.warn("No tasks checkpointed so far in this run. Please ensure caching is enabled")

-- Docs: https://docs.pytest.org/en/latest/warnings.html
================================== 3 passed, 1 warnings in 6.78 seconds ==================================
Exception ignored in: <bound method MonitoringHub.__del__ of MonitoringHub(
    'localhost',
    client_address='127.0.0.1', 
    client_port_range=(55000, 56000), 
    hub_port=55055, 
    hub_port_range=(55050, 56000), 
    logdir='/home/benc/parsl/src/parsl/runinfo_5cqwv1q/000', 
    logging_endpoint='sqlite:///monitoring.db', 
    logging_level=20, 
    resource_monitoring_enabled=True, 
    resource_monitoring_interval=1, 
    workflow_name=None, 
    workflow_version=None
)>
Traceback (most recent call last):
  File "/home/benc/parsl/src/parsl/parsl/monitoring/monitoring.py", line 282, in __del__
  File "/home/benc/parsl/src/parsl/parsl/monitoring/monitoring.py", line 267, in close
  File "/usr/lib/python3.5/logging/__init__.py", line 1280, in info
  File "/usr/lib/python3.5/logging/__init__.py", line 1416, in _log
  File "/usr/lib/python3.5/logging/__init__.py", line 1426, in handle
  File "/usr/lib/python3.5/logging/__init__.py", line 1488, in callHandlers
  File "/usr/lib/python3.5/logging/__init__.py", line 856, in handle
  File "/usr/lib/python3.5/logging/__init__.py", line 1048, in emit
  File "/usr/lib/python3.5/logging/__init__.py", line 1038, in _open
NameError: name 'open' is not defined
```


Resolving patch:
diff --git a/parsl/monitoring/db_manager.py b/parsl/monitoring/db_manager.py
--- a/parsl/monitoring/db_manager.py
+++ b/parsl/monitoring/db_manager.py
@@ -190,9 +190,6 @@ class Resource(Base):
             PrimaryKeyConstraint('task_id', 'run_id', 'timestamp'),
         )
 
-    def __del__(self):
-        self.session.close()
-
 
 class DatabaseManager(object):
     def __init__(self,
diff --git a/parsl/monitoring/monitoring.py b/parsl/monitoring/monitoring.py
--- a/parsl/monitoring/monitoring.py
+++ b/parsl/monitoring/monitoring.py
@@ -121,9 +121,6 @@ def send(self, message_type, task_id, message):
             return False
         return x
 
-    def __del__(self):
-        self.sock.close()
-
 
 class MonitoringHub(RepresentationMixin):
     def __init__(self,
@@ -278,9 +275,6 @@ def close(self):
             self.queue_proc.terminate()
             self.priority_msgs.put(("STOP", 0))
 
-    def __del__(self):
-        self.close()
-
     @staticmethod
     def monitor_wrapper(f, task_id, monitoring_hub_url, run_id, sleep_dur):
         """ Internal


Test patch: