fix(monitor): clean up managed scan workers

This commit is contained in:
Zeusina committed 2026-10-10 12:38:23 +03:00
1 parent b2a41270a9
commit 405d6f42c9
4 files changed
+66 -9

No files matched your search

+1
View File
@@ -5,6 +5,7 @@
## Готово ## Готово
- **2026-10-10 remote acceptance:** target image built from pinned base and started in a fresh directory/state volume; host SOCKS port `11080` was free, old container stayed untouched. Initial managed status confirmed generation `0`, `groups=none`, no active candidate IDs, and `NFQWS2_ENABLE=0`. Automated frontend scan of `youtube.com` completed with `total=4323, working=0`; candidate check produced an empty `strategies` array, recorded one failed run, and bootstrap error caused monitor/entrypoint exit. No strategy was applied. New target remains stopped with its fresh, strategy-free state preserved. This exposed that managed bootstrap should scan the actual Player API host (`www.youtube.com`) and ordinary no-candidate errors must not terminate ongoing monitoring. Updated code/tests are in progress; rerun required. Old container and Xray were not changed. - **2026-10-10 remote acceptance:** target image built from pinned base and started in a fresh directory/state volume; host SOCKS port `11080` was free, old container stayed untouched. Initial managed status confirmed generation `0`, `groups=none`, no active candidate IDs, and `NFQWS2_ENABLE=0`. Automated frontend scan of `youtube.com` completed with `total=4323, working=0`; candidate check produced an empty `strategies` array, recorded one failed run, and bootstrap error caused monitor/entrypoint exit. No strategy was applied. New target remains stopped with its fresh, strategy-free state preserved. This exposed that managed bootstrap should scan the actual Player API host (`www.youtube.com`) and ordinary no-candidate errors must not terminate ongoing monitoring. Updated code/tests are in progress; rerun required. Old container and Xray were not changed.
- **2026-10-10 remote retry:** scanning the correct `www.youtube.com` host still produced `working=0`. Diagnosis: scan left NFQUEUE workers 200–207 in the isolated target namespace; the subsequent `check --no-conflict-cleanup` preserved those workers and every candidate immediately failed. A manual `check` after a standalone scan confirmed the queue-conflict failure. Stopped only the new target (old target/Xray untouched), preserving its empty state. Managed recovery now opts into blockcheck conflict cleanup because it runs only after stopping zapret in the dedicated isolated namespace; ordinary CLI callers retain `--no-conflict-cleanup`. Local test/build pass; remote rerun pending.
- Added `managed monitor` and supervision only in the dedicated target entrypoint: guarded target-owned DB/identity before singleton, policy 5m/3/30m with bounded CLI/env validation, owned-loopback Player API probe/discovery without direct fallback, fixed blockcheck/zapret argv with context cancellation/join. Startup bootstraps only without a qualified active frontend, tries saved scenarios first, then scans; unresolved GGC keeps monitoring; safety/DB errors stop it. SIGTERM waits for monitor restoration before stopping target-local zapret/SS. Local fake CLI/entrypoint tests are present. - Added `managed monitor` and supervision only in the dedicated target entrypoint: guarded target-owned DB/identity before singleton, policy 5m/3/30m with bounded CLI/env validation, owned-loopback Player API probe/discovery without direct fallback, fixed blockcheck/zapret argv with context cancellation/join. Startup bootstraps only without a qualified active frontend, tries saved scenarios first, then scans; unresolved GGC keeps monitoring; safety/DB errors stop it. SIGTERM waits for monitor restoration before stopping target-local zapret/SS. Local fake CLI/entrypoint tests are present.
- Hardened managed recovery artifacts: общий `revalidateCandidate` удаляет старый combined JSON перед каждым check, remove errors/missing/malformed report fail closed; scan shortlist проверяется на regular file и mtime не раньше whole-second scan start (production использует unique temp path). Добавлены fake-only regression tests stale/missing shortlist, stale combined, remove failure и infrastructure classification/restoration. Tests/build/network не запускались; verification deferred orchestrator, CLI/entrypoint не добавлены. - Hardened managed recovery artifacts: общий `revalidateCandidate` удаляет старый combined JSON перед каждым check, remove errors/missing/malformed report fail closed; scan shortlist проверяется на regular file и mtime не раньше whole-second scan start (production использует unique temp path). Добавлены fake-only regression tests stale/missing shortlist, stale combined, remove failure и infrastructure classification/restoration. Tests/build/network не запускались; verification deferred orchestrator, CLI/entrypoint не добавлены.
+37 -8
View File
@@ -17,6 +17,17 @@ import (
// the caller to read and remove. stdout/stderr receive the unmodified streams. // the caller to read and remove. stdout/stderr receive the unmodified streams.
// The caller controls cancellation through ctx; no overall deadline is imposed here. // The caller controls cancellation through ctx; no overall deadline is imposed here.
func runBlockcheck(ctx context.Context, binary, domain string, stdout, stderr io.Writer) (string, error) { func runBlockcheck(ctx context.Context, binary, domain string, stdout, stderr io.Writer) (string, error) {
return runBlockcheckWithConflictCleanup(ctx, binary, domain, stdout, stderr, false)
}
// Managed recovery has already stopped zapret inside its dedicated network
// namespace, so blockcheck may clean up its own scan workers before shortlist
// checks. Other callers preserve foreign nfqws2 processes by default.
func runManagedBlockcheck(ctx context.Context, binary, domain string, stdout, stderr io.Writer) (string, error) {
return runBlockcheckWithConflictCleanup(ctx, binary, domain, stdout, stderr, true)
}
func runBlockcheckWithConflictCleanup(ctx context.Context, binary, domain string, stdout, stderr io.Writer, allowConflictCleanup bool) (string, error) {
if !validDomain(domain) { if !validDomain(domain) {
return "", fmt.Errorf("invalid DNS hostname: %q", domain) return "", fmt.Errorf("invalid DNS hostname: %q", domain)
} }
@@ -44,8 +55,12 @@ func runBlockcheck(ctx context.Context, binary, domain string, stdout, stderr io
} }
} }
domain = strings.ToLower(domain) domain = strings.ToLower(domain)
err = runBlockcheckCommand(ctx, binary, workdir, []string{"--workers", "8", "scan", "--domain", domain, scanArgs := []string{"--workers", "8", "scan", "--domain", domain, "--protocols", "tls12,tls13", "--auto"}
"--protocols", "tls12,tls13", "--auto", "--no-conflict-cleanup", "--output", path}, stdout, stderr) if !allowConflictCleanup {
scanArgs = append(scanArgs, "--no-conflict-cleanup")
}
scanArgs = append(scanArgs, "--output", path)
err = runBlockcheckCommand(ctx, binary, workdir, scanArgs, stdout, stderr)
if err != nil { if err != nil {
return path, fmt.Errorf("scan artifacts in %s (output %s): %w", workdir, path, err) return path, fmt.Errorf("scan artifacts in %s (output %s): %w", workdir, path, err)
} }
@@ -55,9 +70,12 @@ func runBlockcheck(ctx context.Context, binary, domain string, stdout, stderr io
} }
// Leave shortlist semantics to blockcheckw. Only check's JSON contains // Leave shortlist semantics to blockcheckw. Only check's JSON contains
// repeated real-data measurements; scan stdout is not evidence. // repeated real-data measurements; scan stdout is not evidence.
err = runBlockcheckCommand(ctx, binary, workdir, []string{"--workers", "32", "check", "--from-file", vanilla, checkArgs := []string{"--workers", "32", "check", "--from-file", vanilla, "--domain", domain, "--take", "10", "--passes", "3", "--auto"}
"--domain", domain, "--take", "10", "--passes", "3", "--auto", "--no-conflict-cleanup", if !allowConflictCleanup {
"--output", path + ".check.json"}, stdout, stderr) checkArgs = append(checkArgs, "--no-conflict-cleanup")
}
checkArgs = append(checkArgs, "--output", path+".check.json")
err = runBlockcheckCommand(ctx, binary, workdir, checkArgs, stdout, stderr)
if err != nil { if err != nil {
return path, fmt.Errorf("check (vanilla %s, scan output %s, artifacts in %s): %w", vanilla, path, workdir, err) return path, fmt.Errorf("check (vanilla %s, scan output %s, artifacts in %s): %w", vanilla, path, workdir, err)
} }
@@ -120,6 +138,14 @@ func runCombinedBlockcheck(ctx context.Context, binary, path, args string, stdou
} }
func runCombinedBlockcheckForHost(ctx context.Context, binary, path, args, host string, stdout, stderr io.Writer) error { func runCombinedBlockcheckForHost(ctx context.Context, binary, path, args, host string, stdout, stderr io.Writer) error {
return runCombinedBlockcheckForHostWithConflictCleanup(ctx, binary, path, args, host, stdout, stderr, false)
}
func runManagedCombinedBlockcheckForHost(ctx context.Context, binary, path, args, host string, stdout, stderr io.Writer) error {
return runCombinedBlockcheckForHostWithConflictCleanup(ctx, binary, path, args, host, stdout, stderr, true)
}
func runCombinedBlockcheckForHostWithConflictCleanup(ctx context.Context, binary, path, args, host string, stdout, stderr io.Writer, allowConflictCleanup bool) error {
rows, err := combinedVanillaRowsForHost(args, host) rows, err := combinedVanillaRowsForHost(args, host)
if err != nil { if err != nil {
return err return err
@@ -131,9 +157,12 @@ func runCombinedBlockcheckForHost(ctx context.Context, binary, path, args, host
if err := os.WriteFile(input, []byte(rows), 0600); err != nil { if err := os.WriteFile(input, []byte(rows), 0600); err != nil {
return err return err
} }
if err := runBlockcheckCommand(ctx, binary, "", []string{"--workers", "32", "check", "--from-file", input, checkArgs := []string{"--workers", "32", "check", "--from-file", input, "--domain", host, "--take", "1", "--passes", "3", "--auto"}
"--domain", host, "--take", "1", "--passes", "3", "--auto", "--no-conflict-cleanup", if !allowConflictCleanup {
"--output", path + ".combined.json"}, stdout, stderr); err != nil { checkArgs = append(checkArgs, "--no-conflict-cleanup")
}
checkArgs = append(checkArgs, "--output", path+".combined.json")
if err := runBlockcheckCommand(ctx, binary, "", checkArgs, stdout, stderr); err != nil {
return fmt.Errorf("combined check (input %s, output %s): %w", input, path+".combined.json", err) return fmt.Errorf("combined check (input %s, output %s): %w", input, path+".combined.json", err)
} }
return nil return nil
+27
View File
@@ -200,6 +200,33 @@ func TestRunBlockcheckArgvAndStreams(t *testing.T) {
} }
} }
func TestRunManagedBlockcheckAllowsWorkerCleanup(t *testing.T) {
t.Setenv("TMPDIR", t.TempDir())
binary := fakeBlockcheck(t, "success")
var stdout bytes.Buffer
path, err := runManagedBlockcheck(context.Background(), binary, "example.com", &stdout, io.Discard)
if err != nil {
t.Fatal(err)
}
defer os.Remove(path)
defer os.Remove(path + ".check.json")
var scanArgs, checkArgs []string
decoder := json.NewDecoder(&stdout)
if err := decoder.Decode(&scanArgs); err != nil {
t.Fatal(err)
}
if err := decoder.Decode(&checkArgs); err != nil {
t.Fatal(err)
}
for _, args := range [][]string{scanArgs, checkArgs} {
for _, arg := range args {
if arg == "--no-conflict-cleanup" {
t.Fatalf("managed blockcheck retained no-cleanup flag: %q", args)
}
}
}
}
func TestRunBlockcheckValidation(t *testing.T) { func TestRunBlockcheckValidation(t *testing.T) {
for _, domain := range []string{"", "--auto", "a;touch x", "a/b", "a..b", "-a.com", strings.Repeat("a", 64) + ".com"} { for _, domain := range []string{"", "--auto", "a;touch x", "a/b", "a..b", "-a.com", strings.Repeat("a", 64) + ".com"} {
path, err := runBlockcheck(context.Background(), "nonexistent", domain, io.Discard, io.Discard) path, err := runBlockcheck(context.Background(), "nonexistent", domain, io.Discard, io.Discard)
+1 -1
View File
@@ -86,7 +86,7 @@ func managedMonitorCLIContext(ctx context.Context, args []string, stdout, stderr
return 1 return 1
} }
defer db.Close() defer db.Close()
recovery, err := managedMonitorRecoveryAdapters(target, db, runBlockcheck, runCombinedBlockcheckForHost, func(ctx context.Context, path string, args ...string) error { recovery, err := managedMonitorRecoveryAdapters(target, db, runManagedBlockcheck, runManagedCombinedBlockcheckForHost, func(ctx context.Context, path string, args ...string) error {
return runBlockcheckCommand(ctx, path, "", args, io.Discard, io.Discard) return runBlockcheckCommand(ctx, path, "", args, io.Discard, io.Discard)
}) })
if err != nil { if err != nil {