From 28a2c047e97a07ad4b5c899338d4c6f5a80fb8f0 Mon Sep 17 00:00:00 2001
From: INODE64 <ffelix@inode64.com>
Date: Mon, 30 Nov 2026 15:39:59 +0100
Subject: [PATCH] fix: propagate ping delivery failures to the process exit
 status

---
 README.md               |  6 +++
 cmd/runitor/main.go     | 13 ++++--
 cmd/runitor/run_test.go | 89 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 105 insertions(+), 3 deletions(-)
 create mode 100644 cmd/runitor/run_test.go

diff --git a/README.md b/README.md
index e2bdcd7..6f4cef4 100644
--- a/README.md
+++ b/README.md
@@ -111,6 +111,12 @@ command right away and reset the interval.
 
 ### Preventing Overlapping Runs
 
+Runitor returns the command's exit status. If the command succeeds but a start
+or result ping cannot be delivered after retries, runitor returns 1 and logs the
+delivery error. This allows cron to report monitoring failures even when it is
+configured to send mail only for unsuccessful jobs. A failed start ping does
+not prevent the command from running or its result from being reported.
+
 Pass `-pidfile PATH` to prevent concurrent execution. At startup runitor
 takes an exclusive OS-level lock on the file, writes its PID into it, and
 holds both until it exits. If another live runitor already holds the file,
diff --git a/cmd/runitor/main.go b/cmd/runitor/main.go
index 6b10801..b26f5cf 100644
--- a/cmd/runitor/main.go
+++ b/cmd/runitor/main.go
@@ -347,11 +347,13 @@ func main() {
 // Run function runs the cmd line, tees its output to terminal & ping body as
 // configured in cfg and pings the monitoring API to signal start, and then
 // success or failure of execution. Returns the exit code from the ran command
-// unless execution has failed, in such case 1 is returned.
+// unless execution or ping delivery has failed, in which case 1 is returned.
+// A nonzero command exit code takes precedence over a ping delivery failure.
 func Run(cmd []string, cfg RunConfig, handle string, p Pinger) int {
 	var (
-		params PingParams
-		err    error
+		params     PingParams
+		err        error
+		pingFailed bool
 	)
 
 	if !cfg.NoRunId {
@@ -366,6 +368,7 @@ func Run(cmd []string, cfg RunConfig, handle string, p Pinger) int {
 		icfg, err := p.PingStart(handle, params)
 		if err != nil {
 			log.Print("Ping(start): ", err)
+			pingFailed = true
 		} else if instanceLimit, ok := icfg.PingBodyLimit.Get(); ok {
 			if cfg.PingBodyLimitIsExplicit {
 				// Command line flag `-ping-body-limit` was used and
@@ -474,6 +477,10 @@ func Run(cmd []string, cfg RunConfig, handle string, p Pinger) int {
 
 	if err != nil {
 		log.Printf("Ping(%s): %v\n", ping.String(), err)
+		pingFailed = true
+	}
+	if pingFailed && exitCode == 0 {
+		return 1
 	}
 
 	return exitCode
diff --git a/cmd/runitor/run_test.go b/cmd/runitor/run_test.go
new file mode 100644
index 0000000..eb07dbe
--- /dev/null
+++ b/cmd/runitor/run_test.go
@@ -0,0 +1,89 @@
+// Copyright (c) Berk D. Demir and the runitor contributors.
+// SPDX-License-Identifier: 0BSD
+package main
+
+import (
+	"errors"
+	"io"
+	"os"
+	"strconv"
+	"testing"
+
+	. "bdd.fi/x/runitor/internal"
+)
+
+type recordingPinger struct {
+	startErr, resultErr error
+	starts, results     int
+	code                int
+}
+
+func (p *recordingPinger) PingStart(string, PingParams) (*InstanceConfig, error) {
+	p.starts++
+	return &InstanceConfig{}, p.startErr
+}
+
+func (p *recordingPinger) PingSuccess(string, PingParams, io.ReadSeeker) (*InstanceConfig, error) {
+	p.results++
+	return &InstanceConfig{}, p.resultErr
+}
+
+func (p *recordingPinger) PingFail(h string, pp PingParams, b io.ReadSeeker) (*InstanceConfig, error) {
+	return p.PingSuccess(h, pp, b)
+}
+
+func (p *recordingPinger) PingLog(h string, pp PingParams, b io.ReadSeeker) (*InstanceConfig, error) {
+	return p.PingSuccess(h, pp, b)
+}
+
+func (p *recordingPinger) PingExitCode(h string, pp PingParams, code int, b io.ReadSeeker) (*InstanceConfig, error) {
+	p.code = code
+	return p.PingSuccess(h, pp, b)
+}
+
+func TestRunCommand(t *testing.T) {
+	if os.Getenv("RUNITOR_TEST_COMMAND") != "1" {
+		return
+	}
+	code, err := strconv.Atoi(os.Args[len(os.Args)-1])
+	if err != nil {
+		os.Exit(99)
+	}
+	os.Exit(code)
+}
+
+func TestRunPingFailures(t *testing.T) {
+	t.Setenv("RUNITOR_TEST_COMMAND", "1")
+	errPing := errors.New("delivery failed")
+	for _, tc := range []struct {
+		name                string
+		commandCode         int
+		startErr, resultErr error
+		noStart             bool
+		want                int
+	}{
+		{"success", 0, nil, nil, false, 0},
+		{"start failure", 0, errPing, nil, false, 1},
+		{"result failure", 0, nil, errPing, false, 1},
+		{"both fail", 0, errPing, errPing, false, 1},
+		{"command failure", 7, nil, nil, false, 7},
+		{"command failure takes precedence", 7, errPing, errPing, false, 7},
+		{"no start requested", 0, errPing, nil, true, 0},
+	} {
+		t.Run(tc.name, func(t *testing.T) {
+			p := &recordingPinger{startErr: tc.startErr, resultErr: tc.resultErr}
+			cfg := RunConfig{NoRunId: true, Quiet: true, Silent: true, NoStartPing: tc.noStart,
+				OnSuccess: PingTypeSuccess, OnNonzeroExit: PingTypeExitCode}
+			got := Run([]string{os.Args[0], "-test.run=^TestRunCommand$", "--", strconv.Itoa(tc.commandCode)}, cfg, "fixture", p)
+			if got != tc.want {
+				t.Fatalf("Run()=%d, want %d", got, tc.want)
+			}
+			if p.results != 1 || p.starts != map[bool]int{false: 1, true: 0}[tc.noStart] {
+				t.Fatalf("unexpected ping counts: start=%d, result=%d", p.starts, p.results)
+			}
+			if tc.commandCode != 0 && p.code != tc.commandCode {
+				t.Fatalf("reported command status %d, want %d", p.code, tc.commandCode)
+			}
+		})
+	}
+}
-- 
2.56.1

