Camera permission and review fixes (openpanel C3, C4)
C4: a device's `viewers` (superusers by default, or members) covers the image and video messages its ports publish. Anyone else gets none of their values on the socket, in the catalogue or a flow's state, 403 for their history, for the frames they hold (the last 8 included) and for TURN credentials, no bytes when asking for them, and `bye: forbidden` for a live view. A device credential stays bounded by its dashboards. The device page sets it in its Camera section, and a tile the viewer may not see says "No access to this camera" and asks the camera for nothing. C3: - A volatile upload is held to MAX_FRAME_BYTES (4 MiB, 413 above it, read without extra copies), and so is a frame on a device's socket. - A frame the engine holds in memory is written to the ring directory before a local worker is handed it, so load_artifact finds it. - A socket the bus dropped events for catches up with a snapshot, demand included, and a device's credential is checked again per frame, cached by the flow store revision and its nonce, so a lost event never keeps an unpaired device's frames coming. - A report is at most 64 KB once its named config_schemas are put in place. - A device away for 60 s has its true bool messages set to false. - Refused frames are logged by fixed keys, a frame is named after its message, `fluksio serve` waits 5 s for open connections, a tile with a stream URL creates no demand, the 8-name cap keeps the newest names. - Every container's log rotates (json-file, 3 x 10 MB). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0138N7k5PT4dcR1iEqXaaQ9k
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
02b736c9e8
commit
f679264b3a
@@ -306,6 +306,32 @@ class VolatileStore:
|
||||
return True
|
||||
return valid_digest(digest) and self._file(digest).is_file()
|
||||
|
||||
def spill(self, digest: str) -> None:
|
||||
"""Write a frame held in memory to the directory, for a worker here.
|
||||
|
||||
A node in this container opens a frame with ``load_artifact``, which
|
||||
looks in the directory: the worker is another process and cannot see
|
||||
this one's memory. Called before a call naming the frame. The
|
||||
directory's timer trims it like a worker's own frames.
|
||||
"""
|
||||
with self._lock:
|
||||
data = self._frames.get(digest)
|
||||
if data is None:
|
||||
return
|
||||
target = self._file(digest)
|
||||
if target.exists():
|
||||
return
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
handle = tempfile.NamedTemporaryFile(dir=target.parent, delete=False)
|
||||
try:
|
||||
with handle:
|
||||
handle.write(data)
|
||||
os.replace(handle.name, target)
|
||||
except BaseException:
|
||||
with contextlib.suppress(OSError):
|
||||
os.unlink(handle.name)
|
||||
raise
|
||||
|
||||
@property
|
||||
def size(self) -> int:
|
||||
"""Bytes held in memory."""
|
||||
|
||||
@@ -68,6 +68,9 @@ PORT_PATTERN = re.compile(r"^[a-z0-9_.]+$")
|
||||
#: The node type that stands for a device in a flow.
|
||||
DEVICE_TYPE = "device"
|
||||
|
||||
#: The dtypes a device's ``viewers`` setting covers: what a camera sends.
|
||||
CAMERA_DTYPES = frozenset({"image", "video"})
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Binding:
|
||||
@@ -104,6 +107,7 @@ class DeviceBinding(BaseModel):
|
||||
port: str
|
||||
message: str
|
||||
direction: Literal["in", "out"]
|
||||
dtype: str = ""
|
||||
|
||||
|
||||
def bindings_of(flow: FlowDef) -> list[Binding]:
|
||||
@@ -149,6 +153,11 @@ class DeviceDef(BaseModel):
|
||||
#: touched rather than pointed at. A fact about the screen, not about any
|
||||
#: dashboard on it.
|
||||
touch: bool = False
|
||||
#: Who may see what the device's camera sends, its ``image`` and ``video``
|
||||
#: ``in`` messages, their bytes and its live view: superusers only, or
|
||||
#: every member too. A device credential is bounded by its dashboards
|
||||
#: instead, which a superuser assigned.
|
||||
viewers: Literal["superusers", "members"] = "superusers"
|
||||
#: Bumped on every save. A save based on an older one is refused.
|
||||
version: int = 0
|
||||
|
||||
@@ -401,6 +410,9 @@ class DeviceStore:
|
||||
self._states_seen: (
|
||||
tuple[tuple[int, int, int], dict[str, DeviceState]] | None
|
||||
) = None
|
||||
#: The camera messages only superusers may see, at the flow store
|
||||
#: revision they were worked out at. A device save commits there too.
|
||||
self._restricted: tuple[int, frozenset[str]] | None = None
|
||||
|
||||
def _file(self, device_id: str) -> Path:
|
||||
return self.root / f"{checked(device_id)}.json"
|
||||
@@ -459,6 +471,30 @@ class DeviceStore:
|
||||
found.append(binding)
|
||||
return found
|
||||
|
||||
def restricted(self) -> frozenset[str]:
|
||||
"""The messages only a superuser may see: what the cameras of devices
|
||||
whose ``viewers`` are superusers publish, their ``image`` and ``video``
|
||||
``in`` ports.
|
||||
|
||||
Asked on every value a person's socket carries, so worked out again
|
||||
only once the flow store commits, which every device save does.
|
||||
"""
|
||||
revision = self.flows.revision
|
||||
held = self._restricted
|
||||
if held is not None and held[0] == revision:
|
||||
return held[1]
|
||||
wiring = self.wiring()
|
||||
found = frozenset(
|
||||
binding.message
|
||||
for device in self.list()
|
||||
if device.viewers == "superusers"
|
||||
for binding in wiring.get(device.id, ())
|
||||
if binding.direction == "in" and binding.dtype in CAMERA_DTYPES
|
||||
)
|
||||
# Keyed by the revision read before, so a commit meanwhile is seen.
|
||||
self._restricted = (revision, found)
|
||||
return found
|
||||
|
||||
def io(self, device_id: str) -> list[DeviceIo]:
|
||||
"""The ports as the device reads them."""
|
||||
return [
|
||||
@@ -481,6 +517,7 @@ class DeviceStore:
|
||||
port=b.port,
|
||||
message=b.message,
|
||||
direction=b.direction,
|
||||
dtype=b.dtype,
|
||||
)
|
||||
for b in self.wiring().get(device_id, [])
|
||||
),
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
|
||||
Nodes execute in a thread pool; websocket clients live on the event loop.
|
||||
Publishers are therefore thread-safe and never block: a subscriber that cannot
|
||||
keep up loses its oldest queued events rather than stalling the engine.
|
||||
keep up loses its oldest queued events rather than stalling the engine, and
|
||||
can ask whether it did (``dropped``) to catch up another way.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -49,6 +50,9 @@ class EventBus:
|
||||
#: may already have moved past, and a device's credential reaches
|
||||
#: these as well as the current one.
|
||||
self.digests: dict[str, deque[str]] = {}
|
||||
#: Subscribers that lost an event since they last asked. Touched on
|
||||
#: the event loop only, by the flush and the subscribers.
|
||||
self._dropped: set[asyncio.Queue[dict[str, Any]]] = set()
|
||||
|
||||
def bind(self, loop: asyncio.AbstractEventLoop) -> None:
|
||||
"""Attach the bus to the running event loop (called once at startup)."""
|
||||
@@ -108,8 +112,20 @@ class EventBus:
|
||||
queue.get_nowait()
|
||||
except asyncio.QueueEmpty:
|
||||
pass
|
||||
self._dropped.add(queue)
|
||||
queue.put_nowait(event)
|
||||
|
||||
def dropped(self, queue: asyncio.Queue[dict[str, Any]]) -> bool:
|
||||
"""Whether this subscriber lost events since it last asked.
|
||||
|
||||
A device's socket that did catches up with a snapshot: what it lost
|
||||
may have been its demand, or the event that tells it it was unpaired.
|
||||
"""
|
||||
if queue in self._dropped:
|
||||
self._dropped.discard(queue)
|
||||
return True
|
||||
return False
|
||||
|
||||
@asynccontextmanager
|
||||
async def subscribe(self) -> AsyncIterator[asyncio.Queue[dict[str, Any]]]:
|
||||
"""Yield a queue receiving every event published while subscribed."""
|
||||
@@ -119,6 +135,7 @@ class EventBus:
|
||||
yield queue
|
||||
finally:
|
||||
self._subscribers.discard(queue)
|
||||
self._dropped.discard(queue)
|
||||
|
||||
|
||||
event_bus = EventBus()
|
||||
|
||||
@@ -172,19 +172,21 @@ class LiveSessions:
|
||||
person: str,
|
||||
device: str,
|
||||
devices: Any,
|
||||
superuser: bool = False,
|
||||
) -> None:
|
||||
"""One signalling message from a socket.
|
||||
|
||||
``person`` is the id of the account holding the socket and ``device``
|
||||
the device whose credential does, one of them "". Any person may watch
|
||||
any device, camera privacy being deferred. A device speaks only for
|
||||
the sessions it was asked to serve on this socket.
|
||||
the device whose credential does, one of them "". A person watches a
|
||||
device whose ``viewers`` are members, or any as a ``superuser``. A
|
||||
device speaks only for the sessions it was asked to serve on this
|
||||
socket.
|
||||
"""
|
||||
session_id = str(message.get("session") or "")
|
||||
if not _SESSION.match(session_id):
|
||||
return
|
||||
if message.get("type") == "live" and person:
|
||||
self._from_viewer(message, session_id, endpoint, person, devices)
|
||||
self._from_viewer(message, session_id, endpoint, person, devices, superuser)
|
||||
elif message.get("type") == "signal" and device:
|
||||
self._from_device(message, session_id, endpoint, device)
|
||||
|
||||
@@ -197,6 +199,7 @@ class LiveSessions:
|
||||
endpoint: Endpoint,
|
||||
who: str,
|
||||
devices: Any,
|
||||
superuser: bool,
|
||||
) -> None:
|
||||
device_id = str(message.get("device") or "")
|
||||
action = message.get("action")
|
||||
@@ -205,7 +208,7 @@ class LiveSessions:
|
||||
if session is not None:
|
||||
# Said twice, or somebody else's id: neither gets a second one.
|
||||
return
|
||||
reason = self._refusal(device_id, devices)
|
||||
reason = self._refusal(device_id, devices, superuser)
|
||||
if reason:
|
||||
endpoint.deliver(
|
||||
_viewer_message(device_id, session_id, "bye", reason=reason)
|
||||
@@ -289,8 +292,12 @@ class LiveSessions:
|
||||
if not session.viewer.deliver(sent):
|
||||
self._end(session, "overflow", tell_viewer=False)
|
||||
|
||||
def _refusal(self, device_id: str, devices: Any) -> str:
|
||||
"""Why this device cannot serve a live view now, or ""."""
|
||||
def _refusal(self, device_id: str, devices: Any, superuser: bool) -> str:
|
||||
"""Why this device cannot serve this viewer a live view now, or ""."""
|
||||
if not superuser:
|
||||
found = devices.find(device_id) if devices is not None else None
|
||||
if found is None or found.viewers != "members":
|
||||
return "forbidden"
|
||||
if not self._devices.get(device_id):
|
||||
return "offline"
|
||||
reported = devices.reported_ports(device_id) if devices is not None else None
|
||||
|
||||
@@ -13,6 +13,8 @@ reads back as its ``io`` (see ``fluksio.flow.devices``).
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import threading
|
||||
from collections.abc import Iterable
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
@@ -21,10 +23,18 @@ from pydantic import BaseModel, Field
|
||||
from fluksio.flow.devices import PORT_PATTERN, presence
|
||||
from fluksio.flow.messages import MessageSpec
|
||||
from fluksio.flow.nodes.base import Node
|
||||
from fluksio.flow.pipeline import ValueSource
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from fastapi import FastAPI
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
#: How long a device is away before its flags read false: the floor before
|
||||
#: its node is shown degraded (``controller.HEALTH_FLOOR``), so a reconnect
|
||||
#: or a restart of this instance leaves them as they were.
|
||||
OFFLINE_GRACE_S = 60.0
|
||||
|
||||
|
||||
def _never(**_: Any) -> None:
|
||||
"""What the node would compute, were it ever run: nothing."""
|
||||
@@ -45,7 +55,7 @@ class DeviceNode(Node):
|
||||
),
|
||||
)
|
||||
|
||||
__slots__ = ("cfg",)
|
||||
__slots__ = ("_away", "cfg")
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -55,6 +65,8 @@ class DeviceNode(Node):
|
||||
name: str | None = None,
|
||||
):
|
||||
self.cfg = self.Params.model_validate(params or {})
|
||||
#: Counting down from the device going away to its flags reading false.
|
||||
self._away: threading.Timer | None = None
|
||||
super().__init__(
|
||||
f=_never, requires=requires, provides=provides, params=params, name=name
|
||||
)
|
||||
@@ -79,11 +91,49 @@ class DeviceNode(Node):
|
||||
async def stop(self, app: FastAPI | None = None) -> None:
|
||||
if self.cfg.device:
|
||||
presence.unwatch(self.cfg.device, self._presence)
|
||||
self._stay()
|
||||
|
||||
def _presence(self, online: bool) -> None:
|
||||
# Degraded rather than down: a panel switched off or asleep is a thing
|
||||
# to notice, not a fault of the flow wiring it.
|
||||
if online:
|
||||
self._stay()
|
||||
self.report_health("ok")
|
||||
else:
|
||||
self.report_health("degraded", "Offline")
|
||||
self._stay()
|
||||
self._away = threading.Timer(OFFLINE_GRACE_S, self._gone)
|
||||
self._away.daemon = True
|
||||
self._away.start()
|
||||
|
||||
def _stay(self) -> None:
|
||||
if self._away is not None:
|
||||
self._away.cancel()
|
||||
self._away = None
|
||||
|
||||
def _gone(self) -> None:
|
||||
"""The device has been away for the grace: its flags read false.
|
||||
|
||||
A ``bool`` it publishes says something is so right now, such as
|
||||
``person``, and nothing tells a device that went away to say it is
|
||||
no longer so. Credited to this node, as the device's own values are.
|
||||
"""
|
||||
pipeline = self._pipeline
|
||||
if presence.online(self.cfg.device) or pipeline is None:
|
||||
return
|
||||
flags = {
|
||||
spec.name: False
|
||||
for spec in self.output_ports
|
||||
if spec.name
|
||||
and spec.dtype.value == "bool"
|
||||
and pipeline.state.get(spec.name) is True
|
||||
}
|
||||
if not flags:
|
||||
return
|
||||
source = ValueSource(
|
||||
kind="node", id=self.id, label=self.cfg.device, detail="offline"
|
||||
)
|
||||
try:
|
||||
pipeline.publish(flags, source)
|
||||
except Exception:
|
||||
logger.exception("Could not reset the flags of '%s'", self.cfg.device)
|
||||
|
||||
@@ -24,13 +24,14 @@ import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from collections.abc import Callable
|
||||
from collections.abc import Callable, Iterator
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import orjson
|
||||
from fluksio_worker import worker_main as _worker_main
|
||||
|
||||
from fluksio.flow.artifacts import is_reference
|
||||
from fluksio.flow.events import EventBus
|
||||
from fluksio.flow.nodes.base import NodeOutputError
|
||||
|
||||
@@ -129,6 +130,19 @@ def _remote_class(name: str) -> type[RemoteError]:
|
||||
return cls
|
||||
|
||||
|
||||
def _digests(value: Any) -> Iterator[str]:
|
||||
"""Every artifact digest a node's inputs name, however deep."""
|
||||
if isinstance(value, dict):
|
||||
if is_reference(value):
|
||||
yield str(value["digest"])
|
||||
return
|
||||
for item in value.values():
|
||||
yield from _digests(item)
|
||||
elif isinstance(value, list):
|
||||
for item in value:
|
||||
yield from _digests(item)
|
||||
|
||||
|
||||
class _Worker:
|
||||
"""One subprocess, and the framing of one request/response over its pipes."""
|
||||
|
||||
@@ -237,10 +251,16 @@ class PythonWorkerPool:
|
||||
events: EventBus | None = None,
|
||||
env: dict[str, str] | None = None,
|
||||
max_rss_mb: int = 0,
|
||||
spill: Callable[[str], None] | None = None,
|
||||
) -> None:
|
||||
self.python = python
|
||||
self.size = size
|
||||
self.events = events
|
||||
# Called with each artifact a call's inputs name, before the call: a
|
||||
# frame the engine holds in memory is written where a worker in this
|
||||
# container finds it (``VolatileStore.spill``), since it is another
|
||||
# process.
|
||||
self.spill = spill
|
||||
# A worker holding more than this when its call returns is retired
|
||||
# rather than kept warm; 0 keeps them all. A library like JAX keeps what
|
||||
# it allocated for as long as the process lives, and warm is the point
|
||||
@@ -307,6 +327,7 @@ class PythonWorkerPool:
|
||||
events=self.events,
|
||||
env={**self.env, **extra},
|
||||
max_rss_mb=self.max_rss_mb,
|
||||
spill=self.spill,
|
||||
)
|
||||
child._generation = self._generation
|
||||
child.start()
|
||||
@@ -593,6 +614,9 @@ class PythonWorkerPool:
|
||||
run_id: str = "",
|
||||
on_event: Callable[[dict[str, Any]], None] | None = None,
|
||||
) -> Any:
|
||||
if self.spill is not None:
|
||||
for digest in _digests(kwargs):
|
||||
self.spill(digest)
|
||||
response = self._request(
|
||||
{
|
||||
"op": "run",
|
||||
|
||||
Reference in New Issue
Block a user