{{ message }}
Repository navigation
Expand file tree
/
Copy pathdaemon.py
More file actions
1654 lines (1370 loc) · 60.1 KB
/
Copy pathdaemon.py
File metadata and controls
1654 lines (1370 loc) · 60.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Multi-repo watch daemon for code-review-graph.
Reads ``~/.code-review-graph/watch.toml`` to configure which repositories
to watch, then spawns one ``code-review-graph watch`` child process per
repo. Monitors the config file for live changes (adding/removing repos)
and health-checks child processes, restarting any that die.
No external dependencies beyond Python stdlib — no tmux required.
"""
from __future__ import annotations
import hashlib
import json
import logging
import os
import shutil
import signal
import subprocess
import sys
import threading
import time
from collections.abc import Callable
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
if sys.version_info >= (3, 11):
import tomllib
else:
try:
import tomli as tomllib # type: ignore[no-redef]
except ImportError:
tomllib = None # type: ignore[assignment]
from .constants import crg_home, env_float
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Config file location
# ---------------------------------------------------------------------------
def default_config_path() -> Path:
"""Path to ``watch.toml`` under the per-user state directory."""
return crg_home() / "watch.toml"
def default_pid_path() -> Path:
"""Path to the daemon PID file."""
return crg_home() / "daemon.pid"
def default_state_path() -> Path:
"""Path to the persisted daemon state."""
return crg_home() / "daemon-state.json"
def default_log_dir() -> Path:
"""Directory for per-repo daemon logs."""
return crg_home() / "logs"
# These four were module-level constants built from Path.home(). They resolve
# per call now so $CRG_HOME can redirect them: an import-time constant is
# frozen before any caller — a test fixture, a sandboxed run — gets the chance
# to set the variable, which is how the test suite ended up writing into the
# real home directory of whoever ran it.
#
# The PEP 562 shim below keeps the old attribute names working for anything
# that already imported them: both ``daemon.CONFIG_PATH`` and
# ``from …daemon import CONFIG_PATH`` route through ``__getattr__``, and
# ``__dir__`` keeps them visible to introspection. Not covered: ``import *``
# (this module defines no ``__all__``, and adding one would change what the
# star exports for every other name) and static analysers, which cannot see
# dynamic attributes. Both are acceptable — these were never public API, and
# the alternative is deleting the names outright.
_LAZY_PATHS = {
"CONFIG_PATH": default_config_path,
"PID_PATH": default_pid_path,
"STATE_PATH": default_state_path,
}
def __getattr__(name: str) -> Path:
if name in _LAZY_PATHS:
return _LAZY_PATHS[name]()
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
def __dir__() -> list[str]:
"""Include the lazy names so ``dir()`` and tab-completion still find them.
``__getattr__`` alone covers attribute access and ``from … import X``,
but names absent from module globals are otherwise invisible to
``dir()``, ``from … import *`` and static analysers.
"""
return sorted(set(globals()) | set(_LAZY_PATHS))
_HEALTH_CHECK_INTERVAL = 30
# Restarting a watcher costs a full initial update, so a repo that cannot stay
# up must not be restarted every health check forever. The delay doubles per
# consecutive failure and resets once a watcher has stayed up this long.
_RESTART_BACKOFF_BASE = env_float("CRG_RESTART_BACKOFF", 30.0)
_RESTART_BACKOFF_MAX = env_float("CRG_RESTART_BACKOFF_MAX", 900.0)
_RESTART_HEALTHY_SECONDS = env_float("CRG_RESTART_HEALTHY_AFTER", 600.0)
# ---------------------------------------------------------------------------
# Dataclasses
# ---------------------------------------------------------------------------
@dataclass
class WatchRepo:
"""A single repository to watch."""
path: str
"""Resolved absolute path to the repository root."""
alias: str
"""Short name for this repo (derived from directory name when not specified)."""
@dataclass
class DaemonConfig:
"""Top-level daemon configuration."""
session_name: str = "crg-watch"
"""Logical daemon name (used in log messages and status output)."""
log_dir: Path = field(default_factory=default_log_dir)
"""Directory for per-repo log files."""
poll_interval: int = 2
"""Seconds between file-system polls for config changes."""
repos: list[WatchRepo] = field(default_factory=list)
"""Repositories the daemon watches."""
# ---------------------------------------------------------------------------
# Loading
# ---------------------------------------------------------------------------
def load_config(path: Path | None = None) -> DaemonConfig:
"""Load daemon configuration from a TOML file.
Args:
path: Explicit config path. Falls back to :func:`default_config_path`.
Returns:
A fully-validated :class:`DaemonConfig`.
Raises:
RuntimeError: If ``tomllib`` / ``tomli`` is unavailable on Python < 3.11.
"""
if tomllib is None:
raise RuntimeError(
"TOML parsing requires the 'tomli' package on Python < 3.11. "
"Install it with: pip install tomli"
)
config_path = path or default_config_path()
if not config_path.exists():
logger.info("Config file not found at %s — using defaults", config_path)
return DaemonConfig()
with open(config_path, "rb") as fh:
raw: dict[str, Any] = tomllib.load(fh)
# -- [daemon] section ---------------------------------------------------
daemon_section: dict[str, Any] = raw.get("daemon", {})
session_name: str = daemon_section.get("session_name", "crg-watch")
log_dir = Path(daemon_section.get("log_dir", str(DaemonConfig().log_dir)))
poll_interval: int = int(daemon_section.get("poll_interval", 2))
# -- [[repos]] array ----------------------------------------------------
repos: list[WatchRepo] = []
seen_aliases: set[str] = set()
for entry in raw.get("repos", []):
repo_path_str: str = entry.get("path", "")
if not repo_path_str:
logger.warning("Skipping repo entry with empty path")
continue
repo_path = Path(repo_path_str).expanduser().resolve()
if not repo_path.is_dir():
logger.warning("Skipping repo %s — directory does not exist", repo_path)
continue
has_repo_marker = (
(repo_path / ".git").exists()
or (repo_path / ".svn").exists()
or (repo_path / ".code-review-graph").exists()
)
if not has_repo_marker:
logger.warning(
"Skipping repo %s — no .git, .svn, or .code-review-graph directory found",
repo_path,
)
continue
alias: str = entry.get("alias", "") or repo_path.name
if alias in seen_aliases:
logger.warning("Skipping duplicate alias '%s' for repo %s", alias, repo_path)
continue
seen_aliases.add(alias)
repos.append(WatchRepo(path=str(repo_path), alias=alias))
return DaemonConfig(
session_name=session_name,
log_dir=log_dir,
poll_interval=poll_interval,
repos=repos,
)
# ---------------------------------------------------------------------------
# Saving
# ---------------------------------------------------------------------------
# TOML basic strings have named escapes for these; everything else in
# the control range must use the \uXXXX form.
_TOML_SHORT_ESCAPES = {
"\\": "\\\\",
'"': '\\"',
"\b": "\\b",
"\t": "\\t",
"\n": "\\n",
"\f": "\\f",
"\r": "\\r",
}
def _toml_str(value: object) -> str:
"""Render *value* as a TOML basic string.
Backslashes and double quotes are escape characters in TOML basic
strings, so Windows paths like ``C:\\Users\\x`` must be escaped or
the file fails to parse on the next load. Control characters
(U+0000-U+001F, U+007F) are forbidden unescaped by the TOML spec,
so they are escaped too — ``tomllib`` rejects the file otherwise.
"""
chars: list[str] = []
for ch in str(value):
esc = _TOML_SHORT_ESCAPES.get(ch)
if esc is not None:
chars.append(esc)
elif ord(ch) < 0x20 or ord(ch) == 0x7F:
chars.append(f"\\u{ord(ch):04X}")
else:
chars.append(ch)
return '"' + "".join(chars) + '"'
def _serialize_toml(config: DaemonConfig) -> str:
"""Serialize a :class:`DaemonConfig` to TOML text.
``tomllib`` is read-only, so we build the TOML manually.
"""
lines: list[str] = [
"[daemon]",
f"session_name = {_toml_str(config.session_name)}",
f"log_dir = {_toml_str(config.log_dir)}",
f"poll_interval = {config.poll_interval}",
]
for repo in config.repos:
lines.append("")
lines.append("[[repos]]")
lines.append(f"path = {_toml_str(repo.path)}")
lines.append(f"alias = {_toml_str(repo.alias)}")
lines.append("") # trailing newline
return "\n".join(lines)
def save_config(config: DaemonConfig, path: Path | None = None) -> None:
"""Write *config* back to a TOML file.
Creates parent directories if they do not exist.
Args:
config: The daemon configuration to persist.
path: Explicit config path. Falls back to :func:`default_config_path`.
"""
config_path = path or default_config_path()
config_path.parent.mkdir(parents=True, exist_ok=True)
config_path.write_text(_serialize_toml(config), encoding="utf-8")
logger.info("Config saved to %s", config_path)
# ---------------------------------------------------------------------------
# Convenience helpers (used by CLI commands)
# ---------------------------------------------------------------------------
def add_repo_to_config(
repo_path: str,
alias: str | None = None,
config_path: Path | None = None,
) -> DaemonConfig:
"""Add a repository to the daemon config and persist the change.
Args:
repo_path: Path to the repository (will be resolved to absolute).
alias: Optional short name. Derived from dirname if *None*.
config_path: Explicit config file path. Falls back to :func:`default_config_path`.
Returns:
The updated :class:`DaemonConfig`.
Raises:
ValueError: If the path is not a valid repository directory.
"""
resolved = Path(repo_path).expanduser().resolve()
if not resolved.is_dir():
raise ValueError(f"Not a directory: {resolved}")
has_repo_marker = (
(resolved / ".git").exists()
or (resolved / ".svn").exists()
or (resolved / ".code-review-graph").exists()
)
if not has_repo_marker:
raise ValueError(f"No .git, .svn, or .code-review-graph directory in {resolved}")
effective_alias = alias or resolved.name
config = load_config(config_path)
# Check for duplicate path or alias
for existing in config.repos:
if existing.path == str(resolved):
logger.warning("Repo %s is already configured — skipping", resolved)
return config
if existing.alias == effective_alias:
raise ValueError(f"Alias '{effective_alias}' is already in use by {existing.path}")
config.repos.append(WatchRepo(path=str(resolved), alias=effective_alias))
save_config(config, config_path)
return config
def remove_repo_from_config(
path_or_alias: str,
config_path: Path | None = None,
) -> DaemonConfig:
"""Remove a repository from the daemon config by path or alias.
Args:
path_or_alias: Either the absolute/relative repo path or its alias.
config_path: Explicit config file path. Falls back to :func:`default_config_path`.
Returns:
The updated :class:`DaemonConfig`.
"""
config = load_config(config_path)
resolved = str(Path(path_or_alias).expanduser().resolve())
original_count = len(config.repos)
config.repos = [r for r in config.repos if r.path != resolved and r.alias != path_or_alias]
if len(config.repos) == original_count:
logger.warning(
"No repo matching '%s' found in config — nothing removed",
path_or_alias,
)
else:
save_config(config, config_path)
return config
# ---------------------------------------------------------------------------
# PID file management
# ---------------------------------------------------------------------------
# A PID on its own is not an identity. PIDs are recycled, so a stale
# ``daemon.pid`` naming a number the kernel has since handed to something else
# made ``start`` refuse to run and ``stop`` SIGTERM then SIGKILL a stranger.
#
# The identity is an exclusive advisory lock the daemon holds for its whole
# lifetime. The kernel releases it the instant the process dies — including a
# SIGKILL or an OOM kill, where no cleanup code of ours ever runs — so "the
# lock is free" is proof that our daemon is gone, whatever the PID file says.
# ``flock`` is used rather than ``lockf`` deliberately: flock locks belong to
# the open file description, so probing with a second descriptor cannot
# silently drop a lock this process already holds.
_lock_handles: dict[str, Any] = {}
class DaemonAlreadyRunningError(RuntimeError):
"""A live process already holds the daemon lock, so this one must not run.
``is_daemon_running`` is a look, not a claim, and two ``crg-daemon start``
invocations a millisecond apart both used to pass it. The lock is the only
thing that can decide between them, so refusing to start is expressed as an
exception out of the call that takes it rather than a boolean a caller can
ignore.
"""
def __init__(self, pid: int | None, lock_path: Path) -> None:
self.pid = pid
self.lock_path = lock_path
holder = f"PID {pid}" if pid is not None else "an unrecorded PID"
super().__init__(
f"another code-review-graph daemon ({holder}) holds {lock_path}"
)
def daemon_lock_path(path: Path | None = None) -> Path:
"""Companion lock file for a PID file.
Kept separate so ``daemon.pid`` stays exactly what every other tool
expects it to be: one integer and nothing else.
"""
return (path or default_pid_path()).with_suffix(".lock")
def _flock_supported() -> bool:
return sys.platform != "win32"
def _flock(handle: Any, blocking: bool = False) -> bool:
"""Take an exclusive lock on *handle*. Returns False when it is held."""
import fcntl
flags = fcntl.LOCK_EX if blocking else fcntl.LOCK_EX | fcntl.LOCK_NB
try:
fcntl.flock(handle.fileno(), flags)
except OSError:
return False
return True
def acquire_daemon_lock(path: Path | None = None) -> bool:
"""Claim the daemon lock for this process, and hold it until it exits.
Returns True when this process owns the lock afterwards (including when it
already did), False when another live process holds it. On platforms
without ``flock`` this is a no-op that reports success, and identity falls
back to the process command line.
"""
if not _flock_supported():
return True
lock_path = daemon_lock_path(path)
key = str(lock_path)
if key in _lock_handles:
return True
lock_path.parent.mkdir(parents=True, exist_ok=True)
handle = open(lock_path, "a+") # noqa: SIM115 - held for the process lifetime
if not _flock(handle):
handle.close()
return False
_lock_handles[key] = handle
return True
def claim_daemon_lock(path: Path | None = None) -> None:
"""Take the daemon lock, or refuse and name the process that holds it.
Call this before doing anything a second daemon must not do — forking,
spawning watchers, writing the PID file. Under ``flock`` the lock belongs
to the open file description, so a daemon that forks after claiming keeps
holding it once the parents exit.
On Windows ``acquire_daemon_lock`` is a no-op that reports success, so this
never refuses there and two simultaneous starts are still possible.
"""
if not acquire_daemon_lock(path):
raise DaemonAlreadyRunningError(read_pid(path), daemon_lock_path(path))
def release_daemon_lock(path: Path | None = None) -> None:
"""Drop the daemon lock this process holds, if any."""
handle = _lock_handles.pop(str(daemon_lock_path(path)), None)
if handle is not None:
try:
handle.close() # closing the description releases the flock
except OSError: # pragma: no cover - best-effort cleanup
pass
def daemon_lock_held(path: Path | None = None) -> bool:
"""True when some live process holds the daemon lock."""
if not _flock_supported(): # pragma: no cover - POSIX in CI
return True
key = str(daemon_lock_path(path))
if key in _lock_handles:
return True # this process is the daemon
lock_path = daemon_lock_path(path)
if not lock_path.exists():
return False
try:
probe = open(lock_path, "a+") # noqa: SIM115 - closed below
except OSError: # pragma: no cover - unreadable state dir
return False
try:
if _flock(probe):
return False # nobody held it; our probe did, and now releases it
return True
finally:
probe.close()
def _process_command(pid: int) -> str | None:
"""The command line of *pid*, or None when it cannot be determined.
Windows returns None, and ``_looks_like_our_daemon`` reads that as "keep
the old behaviour", so none of the PID-identity work below reaches a
Windows user: there is no ``flock`` to fall back from either, so a PID
file naming any live process is still adopted there. What that costs is
written out in :func:`_looks_like_our_daemon`. Closing it needs a real
command line, which Windows exposes only through WMI or
``NtQueryInformationProcess`` — neither is a change to make blind, with no
Windows machine to test it on.
"""
if sys.platform == "win32": # pragma: no cover - POSIX in CI
return None
try:
result = subprocess.run(
# -ww: never truncate, so a long repository path still matches.
["ps", "-ww", "-p", str(pid), "-o", "command="],
capture_output=True,
text=True,
timeout=5,
check=False,
stdin=subprocess.DEVNULL,
)
except (OSError, subprocess.SubprocessError): # pragma: no cover - no ps
return None
if result.returncode != 0:
return None
return result.stdout.strip() or None
def _looks_like_our_daemon(pid: int) -> bool:
"""Fallback identity check for platforms without ``flock``.
On Windows this always returns True, because ``_process_command`` cannot
answer there. A Windows user therefore keeps the pre-existing behaviour in
full: ``crg-daemon status`` reports a daemon whenever ``daemon.pid`` names
a live process, and ``crg-daemon stop`` signals it, even when the PID was
recycled and now belongs to something unrelated. For the same reason
``claim_daemon_lock`` cannot exclude a second daemon on Windows — see
:func:`acquire_daemon_lock`.
"""
command = _process_command(pid)
if command is None: # pragma: no cover - cannot tell; keep old behaviour
return True
return "crg-daemon" in command or "code_review_graph.daemon" in command
def write_pid(pid: int | None = None, path: Path | None = None) -> None:
"""Claim the daemon lock, then write the current (or given) PID.
The lock is the exclusion; the PID file is only a label on it. Discarding
the result of :func:`acquire_daemon_lock` here meant the new lock could
identify the daemon but never exclude a second one: two ``crg-daemon
start`` invocations that both raced past ``is_daemon_running`` each wrote
the PID file and each went on running, one of them invisible to every
later ``status`` and ``stop``.
Raises:
DaemonAlreadyRunningError: another live process holds the lock. The PID
file is left exactly as its owner wrote it.
"""
pid_path = path or default_pid_path()
pid_path.parent.mkdir(parents=True, exist_ok=True)
claim_daemon_lock(path)
pid_path.write_text(str(pid or os.getpid()), encoding="utf-8")
def read_pid(path: Path | None = None) -> int | None:
"""Read the daemon PID from disk. Returns None if missing/invalid."""
pid_path = path or default_pid_path()
if not pid_path.exists():
return None
try:
return int(pid_path.read_text(encoding="utf-8").strip())
except (ValueError, OSError):
return None
def clear_pid(path: Path | None = None) -> None:
"""Remove the PID file and release the daemon lock."""
pid_path = path or default_pid_path()
release_daemon_lock(path)
try:
pid_path.unlink(missing_ok=True)
except OSError:
pass
# Win32 constants for the OpenProcess-based liveness check (#511).
_PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
_SYNCHRONIZE = 0x00100000
_ERROR_ACCESS_DENIED = 5
_WAIT_OBJECT_0 = 0x0
_WAIT_FAILED = 0xFFFFFFFF
def _pid_alive_windows(
pid: int,
kernel32: Any,
get_last_error: Callable[[], int] | None = None,
) -> bool:
"""Win32 PID liveness check via OpenProcess/WaitForSingleObject.
The access mask must include SYNCHRONIZE: a handle opened with only
PROCESS_QUERY_LIMITED_INFORMATION cannot be waited on, so
WaitForSingleObject returns WAIT_FAILED (ERROR_ACCESS_DENIED) and
every exited process reads as alive.
The kernel32 interface is injected so tests can drive handle/wait
outcomes on non-Windows platforms. *get_last_error* defaults to
``kernel32.GetLastError``; the real caller passes
``ctypes.get_last_error`` (reliable with ``use_last_error=True``).
"""
if get_last_error is None:
get_last_error = kernel32.GetLastError
handle = kernel32.OpenProcess(_PROCESS_QUERY_LIMITED_INFORMATION | _SYNCHRONIZE, False, pid)
if not handle:
# NULL handle: process is dead, or we lack access. ACCESS_DENIED
# means it exists but is owned by another user — treat as alive.
return get_last_error() == _ERROR_ACCESS_DENIED
try:
result = kernel32.WaitForSingleObject(handle, 0)
if result == _WAIT_FAILED:
# The wait itself errored — we cannot prove the process dead,
# so err alive, consistent with the ACCESS_DENIED branch.
logger.debug(
"WaitForSingleObject on PID %d failed (error %d); presuming alive",
pid,
get_last_error(),
)
return True
# WAIT_OBJECT_0 means the process handle is signaled (it exited).
return result != _WAIT_OBJECT_0
finally:
kernel32.CloseHandle(handle)
def pid_alive(pid: int) -> bool:
"""Cross-platform check whether a process with *pid* is running.
On Windows ``os.kill(pid, 0)`` routes to GenerateConsoleCtrlEvent and
raises ``OSError`` (WinError 87) for alive PIDs outside the caller's
console process group (#511), so the Win32 API is used instead.
"""
if sys.platform == "win32":
import ctypes
from ctypes import wintypes
kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
# Explicit prototypes: ctypes otherwise defaults every argument and
# return value to c_int, which truncates 64-bit HANDLEs and returns
# WAIT_FAILED as -1 — never equal to the unsigned 0xFFFFFFFF constant.
kernel32.OpenProcess.argtypes = (wintypes.DWORD, wintypes.BOOL, wintypes.DWORD)
kernel32.OpenProcess.restype = wintypes.HANDLE
kernel32.WaitForSingleObject.argtypes = (wintypes.HANDLE, wintypes.DWORD)
kernel32.WaitForSingleObject.restype = wintypes.DWORD
kernel32.CloseHandle.argtypes = (wintypes.HANDLE,)
kernel32.CloseHandle.restype = wintypes.BOOL
return _pid_alive_windows(pid, kernel32, ctypes.get_last_error)
try:
os.kill(pid, 0) # signal 0 = existence check
return True
except ProcessLookupError:
return False
except PermissionError:
return True # process exists but owned by another user
except OSError as exc:
# Unexpected platform quirk — treat as not alive rather than crash.
logger.debug("PID %d liveness check failed: %s", pid, exc)
return False
def is_daemon_running(path: Path | None = None) -> bool:
"""Check whether *our* daemon process is alive.
A live PID is necessary but not sufficient: PIDs are recycled, and the
file outlives the process that wrote it. The daemon holds an exclusive
lock for its whole lifetime, so a PID file naming a live process that does
not hold that lock names a stranger, and is cleared rather than adopted.
Without it, ``stop`` signalled that stranger.
"""
pid = read_pid(path)
if pid is None:
return False
if not pid_alive(pid):
clear_pid(path) # stale PID file — clean up
return False
identified = daemon_lock_held(path) if _flock_supported() else _looks_like_our_daemon(pid)
if identified:
return True
logger.info(
"PID file names live process %d, which is not this daemon "
"(it holds no daemon lock); treating the file as stale",
pid,
)
clear_pid(path)
return False
# ---------------------------------------------------------------------------
# Child state persistence (for cross-process status queries)
# ---------------------------------------------------------------------------
def load_state(path: Path | None = None) -> dict[str, Any]:
"""Load persisted child process state from disk.
Returns a dict mapping alias to ``{"pid": int, "path": str}``.
Returns an empty dict if the file is missing or corrupt.
"""
state_path = path or default_state_path()
if not state_path.exists():
return {}
try:
return json.loads(state_path.read_text(encoding="utf-8")) # type: ignore[no-any-return]
except (json.JSONDecodeError, OSError):
return {}
def _is_pid_alive(pid: int) -> bool:
"""Check whether a process with the given PID is running."""
return pid_alive(pid)
def clear_state(path: Path | None = None) -> None:
"""Remove the persisted child-state file."""
state_path = path or default_state_path()
try:
state_path.unlink(missing_ok=True)
except OSError: # pragma: no cover - best-effort cleanup
pass
def is_our_watcher(pid: int, repo_path: str) -> bool:
"""Is *pid* really a ``code-review-graph watch`` child for *repo_path*?
Same reasoning as the daemon PID file: signalling a number recorded on
disk is only safe once something ties that number to the process we mean.
The command line is that tie, and it is checked first — a health file
naming a PID proves only that some watcher once had it, and PIDs are
recycled, so trusting it alone could both block a legitimate start and
send SIGKILL to a stranger. The health file is the fallback for platforms
with no ``ps``, and there only while its heartbeat is still fresh.
"""
command = _process_command(pid)
if command is not None:
return (
"watch" in command
and ("code-review-graph" in command or "code_review_graph" in command)
and repo_path in command
)
health = read_watch_health(repo_path) # pragma: no cover - POSIX in CI
return (
isinstance(health, dict)
and health.get("pid") == pid
and not health.get("stalled")
)
def find_orphaned_watchers(
state_path: Path | None = None,
) -> list[dict[str, Any]]:
"""Watcher children still running with no daemon left to manage them.
The watchers are plain ``subprocess.Popen`` children in the daemon's
session, so a SIGKILLed daemon leaves every one of them running. Nothing
used to look for them: ``status`` reported "not running" and listed
nothing while a live watcher kept writing ``graph.db``, and the next
``start`` added a second watcher per repository — two writers on one
database, one of them invisible.
"""
orphans: list[dict[str, Any]] = []
for alias, entry in load_state(state_path).items():
if not isinstance(entry, dict):
continue
pid = entry.get("pid")
repo_path = str(entry.get("path") or "")
if not isinstance(pid, int) or not repo_path:
continue
if not _is_pid_alive(pid):
continue
if not is_our_watcher(pid, repo_path):
logger.info(
"PID %d recorded for '%s' is no longer our watcher; leaving it alone",
pid,
alias,
)
continue
orphans.append({"alias": alias, "pid": pid, "path": repo_path})
return orphans
def reap_orphaned_watchers(
state_path: Path | None = None,
*,
timeout: float = 5.0,
) -> list[dict[str, Any]]:
"""Terminate every orphaned watcher and forget the state that named them.
Returns the entries that were signalled, so the caller can report them.
"""
orphans = find_orphaned_watchers(state_path)
for orphan in orphans:
pid = int(orphan["pid"])
logger.info(
"Reaping orphaned watcher for '%s' (PID %d)", orphan["alias"], pid
)
clear_watch_health(orphan["path"])
_signal_until_dead(pid, timeout=timeout)
if orphans:
clear_state(state_path)
return orphans
def _signal_until_dead(pid: int, *, timeout: float = 5.0) -> bool:
"""SIGTERM, then SIGKILL, then confirm. Returns True when the PID is gone."""
for sig in (signal.SIGTERM, getattr(signal, "SIGKILL", signal.SIGTERM)):
try:
os.kill(pid, sig)
except ProcessLookupError:
return True
except PermissionError: # pragma: no cover - another user's process
logger.warning("Not permitted to signal PID %d", pid)
return False
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
if not _is_pid_alive(pid):
return True
time.sleep(0.1)
return not _is_pid_alive(pid)
# ---------------------------------------------------------------------------
# Watcher health (written by each watch child, read here)
# ---------------------------------------------------------------------------
# A watcher whose observer thread died keeps its process alive, so ``poll()``
# alone reports it healthy forever. Each watch child publishes its observer
# state and last-event time here instead; anything older than this is a stall.
# See: #811.
_WATCH_HEALTH_STALE_SECONDS = env_float("CRG_WATCH_HEALTH_STALE", 90.0)
def watch_health_dir() -> Path:
"""Directory holding one health file per watched repository."""
return crg_home() / "watch-health"
def watch_health_path(repo_root: str | Path) -> Path:
"""Health file for *repo_root*.
Keyed by the real path so the watch child and the daemon agree even when
one of them was handed a symlinked or relative path. Deliberately free of
heavy imports: ``crg-daemon status`` must stay instant.
"""
key = os.path.realpath(os.path.expanduser(str(repo_root)))
digest = hashlib.sha256(key.encode("utf-8", "surrogateescape")).hexdigest()[:16]
return watch_health_dir() / f"{digest}.json"
def read_watch_health(
repo_root: str | Path,
*,
stale_after: float = _WATCH_HEALTH_STALE_SECONDS,
) -> dict[str, Any] | None:
"""Read a watcher's published health, or None when it never published any.
The returned dict gains ``age`` (seconds since the last heartbeat) and
``stalled`` (observer reported dead, or heartbeat too old).
"""
try:
raw = watch_health_path(repo_root).read_text(encoding="utf-8")
data = json.loads(raw)
except (OSError, json.JSONDecodeError, ValueError):
return None
if not isinstance(data, dict):
return None
updated_at = data.get("updated_at")
age = time.time() - updated_at if isinstance(updated_at, (int, float)) else None
data["age"] = age
data["stalled"] = bool(
data.get("observer_alive") is False or (age is not None and age > stale_after)
)
return data
def clear_watch_health(repo_root: str | Path) -> None:
"""Drop a watcher's health file (the daemon reaped or dropped the child)."""
try:
watch_health_path(repo_root).unlink(missing_ok=True)
except OSError: # pragma: no cover - best-effort cleanup
pass
def watcher_status(alive: bool, health: dict[str, Any] | None) -> str:
"""Summarise one watcher: ``dead``, ``stalled``, ``partial``, ``unknown``, ``ok``.
``partial`` means the watcher ran out of watch budget and fell back to a
coarser recursive watch — still watching everything, but no longer
filtering ignored trees.
"""
if not alive:
return "dead"
if health is None:
return "unknown"
if health.get("stalled"):
return "stalled"
return "partial" if health.get("degraded") else "ok"
def _health_fields(repo_path: str, alive: bool) -> dict[str, Any]:
"""Watcher-health columns for one repo entry in :meth:`WatchDaemon.status`."""
health = read_watch_health(repo_path)
return {
"watcher": watcher_status(alive, health),
"observer_alive": None if health is None else health.get("observer_alive"),
"last_event_at": None if health is None else health.get("last_event_at"),
"health_age": None if health is None else health.get("age"),
"degraded": None if health is None else bool(health.get("degraded")),
"phase": None if health is None else health.get("phase"),
}
# ---------------------------------------------------------------------------
# ConfigWatcher — monitors config file for live changes
# ---------------------------------------------------------------------------
class ConfigWatcher:
"""Watches the daemon config file for changes and triggers reconciliation."""
def __init__(
self,
config_path: Path,
callback: Callable[[], None],
poll_interval: int = 2,
) -> None:
self._config_path = config_path
self._callback = callback
self._poll_interval = poll_interval
self._observer: Any = None # watchdog Observer when available
self._last_mtime: float = 0.0
self._poll_thread: threading.Thread | None = None
self._stop_event: threading.Event = threading.Event()
# ------------------------------------------------------------------
# Public
# ------------------------------------------------------------------
def start(self) -> None:
"""Begin watching the config file for modifications."""
try:
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
watcher = self
class _Handler(FileSystemEventHandler): # type: ignore[misc]
def on_modified(self, event: Any) -> None:
if Path(event.src_path).resolve() == watcher._config_path.resolve():
watcher._on_config_changed()
handler = _Handler()
self._observer = Observer()
self._observer.schedule(
handler,
str(self._config_path.parent),
recursive=False,
)
self._observer.daemon = True
self._observer.start()
logger.info(
"Config watcher started (watchdog) for %s",
self._config_path,
)
except ImportError:
# Fallback to polling when watchdog is unavailable
logger.info(
"watchdog not available — falling back to polling for %s",
self._config_path,
)
self._start_polling()
def stop(self) -> None:
You can’t perform that action at this time.
