Skip to content

Commit

Permalink
fix(api): use move flags in aionotify for modules (#14345)
Browse files Browse the repository at this point in the history
After updating openembedded and buildroot, their udevs will actually
create temporary files for module serial port symlinks and then rename
the tempfiles to the real thing. This wouldn't get noticed because they
were moves, rather than creates or deletes. Getting aionotify to tell us
about moves too and plumbing those events through the add/delete paths
fixes the issue.

Closes RQA-2240
  • Loading branch information
sfoster1 authored Jan 24, 2024
1 parent 0e25e2d commit 46195f5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
7 changes: 6 additions & 1 deletion api/src/opentrons/hardware_control/backends/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ def _build_event_watcher() -> aionotify.Watcher:
watcher.watch(
alias="modules",
path="/dev",
flags=(aionotify.Flags.CREATE | aionotify.Flags.DELETE),
flags=(
aionotify.Flags.CREATE
| aionotify.Flags.DELETE
| aionotify.Flags.MOVED_FROM
| aionotify.Flags.MOVED_TO
),
)
return watcher

Expand Down
10 changes: 8 additions & 2 deletions api/src/opentrons/hardware_control/backends/ot3controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,12 @@ def _build_event_watcher() -> aionotify.Watcher:
watcher.watch(
alias="modules",
path="/dev",
flags=(aionotify.Flags.CREATE | aionotify.Flags.DELETE),
flags=(
aionotify.Flags.CREATE
| aionotify.Flags.DELETE
| aionotify.Flags.MOVED_FROM
| aionotify.Flags.MOVED_TO
),
)
return watcher

Expand All @@ -1105,9 +1110,10 @@ async def _handle_watch_event(self) -> None:
log.debug("incomplete read error when quitting watcher")
return
if event is not None:
flags = aionotify.Flags.parse(event.flags)
log.debug(f"aionotify: {flags} {event.name}")
if "ot_module" in event.name:
event_name = event.name
flags = aionotify.Flags.parse(event.flags)
event_description = AionotifyEvent.build(event_name, flags)
await self.module_controls.handle_module_appearance(event_description)

Expand Down
14 changes: 10 additions & 4 deletions api/src/opentrons/hardware_control/module_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@

log = logging.getLogger(__name__)

MODULE_PORT_REGEX = re.compile("|".join(modules.MODULE_TYPE_BY_NAME.keys()), re.I)
MODULE_PORT_REGEX = re.compile(
# capture all modules by name using alternation
"(" + "|".join(modules.MODULE_TYPE_BY_NAME.keys()) + ")"
# add a negative lookahead to suppress matches on tempfiles udev creates
+ r"\d+(?!\.tmp-c\d+:\d+)",
re.I,
)


class AttachedModulesControl:
Expand Down Expand Up @@ -183,7 +189,7 @@ def get_module_at_port(port: str) -> Optional[modules.ModuleAtPort]:
"""
match = MODULE_PORT_REGEX.search(port)
if match:
name = match.group().lower()
name = match.group(1).lower()
if name not in modules.MODULE_TYPE_BY_NAME:
log.warning(f"Unexpected module connected: {name} on {port}")
return None
Expand All @@ -205,10 +211,10 @@ async def handle_module_appearance(self, event: AionotifyEvent) -> None:
new_modules = None
removed_modules = None
if maybe_module_at_port is not None:
if hasattr(event.flags, "DELETE"):
if hasattr(event.flags, "DELETE") or hasattr(event.flags, "MOVED_FROM"):
removed_modules = [maybe_module_at_port]
log.info(f"Module Removed: {maybe_module_at_port}")
elif hasattr(event.flags, "CREATE"):
elif hasattr(event.flags, "CREATE") or hasattr(event.flags, "MOVED_TO"):
new_modules = [maybe_module_at_port]
log.info(f"Module Added: {maybe_module_at_port}")
try:
Expand Down

0 comments on commit 46195f5

Please sign in to comment.