Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

Commit

Permalink
Fix segmentation fault caused by not dereferencing p4p context
Browse files Browse the repository at this point in the history
  • Loading branch information
Rose Yemelyanova committed Aug 7, 2023
1 parent 8a7e377 commit e69eb6f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 549 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ classifiers = [
]
description = "Cross-institution EPICS Devices for Ophyd"
dependencies = [
"ophyd @ git+https://github.com/bluesky/ophyd.git@sim-backend-numpy-typing",
"ophyd[ca] @ git+https://github.com/bluesky/ophyd.git",
"bluesky",
"event-model",
"p4p",
Expand Down
35 changes: 26 additions & 9 deletions src/ophyd_epics_devices/panda.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import atexit
import re
from enum import Enum
from typing import (
Expand Down Expand Up @@ -31,8 +32,6 @@
)
from p4p.client.thread import Context

ctxt = Context("pva")


class PulseBlock(Device):
delay: SignalRW[float]
Expand Down Expand Up @@ -98,8 +97,7 @@ def block_name_number(block_name: str) -> Tuple[str, int]:
return name, int(num or 1)


# in sim mode, this should be called with a timeout of 0.0.
async def pvi_get(pv: str, timeout: float = 5.0) -> Dict[str, PVIEntry]:
async def pvi_get(pv: str, ctxt: Context, timeout: float = 5.0) -> Dict[str, PVIEntry]:
pv_info: Dict[str, Dict[str, str]] = {}
try:
pv_info = ctxt.get(pv, timeout=timeout).get("pvi").todict()
Expand All @@ -115,13 +113,14 @@ async def pvi_get(pv: str, timeout: float = 5.0) -> Dict[str, PVIEntry]:


class PandA(Device):
_ctxt: Optional[Context] = None

pulse: DeviceVector[PulseBlock]
seq: DeviceVector[SeqBlock]
pcap: PcapBlock

def __init__(self, prefix: str, name: str = "") -> None:
self._init_prefix = prefix

self.pvi_mapping: Dict[FrozenSet[str], Callable[..., Signal]] = {
frozenset({"r", "w"}): lambda dtype, rpv, wpv: epics_signal_rw(
dtype, rpv, wpv
Expand All @@ -132,6 +131,20 @@ def __init__(self, prefix: str, name: str = "") -> None:
frozenset({"x"}): lambda dtype, rpv, wpv: epics_signal_x(wpv),
}

@property
def ctxt(self) -> Context:
if PandA._ctxt is None:
PandA._ctxt = Context("pva", nt=False)

@atexit.register
def _del_ctxt():
# If we don't do this we get messages like this on close:
# Error in sys.excepthook:
# Original exception was:
PandA._ctxt = None

return PandA._ctxt

def verify_block(self, name: str, num: int):
"""Given a block name and number, return information about a block."""
anno = get_type_hints(self).get(name)
Expand All @@ -156,7 +169,7 @@ async def _make_block(self, name: str, num: int, block_pv: str, sim: bool = Fals
block = self.verify_block(name, num)

field_annos = get_type_hints(block)
block_pvi = await pvi_get(block_pv) if not sim else None
block_pvi = await pvi_get(block_pv, self.ctxt) if not sim else None

# finds which fields this class actually has, e.g. delay, width...
for sig_name, sig_type in field_annos.items():
Expand Down Expand Up @@ -201,7 +214,7 @@ async def _make_untyped_block(self, block_pv: str):
included dynamically anyway.
"""
block = Device()
block_pvi = await pvi_get(block_pv)
block_pvi = await pvi_get(block_pv, self.ctxt)

for signal_name, signal_pvi in block_pvi.items():
signal_factory = self.pvi_mapping[frozenset(signal_pvi.keys())]
Expand Down Expand Up @@ -236,8 +249,12 @@ async def connect(self, sim=False) -> None:
If there's no pvi information, that's because we're in sim mode. In that case,
makes all required blocks.
"""
pvi = await pvi_get(self._init_prefix + ":PVI") if not sim else None
hints = get_type_hints(self)
pvi = await pvi_get(self._init_prefix + ":PVI", self.ctxt) if not sim else None
hints = {
attr_name: attr_type
for attr_name, attr_type in get_type_hints(self).items()
if not attr_name.startswith("_")
}

# create all the blocks pvi says it should have,
if pvi:
Expand Down
Loading

0 comments on commit e69eb6f

Please sign in to comment.