Skip to content

Commit

Permalink
with DeviceCollector doesn't work in plans, so error rather than hang (
Browse files Browse the repository at this point in the history
  • Loading branch information
coretl authored Oct 16, 2024
1 parent 72c61ad commit 3e74761
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/ophyd_async/core/_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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())
Expand Down
10 changes: 10 additions & 0 deletions tests/core/test_device_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 3e74761

Please sign in to comment.