Skip to content

Commit 7c86376

Browse files
committed
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. 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 <[email protected]>
1 parent 357b642 commit 7c86376

5 files changed

Lines changed: 326 additions & 6 deletions

File tree

‎cmd/backport-batch/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Requires `git` and an authenticated `gh` on PATH, and a remote called `origin`.
2727

2828
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.
2929

30-
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.
30+
Output is written before a push or a publish. Once a write to stdout or stderr fails, a closed pipe included, every push and every GitHub write is refused, and the command 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.
3131

3232
### run
3333

‎cmd/backport-batch/assemble.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,9 @@ func (a *app) publish(b *batch, p publication, rebuilt bool) error {
593593
}
594594
}
595595
if pushed != p.head {
596+
if err := a.mayAct(); err != nil {
597+
return err
598+
}
596599
if _, err := git(pushArgs(p.branch, p.head, lease)...); err != nil {
597600
return err
598601
}
@@ -752,6 +755,9 @@ func (a *app) deleteBranch(l line, c closing) error {
752755
case h != c.Head:
753756
return movedBranch(c, h)
754757
}
758+
if err := a.mayAct(); err != nil {
759+
return err
760+
}
755761
if _, err := git(deleteArgs(c.Branch, c.Head)...); err != nil {
756762
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())
757763
}

‎cmd/backport-batch/github.go‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,47 @@ type github interface {
135135
batchBot() string
136136
}
137137

138+
// guardedGitHub refuses every GitHub write once the run's output was lost.
139+
type guardedGitHub struct {
140+
github
141+
a *app
142+
}
143+
144+
func (g guardedGitHub) createPull(base, head, title, bodyFile string) (int, string, error) {
145+
if err := g.a.mayAct(); err != nil {
146+
return 0, "", err
147+
}
148+
return g.github.createPull(base, head, title, bodyFile)
149+
}
150+
151+
func (g guardedGitHub) editPull(number int, title, bodyFile string) error {
152+
if err := g.a.mayAct(); err != nil {
153+
return err
154+
}
155+
return g.github.editPull(number, title, bodyFile)
156+
}
157+
158+
func (g guardedGitHub) closePull(number int) error {
159+
if err := g.a.mayAct(); err != nil {
160+
return err
161+
}
162+
return g.github.closePull(number)
163+
}
164+
165+
func (g guardedGitHub) createComment(number int, bodyFile string) error {
166+
if err := g.a.mayAct(); err != nil {
167+
return err
168+
}
169+
return g.github.createComment(number, bodyFile)
170+
}
171+
172+
func (g guardedGitHub) editComment(id int64, bodyFile string) error {
173+
if err := g.a.mayAct(); err != nil {
174+
return err
175+
}
176+
return g.github.editComment(id, bodyFile)
177+
}
178+
138179
type ghCLI struct{ bot string }
139180

140181
func (g ghCLI) batchBot() string { return g.bot }

‎cmd/backport-batch/main.go‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import (
2424
"fmt"
2525
"io"
2626
"os"
27+
"os/signal"
2728
"strings"
29+
"syscall"
2830
"time"
2931
)
3032

@@ -111,21 +113,31 @@ type app struct {
111113
policyRef string
112114
// tool is the commit backport-batch was built from, for the record.
113115
tool string
114-
// unwritten is the first error writing to stdout, which fails the run:
115-
// what it printed never reached its reader.
116+
// unwritten is the first error writing to stdout or stderr. Output goes
117+
// out before a push or a publish, so a run that lost it acts on nothing,
118+
// and one that lost a confirmation afterwards fails.
116119
unwritten error
117120
}
118121

119122
func main() {
123+
catchSIGPIPE()
120124
a := &app{gh: ghCLI{bot: batchBotLogin(os.LookupEnv)}, stdout: os.Stdout, stderr: os.Stderr, now: time.Now, tool: toolVersion()}
121125
os.Exit(a.main(os.Args[1:]))
122126
}
123127

128+
// catchSIGPIPE makes a write to a closed stdout or stderr fail with EPIPE like
129+
// any other write, so the run stops before it pushes or publishes and says
130+
// why, instead of dying silently. signal.Ignore would do the same here but
131+
// would also leave SIGPIPE ignored in the git and gh children, across exec.
132+
func catchSIGPIPE() {
133+
signal.Notify(make(chan os.Signal, 1), syscall.SIGPIPE)
134+
}
135+
124136
func (a *app) main(args []string) int {
125-
a.stdout, a.stderr = textWriter{w: a.stdout, failed: &a.unwritten}, textWriter{w: a.stderr}
137+
a.stdout, a.stderr = textWriter{w: a.stdout, failed: &a.unwritten}, textWriter{w: a.stderr, failed: &a.unwritten}
126138
code := a.command(args)
127139
if a.unwritten != nil {
128-
_, _ = 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)
140+
_, _ = fmt.Fprintf(a.stderr, "backport-batch: output was lost: %v\n", a.unwritten)
129141
if code == 0 {
130142
code = 2
131143
}
@@ -134,7 +146,7 @@ func (a *app) main(args []string) int {
134146
}
135147

136148
func (a *app) command(args []string) int {
137-
a.gh = &permCache{github: a.gh, perms: map[string]string{}}
149+
a.gh = guardedGitHub{github: &permCache{github: a.gh, perms: map[string]string{}}, a: a}
138150
if len(args) == 0 {
139151
_, _ = fmt.Fprint(a.stderr, usageText)
140152
return 2
@@ -208,6 +220,16 @@ func parse(fs *flag.FlagSet, args []string) error {
208220
return nil
209221
}
210222

223+
// mayAct refuses an external write once output was lost: the run would act
224+
// on what it printed while nobody read it. Every push goes through it, and
225+
// every GitHub write through guardedGitHub.
226+
func (a *app) mayAct() error {
227+
if a.unwritten != nil {
228+
return fmt.Errorf("output was lost: %w; nothing more is pushed or written to GitHub", a.unwritten)
229+
}
230+
return nil
231+
}
232+
211233
func (a *app) fail(err error) int {
212234
_, _ = fmt.Fprintf(a.stderr, "backport-batch: %v\n", err)
213235
return 2

0 commit comments

Comments
 (0)