From 10404ba5657d2858c84f0f619f0cff13e8ff0962 Mon Sep 17 00:00:00 2001 From: Geno Roupsky <185580+groupsky@users.noreply.github.com> Date: Wed, 18 Nov 2026 22:44:24 +0200 Subject: [PATCH 1/2] test(contract)!: stop the trial broker with SIGTERM alone The harness closed the stdin of nodkey try and then sent SIGTERM. At the end of its input, nodkey try stops with exit status 1, by design, so the two raced. Now the harness sends only SIGTERM, reads all that nodkey try wrote, and then waits. When the stop fails, it prints what nodkey try wrote. Fixes #214 Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 2 ++ docs/updating.md | 22 ++++++++++++++++++++++ test/contract/harness_linux_test.go | 28 ++++++++++++++++++++++------ 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3ae23a1..ee87e0c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -57,6 +57,8 @@ jobs: fi - run: go vet ./... - run: go test -race ./... + - name: TEMPORARY (#214) the contract tests in 20 separate runs + run: for i in $(seq 20); do echo "run $i"; go test -race -count=1 ./test/contract/ || exit 1; done lint: runs-on: ubuntu-latest diff --git a/docs/updating.md b/docs/updating.md index eda510f..024011a 100644 --- a/docs/updating.md +++ b/docs/updating.md @@ -15,6 +15,28 @@ From 2026-11-17 on, the release notes list such a change under ([docs/releasing.md](releasing.md#changes-in-behavior-for-callers)). Older releases listed it as a normal feature. +## Updating from 0.7.0 or older + +**What changed (#214).** Nothing that a caller sees. The harness of the +contract tests in `test/contract/` (#209, new after 0.7.0) now stops +the trial broker (`nodkey try`) with SIGTERM alone. Before, it closed the +stdin of `nodkey try` first, and sometimes `nodkey try` saw the end of +its input before the signal and ended with exit status 1. No release had +the old harness. A change to the harness +counts as a change in behavior for callers +([test/contract/README.md](../test/contract/README.md)), so it has this +section. `nodkey run` and `nodkey try` did not change. + +**Who is affected.** No caller of `nodkey run`. A person who runs the +contract tests gets the same tests, without the random failure at the +end. + +**What to do.** Nothing. To run the contract tests: + +```sh +go test -race -count=1 ./test/contract/ +``` + ## Updating from 0.5.0 or older **What changed (0.6.0, #171).** `nodkey run` masks the values that it diff --git a/test/contract/harness_linux_test.go b/test/contract/harness_linux_test.go index 6766f0a..cd3fe13 100644 --- a/test/contract/harness_linux_test.go +++ b/test/contract/harness_linux_test.go @@ -91,7 +91,7 @@ func runTests(m *testing.M) int { } code := m.Run() if err := tr.stop(); err != nil { - _, _ = fmt.Fprintln(stderr, "contract: the trial broker did not stop cleanly:", err) + _, _ = fmt.Fprintf(stderr, "contract: the trial broker did not stop cleanly: %v; nodkey try wrote:\n%s", err, tr.output()) code = 1 } _ = os.RemoveAll(tr.dir) @@ -191,6 +191,8 @@ func startTrial(dir string) (*trial, error) { // Not with the context of a test: the trial lives for all of them. tr.cmd = exec.CommandContext(context.Background(), setup.nodkey, "try") //nolint:gosec // G204: the nodkey binary of the test. tr.cmd.Env = append(os.Environ(), "XDG_RUNTIME_DIR="+run) + // Wait ends at most this long after nodkey try, even if a pipe stays open. + tr.cmd.WaitDelay = waitLimit in, err := tr.cmd.StdinPipe() if err != nil { return nil, err @@ -264,11 +266,25 @@ func (tr *trial) output() string { return tr.out.String() } -// stop ends nodkey try with SIGTERM: it removes its files. +// stop ends nodkey try with SIGTERM: it removes its files, and ends +// with exit status 0. It does not close the stdin of nodkey try first: +// at the end of its input, nodkey try stops with exit status 1, because +// no answer can come any more (TestTryStopsWhenTheInputEnds in +// cmd/nodkey). A closed stdin before the signal was a race between the +// two, and nodkey try sometimes saw the end of its input first (#214). +// cmd.Wait closes the stdin after nodkey try has ended. func (tr *trial) stop() error { - // No more answers: a request that comes now expires. - _ = tr.in.Close() - _ = tr.cmd.Process.Signal(syscall.SIGTERM) + if err := tr.cmd.Process.Signal(syscall.SIGTERM); err != nil && !errors.Is(err, os.ErrProcessDone) { + return fmt.Errorf("cannot send SIGTERM to nodkey try: %w", err) + } + // Read all that nodkey try wrote before Wait closes its stdout. + select { + case <-tr.done: + case <-time.After(waitLimit): + _ = tr.cmd.Process.Kill() + _ = tr.cmd.Wait() + return fmt.Errorf("the stdout of nodkey try did not close within %s after SIGTERM", waitLimit) + } waited := make(chan error, 1) go func() { waited <- tr.cmd.Wait() }() select { @@ -276,7 +292,7 @@ func (tr *trial) stop() error { return err case <-time.After(waitLimit): _ = tr.cmd.Process.Kill() - return errors.New("nodkey try did not end after SIGTERM") + return fmt.Errorf("nodkey try closed its stdout, but did not exit within %s after SIGTERM", waitLimit) } } From c34bed165f76020ec53d4b4d2471242a9a2e3c9c Mon Sep 17 00:00:00 2001 From: Geno Roupsky <185580+groupsky@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:13:19 +0200 Subject: [PATCH 2/2] ci: remove the temporary repeated runs Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ee87e0c..3ae23a1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -57,8 +57,6 @@ jobs: fi - run: go vet ./... - run: go test -race ./... - - name: TEMPORARY (#214) the contract tests in 20 separate runs - run: for i in $(seq 20); do echo "run $i"; go test -race -count=1 ./test/contract/ || exit 1; done lint: runs-on: ubuntu-latest