fix(player): preserve private cancellation causes on shutdown

Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
ZeusinaandGPT-6 Bot committed 2026-10-11 09:16:08 +03:00
1 parent 3f9fd2f36f
commit d7397ed6cf
3 files changed
+207 -13

No files matched your search

+11 -1
View File
@@ -26,6 +26,12 @@ var discoveryDNSLabel = regexp.MustCompile(`^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])
type playerRunner func(context.Context, []string, io.Writer, io.Writer) error
// Keep request diagnostics private while retaining typed causes for supervision.
type playerRequestError struct{ cause error }
func (e *playerRequestError) Error() string { return "player request failed or exceeded output limit" }
func (e *playerRequestError) Unwrap() error { return e.cause }
func runPlayerCurl(ctx context.Context, args []string, stdout, stderr io.Writer) error {
cmd := exec.CommandContext(ctx, "curl", args...)
cmd.Stdout, cmd.Stderr = stdout, stderr
@@ -158,7 +164,11 @@ func requestPlayer(videoID, proxy string, runner playerRunner) ([]byte, error) {
err := runner(ctx, []string{"--disable", "--silent", "--show-error", "--fail", "--max-time", "30",
"--noproxy", "", "--proxy", proxy, "--user-agent", playerUserAgent, "--json", string(payload), playerURL}, output, diagnostics)
if err != nil || output.exceeded || diagnostics.exceeded {
return nil, errors.New("player request failed or exceeded output limit")
if output.exceeded || diagnostics.exceeded {
// An output failure remains unsafe even if the child was also canceled.
err = errors.Join(err, errors.New("player output limit exceeded"))
}
return nil, &playerRequestError{cause: err}
}
return body.Bytes(), nil
}
+1 -12
View File
@@ -179,16 +179,5 @@ func managedMonitorRecoveryAdapters(target managedDeps, db *sql.DB, scanCommand
// Joined cancellation plus failed cleanup/DB/lock close is not a graceful exit.
func managedCancellationOnly(err, cancellation error) bool {
if err == cancellation {
return true
}
if joined, ok := err.(interface{ Unwrap() []error }); ok {
for _, e := range joined.Unwrap() {
if !managedCancellationOnly(e, cancellation) {
return false
}
}
return len(joined.Unwrap()) != 0
}
return false
return err != nil && errors.Is(err, cancellation) && managedRecoveryTimeoutSafe(err)
}
@@ -0,0 +1,195 @@
package main
import (
"bytes"
"context"
"errors"
"io"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"testing"
"time"
)
// A real child, with no network: readiness triggers parent cancellation only
// after exec has started, so the request returns a genuine signal ExitError.
func TestPlayerCancellationChild(t *testing.T) {
if os.Getenv("AUTOTUNE_PLAYER_CANCEL_CHILD") != "1" {
return
}
io.WriteString(os.Stdout, "ready")
time.Sleep(time.Hour)
os.Exit(0)
}
type playerCancelWriter struct{ cancel context.CancelFunc }
func (w playerCancelWriter) Write(p []byte) (int, error) {
w.cancel()
return len(p), nil
}
func canceledPlayerProcess(t *testing.T, cancel context.CancelFunc, extra error, overflow string) playerRunner {
t.Helper()
return func(ctx context.Context, _ []string, out, diagnostics io.Writer) error {
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestPlayerCancellationChild$")
cmd.Env = append(os.Environ(), "AUTOTUNE_PLAYER_CANCEL_CHILD=1")
cmd.Stdout = playerCancelWriter{cancel: cancel}
cmd.Stderr = diagnostics
if overflow == "body" {
out.Write(make([]byte, playerResponseLimit+1))
} else if overflow == "diagnostics" {
diagnostics.Write(make([]byte, (64<<10)+1))
}
return errors.Join(cmd.Run(), extra)
}
}
func TestPlayerRequestCancellationCause(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
runner := canceledPlayerProcess(t, cancel, nil, "")
_, err := requestPlayer("abcdefghijk", "socks5://127.0.0.1:11080", func(_ context.Context, args []string, out, diagnostics io.Writer) error {
return runner(ctx, args, out, diagnostics)
})
var child *exec.ExitError
if !errors.As(err, &child) || !managedRecoveryTimeoutSafe(err) {
t.Fatalf("child cause lost: %v", err)
}
if !managedCancellationOnly(managedRecoveryContextError(ctx, err), context.Canceled) {
t.Fatal("clean canceled child classified as unsafe")
}
}
func TestPlayerRequestPublicErrorsOpaque(t *testing.T) {
const secret = "https://user:password@example.com/video?signature=private"
cause := errors.New(secret)
runner := func(_ context.Context, _ []string, _, diagnostics io.Writer) error {
io.WriteString(diagnostics, secret)
return cause
}
_, err := requestPlayer("abcdefghijk", "socks5://127.0.0.1:11080", runner)
if !errors.Is(err, cause) || strings.Contains(err.Error(), secret) || managedRecoveryTimeoutSafe(err) {
t.Fatal("opaque error lost its unsafe cause or exposed it")
}
t.Setenv("ZAPRET_AUTOTUNE_VIDEO_ID", "abcdefghijk")
for _, command := range []string{"health", "discovery"} {
var out, diagnostics bytes.Buffer
code := 0
if command == "health" {
code = healthYoutube(&out, &diagnostics, runner)
} else {
code = discoverYoutube([]string{"--video-id", "abcdefghijk", "--proxy", "socks5://127.0.0.1:11080"}, &out, &diagnostics, runner)
}
if code != 1 || out.Len() != 0 || strings.Contains(diagnostics.String(), secret) || !strings.Contains(diagnostics.String(), "player request failed") {
t.Fatalf("%s: code=%d diagnostics=%q", command, code, diagnostics.String())
}
}
}
func TestManagedMonitorPlayerCancellationExit(t *testing.T) {
for _, active := range []bool{false, true} {
for _, failure := range []string{"", "body", "diagnostics", "mixed", "guard"} {
t.Run(strings.Join([]string{map[bool]string{false: "baseline", true: "active"}[active], failure}, "/"), func(t *testing.T) {
d, env, _ := managedCLIFixture(t)
initManagedCLI(t, d)
env["ZAPRET_AUTOTUNE_DB"] = managedTargetDirectory + "/candidates.db"
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var extra error
if failure == "mixed" {
extra = errors.New("private I/O failure")
}
d.player = canceledPlayerProcess(t, cancel, extra, failure)
var output bytes.Buffer
code := managedMonitorCLIContext(ctx, nil, &output, &output, d, func(ctx context.Context, m managedMonitorDeps) error {
if active {
id := managedCandidateFixture(t, m.db, "frontend", "youtube.com", "--lua-desync=fake", true)
transaction := managedTransaction{Dir: d.directory, Name: "config", Reload: func() error { return nil }, Health: func() (string, error) { return "OK", nil }}
if err := transaction.Apply(m.db, []int64{id}); err != nil {
t.Fatal(err)
}
}
if failure == "guard" {
cancel()
delete(env, "ZAPRET_CONFIG")
}
return m.probe(ctx)
})
want := 0
if failure != "" {
want = 78
}
if code != want || strings.Contains(output.String(), "private I/O failure") {
t.Fatalf("exit=%d want=%d output=%q", code, want, output.String())
}
})
}
}
}
func TestManagedMonitorSIGTERMChild(t *testing.T) {
mode := os.Getenv("AUTOTUNE_MONITOR_SIGTERM_CHILD")
if mode == "" {
return
}
d, env, _ := managedCLIFixture(t)
initManagedCLI(t, d)
env["ZAPRET_AUTOTUNE_DB"] = managedTargetDirectory + "/candidates.db"
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM)
defer cancel()
d.player = func(ctx context.Context, _ []string, _, diagnostics io.Writer) error {
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestPlayerCancellationChild$")
cmd.Env = append(os.Environ(), "AUTOTUNE_PLAYER_CANCEL_CHILD=1")
cmd.Stdout, cmd.Stderr = os.Stdout, diagnostics
return cmd.Run()
}
code := managedMonitorCLIContext(ctx, nil, os.Stdout, os.Stderr, d, func(ctx context.Context, m managedMonitorDeps) error {
if mode == "active" {
id := managedCandidateFixture(t, m.db, "frontend", "youtube.com", "--lua-desync=fake", true)
transaction := managedTransaction{Dir: d.directory, Name: "config", Reload: func() error { return nil }, Health: func() (string, error) { return "OK", nil }}
if err := transaction.Apply(m.db, []int64{id}); err != nil {
t.Fatal(err)
}
}
return m.probe(ctx)
})
if code != 0 {
t.Fatalf("SIGTERM exit=%d", code)
}
}
type playerSignalWriter struct{ command *exec.Cmd }
func (w playerSignalWriter) Write(p []byte) (int, error) {
if strings.Contains(string(p), "ready") {
if err := w.command.Process.Signal(syscall.SIGTERM); err != nil {
return 0, err
}
}
return len(p), nil
}
func TestManagedMonitorExternalSIGTERM(t *testing.T) {
for _, mode := range []string{"baseline", "active"} {
t.Run(mode, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestManagedMonitorSIGTERMChild$")
cmd.Env = append(os.Environ(), "AUTOTUNE_MONITOR_SIGTERM_CHILD="+mode)
var diagnostics bytes.Buffer
cmd.Stderr = &diagnostics
// The writer observes readiness after Start has assigned Process.
cmd.Stdout = playerSignalWriter{command: cmd}
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
if err := cmd.Wait(); err != nil {
t.Fatalf("SIGTERM failed: %v %s", err, diagnostics.String())
}
})
}
}