From 3e74761b76f337bf0e36dcdbdd7fdc0dd814105b Mon Sep 17 00:00:00 2001 From: "Tom C (DLS)" <101418278+coretl@users.noreply.github.com> Date: Wed, 16 Oct 2024 17:14:08 +0100 Subject: [PATCH] with DeviceCollector doesn't work in plans, so error rather than hang (#612) --- src/ophyd_async/core/_device.py | 7 ++++++- tests/core/test_device_collector.py | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/ophyd_async/core/_device.py b/src/ophyd_async/core/_device.py index b4305c2bfa..bf3e23f5d6 100644 --- a/src/ophyd_async/core/_device.py +++ b/src/ophyd_async/core/_device.py @@ -12,7 +12,7 @@ ) from bluesky.protocols import HasName -from bluesky.run_engine import call_in_bluesky_event_loop +from bluesky.run_engine import call_in_bluesky_event_loop, in_bluesky_event_loop from ._utils import DEFAULT_TIMEOUT, NotConnected, wait_for_connection @@ -224,6 +224,11 @@ async def __aexit__(self, type, value, traceback): await self._on_exit() def __exit__(self, type_, value, traceback): + if in_bluesky_event_loop(): + raise RuntimeError( + "Cannot use DeviceConnector inside a plan, instead use " + "`yield from ophyd_async.plan_stubs.ensure_connected(device)`" + ) self._objects_on_exit = self._caller_locals() try: fut = call_in_bluesky_event_loop(self._on_exit()) diff --git a/tests/core/test_device_collector.py b/tests/core/test_device_collector.py index efebc6a48f..cdb6e122c5 100644 --- a/tests/core/test_device_collector.py +++ b/tests/core/test_device_collector.py @@ -67,6 +67,16 @@ def test_sync_device_connector_run_engine_created_connects(RE): assert working_device.connected +def test_connecting_in_plan_raises(RE): + def bad_plan(): + yield from bps.null() + with DeviceCollector(): + working_device = WorkingDevice("somename") # noqa: F841 + + with pytest.raises(RuntimeError, match="Cannot use DeviceConnector inside a plan"): + RE(bad_plan()) + + def test_async_device_connector_run_engine_same_event_loop(): async def set_up_device(): async with DeviceCollector(mock=True):