Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# per-greffon ``min_greffer_version`` compatibility gate can refuse deploying an
# L4 (or any version-floored) greffon onto a greffer too old to run it. Keep in
# sync with pyproject.toml. Single source for the register report.
__version__ = "0.3.9"
__version__ = "0.3.10"
21 changes: 21 additions & 0 deletions app/workers/heartbeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,23 @@ def _disk_free_bytes(settings: Settings) -> int | None:
return None


def _update_in_progress(settings: Settings) -> bool:
"""Whether a self-update currently holds the ``/data`` update lock.

Reports the SAME non-blocking lock probe the controller uses to 409
start/stop/update (HLD section 10), at the SAME path
(``$GREFFON_PATH/.update.lock``). The manager reads this to clear its
transient ``updating`` flag once an update finishes OR fails without
restarting the node (the updater dying mid-verify releases the lock but never
recreates the greffer, so no re-register fires). The probe itself fails safe
to False (cannot tell -> do not claim updating)."""
from pathlib import Path

from apps.utils.docker import updater

return updater.update_in_progress(Path(settings.greffon_path) / ".update.lock")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Do not let heartbeats contend with the update lock

When a heartbeat runs during the small window after /remote_update starts the detached updater but before the updater has acquired /data/.update.lock, this call can briefly take the same exclusive non-blocking flock used by apps.utils.docker.updater.update_in_progress. The updater entrypoint also uses LOCK_EX | LOCK_NB and treats any held lock as “another update” (cli/greffer_cli/updater/__main__.py:53-58), so a regular 5s heartbeat can make a valid manager-triggered update exit as refused before it starts.

Useful? React with 👍 / 👎.



def _read_meminfo() -> tuple[int | None, int | None]:
"""Host memory ``(used, total)`` in bytes from ``/proc/meminfo``, or
``(None, None)``.
Expand Down Expand Up @@ -202,6 +219,10 @@ def _one_heartbeat(app: FastAPI, seq: int) -> int:
"reasons": reasons,
"disk_free_bytes": _disk_free_bytes(settings),
"instances": status_map,
# Lets the manager clear its transient "updating" flag the moment a
# self-update finishes or fails (lock released), instead of waiting out
# its grace window (covers a failed update that never restarts the node).
"update_in_progress": _update_in_progress(settings),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Retry the host CLI lock path as well

When an operator runs greffer update while the daemon is otherwise idle, this new per-heartbeat probe briefly takes the same $GREFFON_PATH/.update.lock exclusive flock; the host CLI still does a single non-blocking flock in cli/greffer_cli/update.py:406-409 and returns EXIT_PREFLIGHT_REFUSED at cli/greffer_cli/update.py:517-521, so a valid manual update can be rejected just because it landed during a heartbeat. The in-container updater was given a bounded retry for this same probe race, but the host update path needs the same treatment or the heartbeat should avoid taking an exclusive lock.

Useful? React with 👍 / 👎.

}
# Host vitals (resource-monitoring epic, Feature 1). Read from /proc,
# independent of the docker status collection above, so they ride even a
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "greffer"
version = "0.3.9"
version = "0.3.10"
description = ""
authors = ["Greffon <admin@greffon.io>"]
license = "AGPL-3.0-only"
Expand Down
23 changes: 23 additions & 0 deletions tests/test_workers_heartbeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,29 @@ def test_one_heartbeat_posts_payload_with_token(
assert body["degraded"] is False
assert body["interval"] == settings.heartbeat_interval
assert "captured_at" in body and "uptime_s" in body
# No update lock held under a fresh tmp greffon_path -> reports not-updating.
assert body["update_in_progress"] is False


def test_one_heartbeat_reports_update_in_progress(
settings: Settings, tmp_path
) -> None:
"""When a self-update holds the /data lock, the beat reports it so the manager
can keep ``updating`` true (and later clear it when this flips back to False)."""
settings.greffon_path = tmp_path # type: ignore[misc]
app = create_app(token="hb-tok", settings=settings)
app.state.status_map = {"map": {}, "at": time.monotonic()}

with patch("app.workers.heartbeat.requests") as mock_requests, \
patch("apps.utils.docker.updater.update_in_progress",
return_value=True) as probe:
mock_requests.post.return_value.status_code = 200
_one_heartbeat(app, 1)

body = mock_requests.post.call_args.kwargs["json"]
assert body["update_in_progress"] is True
# Probes the SAME path the controller 409s on (HLD section 10).
assert probe.call_args.args[0] == tmp_path / ".update.lock"


def test_collect_or_reuse_uses_fresh_cache(settings: Settings) -> None:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_workers_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ def test_post_register_version_defaults_to_app_version(settings: Settings) -> No
from app import __version__

# Sanity: the fixture didn't set GREFFER_VERSION, so the default applies.
assert settings.greffer_version == __version__ == "0.3.9"
assert settings.greffer_version == __version__ == "0.3.10"
with patch("app.workers.register.requests") as mock_requests:
mock_requests.post.return_value.status_code = 200
_post_register(settings, "10.0.0.1", "tok")
kwargs = mock_requests.post.call_args.kwargs
assert kwargs["json"]["version"] == "0.3.9"
assert kwargs["json"]["version"] == "0.3.10"


def test_post_register_version_overridable_via_env(
Expand Down