Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/embeddedci/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog — `embeddedci`

## 4.0.5

- **The pod's lease is released when the process exits.** A script that ends without `close()` (for
example `bp = BenchPod(...); bp.generate(...)`) used to leave the pod's lease held for its full TTL,
so every other client got `BP0025` for about two minutes. An `atexit` hook now releases the leases
the process still holds (quick, best effort, never blocks exit), and so does SIGTERM when the
application has not installed its own handler. A lease joined from `BENCHPOD_LEASE` is still the
runner's and is not released.

## 4.0.4

For BenchPod firmware 4.0.4 (features table revision 17).
Expand Down
2 changes: 1 addition & 1 deletion packages/embeddedci/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "embeddedci"
version = "4.0.4"
version = "4.0.5"
description = "BenchPod SDK and pytest plugin for EmbeddedCI hardware-in-the-loop testing"
readme = "README.md"
requires-python = ">=3.10"
Expand Down
73 changes: 67 additions & 6 deletions packages/embeddedci/src/embeddedci/benchpod/pod_lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@
the session raises :class:`~embeddedci.benchpod.errors.PodLeaseLostError` once, stops tagging and
renewing, and never takes the lease again.

A process that ends without calling ``close()`` releases its leases at exit (``atexit``, and on
SIGTERM when nothing else handles it), so the next client does not wait for the TTL.

Several :class:`~embeddedci.benchpod.BenchPod` objects of one process on the same pod share one
lease (a test opening its own pod next to the session's), so they never wait for each other.
"""

from __future__ import annotations

import atexit
import getpass
import logging
import os
import signal
import socket
import threading
import time
Expand All @@ -43,6 +48,8 @@
POLL_S = 2.0
HOLDER_MAX = 40
PURPOSE_MAX = 64
#: Longest an exit-time release may take per lease (connect + reply), and for the heartbeat join.
EXIT_RELEASE_TIMEOUT_S = 1.0

_registry: Dict[str, "PodLease"] = {}
_registry_lock = threading.Lock()
Expand Down Expand Up @@ -172,10 +179,19 @@ def take_pending_loss(self) -> Optional[PodLeaseLostError]:

# -- acquire, heartbeat, release -------------------------------------------------------------

def _command(self, req: Dict[str, Any]) -> Any:
def _command(self, req: Dict[str, Any], timeout: Optional[float] = None) -> Any:
"""One ``lease_*`` command on its own connection."""
transport = self._transport
sock = transport._dial(transport.timeout)
if timeout is None:
sock = transport._dial(transport.timeout)
else:
# Short budget (exit time): the connect retries for ``dial_timeout`` otherwise.
dial = transport.dial_timeout
transport.dial_timeout = min(dial, timeout)
try:
sock = transport._dial(timeout)
finally:
transport.dial_timeout = dial
try:
sock.sendall(encode_request(req))
reply = parse_reply(transport._recv_line_exact(sock))
Expand Down Expand Up @@ -250,20 +266,63 @@ def _renew(self) -> None:
except PodLeaseLostError as lost:
self.pending_lost = lost

def release(self) -> None:
"""Stop the heartbeat and free the lease (not a joined one). Never raises."""
def release(self, timeout: Optional[float] = None) -> None:
"""Stop the heartbeat and free the lease (not a joined one). Never raises.
``timeout`` (seconds) caps the heartbeat join and the release command."""
self._stop.set()
if self._thread is not None:
self._thread.join(timeout=2.0)
self._thread.join(timeout=2.0 if timeout is None else timeout)
self._thread = None
if self.held and not self.joined and not self.lost and self._transport is not None:
try:
self._command({"cmd": "lease_release", "id": self.lease_id})
self._command({"cmd": "lease_release", "id": self.lease_id}, timeout)
except Exception as exc: # noqa: BLE001 - best effort; it runs out on its own
_log.debug("pod lease release failed: %s", exc)
self.held = False


def release_all_at_exit() -> None:
"""Release every lease this process still holds (not joined ones). Quick and never raises:
a dead transport or an unplugged pod costs at most ``EXIT_RELEASE_TIMEOUT_S`` per lease."""
try:
with _registry_lock:
leases = list(_registry.values())
_registry.clear()
except Exception: # noqa: BLE001
return
for lease in leases:
try:
if lease.active and not lease.joined:
lease.release(timeout=EXIT_RELEASE_TIMEOUT_S)
except Exception as exc: # noqa: BLE001 - never hang or fail interpreter exit
_log.debug("pod lease release at exit failed: %s", exc)


_exit_hooks_installed = False


def _on_sigterm(signum: int, frame: Any) -> None:
release_all_at_exit()
signal.signal(signum, signal.SIG_DFL)
os.kill(os.getpid(), signum) # die of SIGTERM as before, so the exit status is unchanged


def _install_exit_hooks() -> None:
"""Once per process: the atexit release, and a SIGTERM release when SIGTERM still has its
default action (a handler the application installed is never replaced)."""
global _exit_hooks_installed
if _exit_hooks_installed:
return
_exit_hooks_installed = True
atexit.register(release_all_at_exit)
try:
if (threading.current_thread() is threading.main_thread()
and signal.getsignal(signal.SIGTERM) == signal.SIG_DFL):
signal.signal(signal.SIGTERM, _on_sigterm)
except (ValueError, OSError, AttributeError):
pass


def lease_key(transport: Any) -> str:
return str(getattr(transport, "addr", "") or id(transport))

Expand Down Expand Up @@ -300,6 +359,8 @@ def open_pod_lease(transport: Any, *, supported: bool, purpose: Optional[str], t
return None
lease._users = 1
_registry[key] = lease
if not lease.joined:
_install_exit_hooks()
transport.pod_lease = lease
return lease

Expand Down
27 changes: 27 additions & 0 deletions packages/embeddedci/tests/test_pod_lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,33 @@ def test_takes_tags_every_request_and_releases(pod):
assert pod.lease is None and not bp.leased


def test_exit_hook_releases_active_lease_and_skips_joined(pod, monkeypatch):
bp = _open(pod) # never closed: the process "ends"
lid = pod.lease["id"]
pod_lease.release_all_at_exit()
assert pod.cmds()[-1] == "lease_release" and pod.log[-1]["id"] == lid
assert pod.lease is None and not pod_lease._registry
pod_lease.release_all_at_exit() # twice is fine
assert pod.cmds().count("lease_release") == 1
bp.close()

monkeypatch.setenv("BENCHPOD_LEASE", "L-runner")
joined = _open(pod)
before = len(pod.log)
pod_lease.release_all_at_exit()
assert "lease_release" not in pod.cmds()[before:] and pod.lease["id"] == "L-runner"
joined.close()


def test_exit_hook_survives_a_dead_transport(pod):
bp = _open(pod)
pod.close() # the pod went away
t0 = time.monotonic()
pod_lease.release_all_at_exit() # must not raise or hang
assert time.monotonic() - t0 < 3.0 and not pod_lease._registry
bp.close()


def test_busy_waits_then_raises_with_the_holder(pod):
pod.hold("L-other", "benchpod ci: ward", "process-node-hil")
with pytest.raises(PodLeasedError) as info:
Expand Down