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

+37 -8
View File
@@ -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
+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) {
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)
+1 -1
View File
@@ -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 {