fix(backport): push and publish nothing once output is lost

backport-batch writes its body, summary, findings and warnings before
it pushes, opens or edits a pull request, closes a batch or posts a
comment. A run that kept going after that output was lost acted on
what nobody read, and a closed pipe was worse: with Go's default
SIGPIPE handling the process died silently mid-run. Lines that confirm
an action after it happened, such as the URL of an opened pull request,
go to stderr, so a lost stderr matters as much as a lost stdout.

A failed write to either stream is kept, and from then on every push and
every GitHub write is refused at the point it would be made: the GitHub
client is wrapped so that each write checks first, and each push checks
before it runs. Once output is lost, the run changes nothing more: no
push, no GitHub write, no local branch move. No new unit starts after
output is lost; a unit already in flight finishes its rollback. A reset
whose own hint, the only way back to the commits it drops, cannot be
written does not run either. The run exits 2 saying the output was lost;
a confirmation lost after an action fails the run too. Such a
confirmation is lost only when stderr itself fails, so a lost
confirmation can only be reported by the exit code. A test holds every
method of the GitHub interface to being a known read or a guarded write.

The tool catches SIGPIPE with signal.Notify, so a closed pipe is a
write error like any other. It does not use signal.Ignore, whose ignored
disposition the git and gh children would inherit across exec; a test
checks that a child still dies of SIGPIPE.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
This commit is contained in:
Aleksei Sviridkin committed 2026-11-13 12:35:09 +03:00
1 parent 6f2d6ed17b
commit a3305e8dd3
7 files changed
+439 -9

No files matched your search

+1 -1
View File
@@ -27,7 +27,7 @@ Requires `git` and an authenticated `gh` on PATH, and a remote called `origin`.
Build the binary wherever an exit code matters: `go run` turns any non-zero exit into its own `1`, so under it a check that could not be completed looks the same as one that found something missing.
A command whose output on stdout could not be written exits 2 where it would have exited 0, and says on stderr that the output was lost; anything the run did before that, a push or a published pull request included, has still happened.
Output is written before a push or a publish. Once a write to stdout or stderr fails, a closed pipe included, the run changes nothing more: no push, no GitHub write, no local branch move, and no new pick; a pick already in flight still finishes its rollback. It exits 2 where it would have exited 0, saying on stderr, while it still can, that the output was lost. A confirmation lost after a push or a publish fails the run the same way.
### run
+15 -2
View File
@@ -273,7 +273,7 @@ func (a *app) run(args []string) int {
continue
}
}
p, err := pickOriginal(v, func(base, head string) ([]string, error) {
p, err := a.pickUnit(v, func(base, head string) ([]string, error) {
omitted, diag, err := b.omittedEffects(v, base, head)
v.addDiag(diag...)
return omitted, err
@@ -529,6 +529,9 @@ func cleanTree() error {
// the move drops commits of and how to get them back: a clean tree says
// nothing about commits only a local branch has.
func (a *app) warnReset(branch, start string) error {
if err := a.mayAct(); err != nil {
return err
}
old, err := lookup("refs/heads/" + branch)
if err != nil || old == "" || old == start {
return err
@@ -537,7 +540,11 @@ func (a *app) warnReset(branch, start string) error {
if err != nil || kept {
return err
}
_, _ = fmt.Fprintf(a.stderr, "resetting the local %s from %s, dropping commits %.12s does not carry; git branch %s-%.12s %s restores them\n", branch, old, start, branch, old, old)
// The hint is the only way back to the dropped commits, so the reset does
// not run when it could not be written.
if _, err := fmt.Fprintf(a.stderr, "resetting the local %s from %s, dropping commits %.12s does not carry; git branch %s-%.12s %s restores them\n", branch, old, start, branch, old, old); err != nil {
return fmt.Errorf("not resetting the local %s: %w", branch, err)
}
return nil
}
@@ -719,6 +726,9 @@ func (a *app) publish(b *batch, p publication, rebuilt bool) error {
}
}
if pushed != p.head {
if err := a.mayAct(); err != nil {
return err
}
if _, err := git(pushArgs(p.branch, p.head, lease)...); err != nil {
return err
}
@@ -878,6 +888,9 @@ func (a *app) deleteBranch(l line, c closing) error {
case h != c.Head:
return movedBranch(c, h)
}
if err := a.mayAct(); err != nil {
return err
}
if _, err := git(deleteArgs(c.Branch, c.Head)...); err != nil {
return fmt.Errorf("deleting %s, left by the close of #%d: %w; backport-batch run --line %s --push deletes it", c.Branch, c.PR, err, l.branch())
}
+1 -1
View File
@@ -102,7 +102,7 @@ func (a *app) reassemble(b *batch, out, hold map[int]string, drops []droppedEntr
return a.fail(err)
}
for _, u := range replay {
failed, files, err := replayUnit(u)
failed, files, err := a.replayNext(u)
switch {
case err != nil:
return a.fail(err)
+48
View File
@@ -137,6 +137,54 @@ type github interface {
batchBot() string
}
// guardedGitHub refuses every GitHub write once the run's output was lost.
type guardedGitHub struct {
github
a *app
}
func (g guardedGitHub) createPull(base, head, title, bodyFile string) (int, string, error) {
if err := g.a.mayAct(); err != nil {
return 0, "", err
}
return g.github.createPull(base, head, title, bodyFile)
}
func (g guardedGitHub) editPull(number int, title, bodyFile string) error {
if err := g.a.mayAct(); err != nil {
return err
}
return g.github.editPull(number, title, bodyFile)
}
func (g guardedGitHub) editBody(number int, bodyFile string) error {
if err := g.a.mayAct(); err != nil {
return err
}
return g.github.editBody(number, bodyFile)
}
func (g guardedGitHub) closePull(number int) error {
if err := g.a.mayAct(); err != nil {
return err
}
return g.github.closePull(number)
}
func (g guardedGitHub) createComment(number int, bodyFile string) error {
if err := g.a.mayAct(); err != nil {
return err
}
return g.github.createComment(number, bodyFile)
}
func (g guardedGitHub) editComment(id int64, bodyFile string) error {
if err := g.a.mayAct(); err != nil {
return err
}
return g.github.editComment(id, bodyFile)
}
type ghCLI struct{ bot string }
func (g ghCLI) batchBot() string { return g.bot }
+28 -5
View File
@@ -24,7 +24,9 @@ import (
"fmt"
"io"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
@@ -111,21 +113,31 @@ type app struct {
policyRef string
// tool is the commit backport-batch was built from, for the record.
tool string
// unwritten is the first error writing to stdout, which fails the run:
// what it printed never reached its reader.
// unwritten is the first error writing to stdout or stderr. Output goes
// out before a push or a publish, so a run that lost it acts on nothing,
// and one that lost a confirmation afterwards fails.
unwritten error
}
func main() {
catchSIGPIPE()
a := &app{gh: ghCLI{bot: batchBotLogin(os.LookupEnv)}, stdout: os.Stdout, stderr: os.Stderr, now: time.Now, tool: toolVersion()}
os.Exit(a.main(os.Args[1:]))
}
// catchSIGPIPE makes a write to a closed stdout or stderr fail with EPIPE like
// any other write, so the run stops before it pushes or publishes and says
// why, instead of dying silently. signal.Ignore would do the same here but
// would also leave SIGPIPE ignored in the git and gh children, across exec.
func catchSIGPIPE() {
signal.Notify(make(chan os.Signal, 1), syscall.SIGPIPE)
}
func (a *app) main(args []string) int {
a.stdout, a.stderr = textWriter{w: a.stdout, failed: &a.unwritten}, textWriter{w: a.stderr}
a.stdout, a.stderr = textWriter{w: a.stdout, failed: &a.unwritten}, textWriter{w: a.stderr, failed: &a.unwritten}
code := a.command(args)
if a.unwritten != nil {
_, _ = fmt.Fprintf(a.stderr, "backport-batch: output was lost: %v; anything the run did, such as a push or a publish, still happened\n", a.unwritten)
_, _ = fmt.Fprintf(a.stderr, "backport-batch: output was lost: %v\n", a.unwritten)
if code == 0 {
code = 2
}
@@ -134,7 +146,7 @@ func (a *app) main(args []string) int {
}
func (a *app) command(args []string) int {
a.gh = &permCache{github: a.gh, perms: map[string]string{}}
a.gh = guardedGitHub{github: &permCache{github: a.gh, perms: map[string]string{}}, a: a}
if len(args) == 0 {
_, _ = fmt.Fprint(a.stderr, usageText)
return 2
@@ -208,6 +220,17 @@ func parse(fs *flag.FlagSet, args []string) error {
return nil
}
// mayAct refuses any further change once output was lost: the run would act
// on what it printed while nobody read it. Every push and every move of a
// local batch branch go through it, and every GitHub write through
// guardedGitHub.
func (a *app) mayAct() error {
if a.unwritten != nil {
return fmt.Errorf("output was lost: %w; nothing more is pushed or written to GitHub", a.unwritten)
}
return nil
}
func (a *app) fail(err error) int {
_, _ = fmt.Fprintf(a.stderr, "backport-batch: %v\n", err)
return 2
+329
View File
@@ -18,6 +18,10 @@ package main
import (
"errors"
"io"
"os"
"os/exec"
"reflect"
"strings"
"testing"
"time"
@@ -54,3 +58,328 @@ func TestUnwritableOutputKeepsTheCommandsFailure(t *testing.T) {
t.Errorf("stderr does not name the write failure:\n%s", stderr.String())
}
}
// A closed pipe on stdout does not kill the run before it can say what
// already happened: the write fails like any other, and the run exits 2.
func TestClosedStdoutPipeIsReported(t *testing.T) {
if os.Getenv("BACKPORT_BATCH_CLOSED_STDOUT_HELPER") == "1" {
os.Args = []string{"backport-batch", "help"}
main()
return
}
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
_ = r.Close()
t.Cleanup(func() { _ = w.Close() })
cmd := exec.Command(os.Args[0], "-test.run=^TestClosedStdoutPipeIsReported$")
cmd.Env = append(os.Environ(), "BACKPORT_BATCH_CLOSED_STDOUT_HELPER=1")
var stderr strings.Builder
cmd.Stdout, cmd.Stderr = w, &stderr
err = cmd.Run()
var exit *exec.ExitError
if !errors.As(err, &exit) || exit.ExitCode() != 2 {
t.Fatalf("help into a closed pipe = %v, want exit 2\nstderr:\n%s", err, stderr.String())
}
if !strings.Contains(stderr.String(), "output was lost") {
t.Errorf("stderr does not say the output was lost:\n%s", stderr.String())
}
}
// failOn fails every write that contains want, and takes every other one.
type failOn struct {
want string
buf strings.Builder
}
func (w *failOn) Write(p []byte) (int, error) {
if strings.Contains(string(p), w.want) {
return 0, errors.New("broken pipe")
}
return w.buf.Write(p)
}
func runWith(f *fixture, gh *fakeGH, stdout, stderr io.Writer, args ...string) int {
f.t.Helper()
if gh.remoteHead == nil {
gh.remoteHead = f.remoteHead
}
a := &app{gh: gh, stdout: stdout, stderr: stderr, now: func() time.Time { return f.now }, tool: f.tool}
return a.main(args)
}
// Output is written before the push; when it is lost, on either stream,
// nothing is pushed and no pull request is opened.
func TestLostOutputPushesNothing(t *testing.T) {
for _, stream := range []string{"stdout", "stderr"} {
t.Run(stream, func(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
gh := &fakeGH{}
var stdout, stderr io.Writer = &strings.Builder{}, &strings.Builder{}
if stream == "stdout" {
stdout = failingWriter{}
} else {
stderr = failingWriter{}
}
if code := runWith(f, gh, stdout, stderr, "run", "--line", "release-1.1", "--push"); code != 2 {
t.Fatalf("run --push with a failing %s = %d, want 2", stream, code)
}
if head := f.remoteHead(batchBranch1); head != "" {
t.Errorf("the batch was pushed at %.12s", head)
}
if len(gh.prs) != 0 {
t.Errorf("a pull request was opened: %+v", gh.prs)
}
})
}
}
// A confirmation lost after the push fails the run: the pull request was
// opened, and the run does not exit 0 without having said so.
func TestLostConfirmationFailsTheRun(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
gh := &fakeGH{}
if code := runWith(f, gh, &strings.Builder{}, &failOn{want: "opened draft"}, "run", "--line", "release-1.1", "--push"); code != 2 {
t.Fatalf("run --push losing its confirmation = %d, want 2", code)
}
if len(gh.prs) != 1 {
t.Errorf("%d pull requests, want the one the run opened", len(gh.prs))
}
if len(gh.issueComments) != 0 {
t.Errorf("the report was posted after the confirmation was lost: %+v", gh.issueComments)
}
}
// notify writes what it found before it comments; when that is lost, it
// does not comment.
func TestLostOutputCommentsNothing(t *testing.T) {
gh := &fakeGH{prs: []pullRequest{notifyPR("20261019", "")}}
var stderr strings.Builder
a := &app{gh: gh, stdout: failingWriter{}, stderr: &stderr, now: func() time.Time { return time.Date(2026, 10, 26, 6, 23, 0, 0, time.UTC) }}
if code := a.main([]string{"notify", "--line", "release-1.1", "--push"}); code != 2 {
t.Fatalf("notify --push with a failing stdout = %d, want 2\n%s", code, stderr.String())
}
if len(gh.issueComments) != 0 {
t.Errorf("notify commented: %+v", gh.issueComments)
}
}
// writesGitHub records the GitHub writes that reach it.
type writesGitHub struct {
github
writes []string
}
func (g *writesGitHub) createPull(string, string, string, string) (int, string, error) {
g.writes = append(g.writes, "createPull")
return 1, "", nil
}
func (g *writesGitHub) editPull(int, string, string) error {
g.writes = append(g.writes, "editPull")
return nil
}
func (g *writesGitHub) editBody(int, string) error {
g.writes = append(g.writes, "editBody")
return nil
}
func (g *writesGitHub) closePull(int) error {
g.writes = append(g.writes, "closePull")
return nil
}
func (g *writesGitHub) createComment(int, string) error {
g.writes = append(g.writes, "createComment")
return nil
}
func (g *writesGitHub) editComment(int64, string) error {
g.writes = append(g.writes, "editComment")
return nil
}
// Every GitHub write is refused once output was lost. Every method of
// github is either a read or in this table, so a write added later without
// a guard fails here.
func TestEveryGitHubWriteIsGuarded(t *testing.T) {
reads := map[string]bool{"openPulls": true, "recentPulls": true, "permission": true, "labelled": true, "pull": true, "comments": true, "batchBot": true}
inner := &writesGitHub{}
g := guardedGitHub{github: inner, a: &app{unwritten: errors.New("broken pipe")}}
writes := map[string]func() error{
"createPull": func() error { _, _, err := g.createPull("b", "h", "t", "f"); return err },
"editPull": func() error { return g.editPull(1, "t", "f") },
"editBody": func() error { return g.editBody(1, "f") },
"closePull": func() error { return g.closePull(1) },
"createComment": func() error { return g.createComment(1, "f") },
"editComment": func() error { return g.editComment(1, "f") },
}
iface := reflect.TypeFor[github]()
for i := range iface.NumMethod() {
name := iface.Method(i).Name
if _, ok := writes[name]; !ok && !reads[name] {
t.Errorf("github.%s is neither a known read nor a guarded write", name)
}
}
for name, write := range writes {
if err := write(); err == nil || !strings.Contains(err.Error(), "output was lost") {
t.Errorf("%s with output lost = %v, want a refusal", name, err)
}
}
if len(inner.writes) != 0 {
t.Errorf("writes reached GitHub after output was lost: %q", inner.writes)
}
}
// A warning lost while the open batch is being updated stops the update:
// nothing is pushed, posted or edited.
func TestLostWarningLeavesTheOpenBatchAlone(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
gh := &fakeGH{}
mustRun(t, f, gh, "run", "--line", "release-1.1", "--push")
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
bad := issueComment{ID: 8, Body: reportMarker + "names none\n"}
bad.User.Login, bad.User.Type = "maintainer", "User"
gh.issueComments = append(gh.issueComments, bad)
head, comments, body := f.remoteHead(batchBranch1), len(gh.issueComments), gh.find(90).Body
f.now = f.now.AddDate(0, 0, 1)
if code := runWith(f, gh, &strings.Builder{}, &failOn{want: "does not count as a report"}, "run", "--line", "release-1.1", "--continue", "--push"); code != 2 {
t.Fatalf("run --continue --push losing a warning = %d, want 2", code)
}
if got := f.remoteHead(batchBranch1); got != head {
t.Errorf("the batch moved to %.12s", got)
}
if len(gh.issueComments) != comments {
t.Errorf("a comment was posted: %d comments, want %d", len(gh.issueComments), comments)
}
if gh.find(90).Body != body {
t.Error("the batch pull request was edited")
}
}
// A warning lost while a batch is being closed leaves it open, with no
// closing record half written.
func TestLostWarningLeavesTheBatchUnclosed(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
gh := &fakeGH{}
mustRun(t, f, gh, "run", "--line", "release-1.1", "--push")
bad := issueComment{ID: 8, Body: reportMarker + "names none\n"}
bad.User.Login, bad.User.Type = "maintainer", "User"
gh.issueComments = append(gh.issueComments, bad)
body := gh.find(90).Body
if code := runWith(f, gh, &strings.Builder{}, &failOn{want: "does not count as a report"}, "run", "--line", "release-1.1", "--defer", "5", "--reason", "x", "--push"); code != 2 {
t.Fatalf("run --defer losing a warning = %d, want 2", code)
}
if gh.find(90).Body != body {
t.Error("the batch pull request was edited")
}
if !gh.isOpen(90) {
t.Error("the batch pull request was closed")
}
}
// The git and gh children keep the default SIGPIPE: catching it here must not
// leave it ignored in them, as signal.Ignore would through exec.
func TestChildrenKeepTheDefaultSIGPIPE(t *testing.T) {
catchSIGPIPE()
out, err := exec.Command("sh", "-c", "kill -PIPE $$; echo survived").Output()
if err == nil || strings.Contains(string(out), "survived") {
t.Fatalf("a child survived SIGPIPE: %q, %v", out, err)
}
}
// The branch a close left is not deleted once output was lost: a deletion
// is a push like any other.
func TestLostOutputDeletesNoBranch(t *testing.T) {
f, gh, h1 := closeWorld(t)
f.git("switch", "-q", "main")
l, err := parseLine("release-1.1")
if err != nil {
t.Fatal(err)
}
a := &app{gh: gh, unwritten: errors.New("broken pipe")}
if err := a.deleteBranch(l, closing{PR: 90, Branch: batchBranch1, Head: h1}); err == nil || !strings.Contains(err.Error(), "output was lost") {
t.Fatalf("deleteBranch with output lost = %v, want a refusal", err)
}
if got := f.remoteHead(batchBranch1); got != h1 {
t.Errorf("the branch is at %q, want it left at %.12s", got, h1)
}
}
// A park line lost on stdout stops notify before it writes: no comment, and
// the record is not marked as announced.
func TestLostParkLineWritesNothing(t *testing.T) {
park := parkedEntry{Original: 5, Merge: strings.Repeat("c", 40), Kind: parkDependency, Needs: []int{3}}
gh := &fakeGH{prs: []pullRequest{notifyPR("20261026", parkedBody(park))}}
body := gh.prs[0].Body
var stderr strings.Builder
a := &app{gh: gh, stdout: &failOn{want: "parked originals"}, stderr: &stderr, now: func() time.Time { return time.Date(2026, 10, 26, 6, 23, 0, 0, time.UTC) }}
if code := a.main([]string{"notify", "--line", "release-1.1", "--push"}); code != 2 {
t.Fatalf("notify --push losing the park line = %d, want 2\n%s", code, stderr.String())
}
if len(gh.issueComments) != 0 {
t.Errorf("notify commented: %+v", gh.issueComments)
}
if gh.prs[0].Body != body {
t.Error("notify marked the park as announced")
}
}
// The hint naming the commits a reset drops is the only way back to them, so
// the local branch is not reset when the hint is lost.
func TestLostResetHintKeepsTheBranch(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.git("switch", "-q", "-C", batchBranch1, "origin/release-1.1")
f.write("app/mine.txt", "unpushed\n")
f.git("add", "-A")
f.git("commit", "-q", "-m", "an unpushed resolution")
tip := f.git("rev-parse", "HEAD")
f.git("switch", "-q", "main")
if code := runWith(f, &fakeGH{}, &strings.Builder{}, &failOn{want: "resetting the local"}, "run", "--line", "release-1.1"); code != 2 {
t.Fatalf("run losing the reset hint = %d, want 2", code)
}
if got := f.git("rev-parse", batchBranch1); got != tip {
t.Errorf("the local %s moved to %.12s, want it kept at %.12s", batchBranch1, got, tip)
}
}
// Once output is lost the run moves no local branch, even one a reset would
// only fast-forward.
func TestLostOutputMovesNoLocalBranch(t *testing.T) {
f := newFixture(t)
f.lines()
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
f.git("branch", "-f", batchBranch1, "origin/release-1.1^")
before := f.git("rev-parse", batchBranch1)
if code := runWith(f, &fakeGH{}, &strings.Builder{}, &failOn{want: "prepared"}, "run", "--line", "release-1.1"); code != 2 {
t.Fatalf("run losing an early line = %d, want 2", code)
}
if got := f.git("rev-parse", batchBranch1); got != before {
t.Errorf("the local %s moved to %.12s, want it kept at %.12s", batchBranch1, got, before)
}
}
// No new unit starts once output is lost: neither a pick nor a replay touches
// the branch.
func TestLostOutputStartsNoUnit(t *testing.T) {
f := newFixture(t)
f.lines()
before := f.git("rev-parse", "HEAD")
a := &app{unwritten: errors.New("broken pipe")}
if _, err := a.pickUnit(&verdict{}, nil); err == nil || !strings.Contains(err.Error(), "output was lost") {
t.Errorf("pickUnit with output lost = %v, want a refusal", err)
}
if _, _, err := a.replayNext(unit{}); err == nil || !strings.Contains(err.Error(), "output was lost") {
t.Errorf("replayNext with output lost = %v, want a refusal", err)
}
if got := f.git("rev-parse", "HEAD"); got != before {
t.Errorf("HEAD moved to %.12s", got)
}
}
+17
View File
@@ -114,6 +114,23 @@ func mergeMessage(message, merge string) string {
return strings.TrimRight(message, " \t\r\n") + "\n\n" + batchTrailer + ": " + merge + "\n"
}
// pickUnit and replayNext start the next unit only while the run's output is
// intact: once it was lost no new unit starts, and a unit already in flight
// still finishes its rollback.
func (a *app) pickUnit(v *verdict, validate func(base, head string) ([]string, error)) (pick, error) {
if err := a.mayAct(); err != nil {
return pick{}, err
}
return pickOriginal(v, validate)
}
func (a *app) replayNext(u unit) (string, []string, error) {
if err := a.mayAct(); err != nil {
return "", nil, err
}
return replayUnit(u)
}
// commitMerge makes the merge of tip onto base, whose tree is tip's since tip
// descends from base, and moves the current branch from tip to it.
func commitMerge(base, tip, message string) (string, error) {