diff --git a/artiq/frontend/artiq_run.py b/artiq/frontend/artiq_run.py index ab3cb0ec9..e5bfd7a9e 100755 --- a/artiq/frontend/artiq_run.py +++ b/artiq/frontend/artiq_run.py @@ -154,12 +154,15 @@ def run(with_file=False): pdb = FlatFileDB(args.pdb) pdb.hooks.append(SimpleParamLogger()) rdb = ResultDB(lambda description: None, lambda mod: None) + dbh = DBHub(ddb, pdb, rdb) - with DBHub(ddb, pdb, rdb) as dbh: + try: exp_inst = _build_experiment(dbh, args) rdb.build() exp_inst.run() exp_inst.analyze() + finally: + dbh.close_devices() if args.hdf5 is not None: with h5py.File(args.hdf5, "w") as f: diff --git a/artiq/master/worker_db.py b/artiq/master/worker_db.py index 962a62de8..a709a9410 100644 --- a/artiq/master/worker_db.py +++ b/artiq/master/worker_db.py @@ -1,11 +1,15 @@ from collections import OrderedDict import importlib +import logging from artiq.protocols.sync_struct import Notifier from artiq.protocols.pc_rpc import Client, BestEffortClient from artiq.master.results import result_dict_to_hdf5 +logger = logging.get_logger(__name__) + + class ResultDB: def __init__(self, init_rt_results, update_rt_results): self.init_rt_results = init_rt_results @@ -108,15 +112,11 @@ class DBHub: """Closes all active devices, in the opposite order as they were requested.""" for dev in reversed(list(self.active_devices.values())): - if isinstance(dev, (Client, BestEffortClient)): - dev.close_rpc() - elif hasattr(dev, "close"): - dev.close() + try: + if isinstance(dev, (Client, BestEffortClient)): + dev.close_rpc() + elif hasattr(dev, "close"): + dev.close() + except Exception as e: + logger.warning("Exception %r when closing device %r", e, dev) self.active_devices.clear() - - def __enter__(self): - return self - - def __exit__(self, exc_type, exc_val, exc_tb): - self.close_devices() - return False # do not suppress exceptions within context diff --git a/artiq/master/worker_impl.py b/artiq/master/worker_impl.py index 6475fca9a..8da3a3fd3 100644 --- a/artiq/master/worker_impl.py +++ b/artiq/master/worker_impl.py @@ -97,8 +97,9 @@ def main(): exp_inst = None rdb = ResultDB(init_rt_results, update_rt_results) + dbh = DBHub(ParentDDB, ParentPDB, rdb) - with DBHub(ParentDDB, ParentPDB, rdb) as dbh: + try: while True: obj = get_object() action = obj["action"] @@ -115,7 +116,6 @@ def main(): put_object({"action": "completed"}) elif action == "run": exp_inst.run() - dbh.close_devices() put_object({"action": "completed"}) elif action == "analyze": exp_inst.analyze() @@ -129,6 +129,8 @@ def main(): put_object({"action": "completed"}) elif action == "terminate": break + finally: + dbh.close_devices() if __name__ == "__main__": main()