From 405d6f42c90ad2e648c50c84234725338742cfa6 Mon Sep 17 00:00:00 2001 From: Zeusina Date: Sat, 10 Oct 2026 12:38:23 +0300 Subject: [PATCH] fix(monitor): clean up managed scan workers --- PROGRESS.md | 1 + cmd/zapret-autotune/blockcheck.go | 45 ++++++++++++++++++---- cmd/zapret-autotune/blockcheck_test.go | 27 +++++++++++++ cmd/zapret-autotune/managed_monitor_cli.go | 2 +- 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/PROGRESS.md b/PROGRESS.md index cb3ba2a..47701bb 100644 --- a/PROGRESS.md +++ b/PROGRESS.md @@ -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 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. - 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 не добавлены. diff --git a/cmd/zapret-autotune/blockcheck.go b/cmd/zapret-autotune/blockcheck.go index 02b7ae4..70d3764 100644 --- a/cmd/zapret-autotune/blockcheck.go +++ b/cmd/zapret-autotune/blockcheck.go @@ -17,6 +17,17 @@ import ( // the caller to read and remove. stdout/stderr receive the unmodified streams. // 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) { + 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) { 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) - err = runBlockcheckCommand(ctx, binary, workdir, []string{"--workers", "8", "scan", "--domain", domain, - "--protocols", "tls12,tls13", "--auto", "--no-conflict-cleanup", "--output", path}, stdout, stderr) + scanArgs := []string{"--workers", "8", "scan", "--domain", domain, "--protocols", "tls12,tls13", "--auto"} + if !allowConflictCleanup { + scanArgs = append(scanArgs, "--no-conflict-cleanup") + } + scanArgs = append(scanArgs, "--output", path) + err = runBlockcheckCommand(ctx, binary, workdir, scanArgs, stdout, stderr) if err != nil { 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 // repeated real-data measurements; scan stdout is not evidence. - err = runBlockcheckCommand(ctx, binary, workdir, []string{"--workers", "32", "check", "--from-file", vanilla, - "--domain", domain, "--take", "10", "--passes", "3", "--auto", "--no-conflict-cleanup", - "--output", path + ".check.json"}, stdout, stderr) + checkArgs := []string{"--workers", "32", "check", "--from-file", vanilla, "--domain", domain, "--take", "10", "--passes", "3", "--auto"} + if !allowConflictCleanup { + checkArgs = append(checkArgs, "--no-conflict-cleanup") + } + checkArgs = append(checkArgs, "--output", path+".check.json") + err = runBlockcheckCommand(ctx, binary, workdir, checkArgs, stdout, stderr) if err != nil { 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 { + 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) if err != nil { 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 { return err } - if err := runBlockcheckCommand(ctx, binary, "", []string{"--workers", "32", "check", "--from-file", input, - "--domain", host, "--take", "1", "--passes", "3", "--auto", "--no-conflict-cleanup", - "--output", path + ".combined.json"}, stdout, stderr); err != nil { + checkArgs := []string{"--workers", "32", "check", "--from-file", input, "--domain", host, "--take", "1", "--passes", "3", "--auto"} + if !allowConflictCleanup { + 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 nil diff --git a/cmd/zapret-autotune/blockcheck_test.go b/cmd/zapret-autotune/blockcheck_test.go index 604c802..6dfc7d1 100644 --- a/cmd/zapret-autotune/blockcheck_test.go +++ b/cmd/zapret-autotune/blockcheck_test.go @@ -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) { 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) diff --git a/cmd/zapret-autotune/managed_monitor_cli.go b/cmd/zapret-autotune/managed_monitor_cli.go index 6d41e35..999e052 100644 --- a/cmd/zapret-autotune/managed_monitor_cli.go +++ b/cmd/zapret-autotune/managed_monitor_cli.go @@ -86,7 +86,7 @@ func managedMonitorCLIContext(ctx context.Context, args []string, stdout, stderr return 1 } 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) }) if err != nil {