Skip to content

Return NotFound when watching a missing Task - #462

Closed
erain wants to merge 1 commit into
mainfrom
fix/watch-missing-task-435
Closed

erain wants to merge 1 commit into
mainfrom
fix/watch-missing-task-435

Conversation

@erain

@erain erain commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

ax watch task missing now exits promptly with NotFound. Initial store lookup failures return Internal. The subscription still registers before the initial lookup to avoid losing updates, and its deferred close covers every subsequent exit.

Fixes #435.

This picks up vincent0426's offered patch, with coverage for the current main, and credits Yi-111-a's diagnosis and reproduction.

Validation on Go 1.27.1, Linux/amd64 (all final checks exited 0):

  • Direct and real TCP/gRPC tests cover error codes, initial/modified delivery, terminal EOF, cancellation, subscription cleanup, and a deterministic update during the initial lookup.
  • WatchTask has 100.0% statement coverage in the focused suite; 50 focused race repetitions passed. Missing-task and lookup-before-registration negative controls fail as expected.
  • Full tests, uncached tests, race tests, build, vet, formatting, and module/whitespace checks.
  • Stock CLI/API plus isolated Redis: the baseline times out (124); the fix returns NotFound (1) in 12 ms. Existing watches reach Completed, and all subscriber counts return to zero. No sandbox provisioning was needed.

Prepared with an AI coding assistant.

Captured CLI/API output, detailed checks, and copyable Redis reproduction

Validation on Go 1.27.1, base ac2332829f22360ff97b0ba34d94dd0dd782f17e:

  • Standard-library table tests cover missing and wrapped missing errors, backend lookup and registration failures, initial and modified send failures, cancellation, closed channels, both terminal phases, nil requests, default scope, and exact subscription close counts.
  • Real TCP/gRPC tests cover missing and cross-atespace tasks, error codes, initial/modified delivery, terminal EOF, cancellation, and subscription cleanup. A real memory-store update is published between taking and returning the initial snapshot, with no sleeps or probabilistic race assumptions.
  • Focused TestWatchTask coverage measures 100.0% statement coverage of WatchTask, via go test -count=1 -run '^TestWatchTask' -coverprofile=watch.coverage ./internal/server and go tool cover -func=watch.coverage. This is a function measurement, not a package-wide claim.
  • The missing-task regression fails on unmodified main: DeadlineExceeded rather than NotFound, exit 1. Fixed tests exit 0.
  • A negative-control Go overlay that moves lookup before registration fails the update test with DeadlineExceeded, exit 1, proving the test detects the lost-update gap.
go test -count=1 -v -run '^TestWatchTask' ./internal/server
go test -count=1 -v ./...
go test -race -count=1 ./...
go test -race -count=50 -run '^TestWatchTask' ./internal/server
make test
go vet ./...
make build
go mod tidy
git diff --exit-code -- go.mod go.sum
git diff --check
gofmt -l internal/server/server.go internal/server/watch_test.go

All commands above exited 0; no prerequisite skips or dependency changes. The formatter reports no files. The full contribution diff was reread.

The CLI/API proof used stock baseline and fixed ax-server binaries, the actual ax CLI, and a disposable Redis 7.4.11 container. The baseline binary was built with a Go overlay restoring only internal/server/server.go from main. No live Substrate sandbox or compatibility overlay was used: these watches exercise the AX API/storage boundary, with existing-task records seeded directly in isolated Redis.

Captured baseline:

WatchTask missing: code=DeadlineExceeded error=rpc error: code = DeadlineExceeded desc = context deadline exceeded
$ timeout --signal=TERM 2s ./bin/ax --server http://127.0.0.1:<port> watch task missing
Watching task default/missing...
exit_status=124

Captured fixed behavior:

Watching task default/missing...
Error: receiving stream update: rpc error: code = NotFound desc = task "missing" not found in atespace "default"
exit_status=1
missing CLI elapsed_seconds=0.012
WatchTask missing: code=NotFound
WatchTask api-positive: action=INITIAL phase=Running
Redis published Completed: subscribers=1
WatchTask api-positive: action=MODIFIED phase=Completed
WatchTask terminal: EOF=true
exit_status=0
Watching task default/cli-positive...
[10:50:10] Phase: Running    Actor:                    WorkerIP:
[10:50:10] Phase: Completed  Actor:                    WorkerIP:
Task reached terminal phase "Completed".
exit_status=0
Redis PUBSUB NUMSUB missing = 0
Redis PUBSUB NUMSUB api-positive = 0
Redis PUBSUB NUMSUB cli-positive = 0
missing task Redis EXISTS = 0
cleanup verified: server/CLI processes exited; isolated Redis container absent

The following standalone reproduction was also executed successfully. It requires Bash, Go, Docker, Python 3, curl, ripgrep, and GNU timeout; missing prerequisites fail rather than skip. It builds the binaries, starts only disposable local fixtures, checks missing and existing CLI watches, verifies subscriber cleanup, and tears down its processes/container. To observe the baseline hang, run the missing-watch command against main: timeout exits 124 instead of the fixed exit 1.

Copyable CLI/Redis reproduction
#!/usr/bin/env bash
set -euo pipefail
make build
fixture="ax-watch-proof-$$"
evidence=$(mktemp -d)
server_pid=""
watch_pid=""
cleanup() {
  if [[ -n "$watch_pid" ]] && kill -0 "$watch_pid" 2>/dev/null; then
    kill "$watch_pid"
    wait "$watch_pid" || true
  fi
  if [[ -n "$server_pid" ]] && kill -0 "$server_pid" 2>/dev/null; then
    kill "$server_pid"
    wait "$server_pid"
  fi
  docker rm --force "$fixture" >/dev/null
  rm -rf "$evidence"
}
trap cleanup EXIT
docker run --detach --name "$fixture" -p 127.0.0.1::6379 \
  redis:7-alpine redis-server --save '' --appendonly no >/dev/null
redis_address=$(docker port "$fixture" 6379/tcp)
api_port=$(python3 -c 'import socket; s=socket.socket(); s.bind(("127.0.0.1", 0)); print(s.getsockname()[1]); s.close()')
api_address="127.0.0.1:$api_port"
./bin/ax-server -addr "$api_address" -redis-addr "$redis_address" \
  -substrate-plaintext -substrate-endpoint 127.0.0.1:1 >"$evidence/server.log" 2>&1 &
server_pid=$!
for attempt in {1..100}; do
  if curl --silent --fail "http://$api_address/healthz" >/dev/null; then break; fi
  sleep .05
done
curl --silent --fail "http://$api_address/healthz" >/dev/null
set +e
timeout 3s ./bin/ax --server "http://$api_address" watch task missing
missing_status=$?
set -e
printf 'missing_watch_exit_status=%s\n' "$missing_status"
test "$missing_status" -eq 1
redis() { docker exec "$fixture" redis-cli --raw "$@"; }
redis SET ax:task:default:positive \
  '{"metadata":{"name":"positive","atespace":"default"},"status":{"phase":"Running"}}'
timeout 5s ./bin/ax --server "http://$api_address" watch task positive >"$evidence/watch.log" 2>&1 &
watch_pid=$!
for attempt in {1..100}; do
  if rg -q 'Phase: Running' "$evidence/watch.log"; then break; fi
  sleep .02
done
rg 'Phase: Running' "$evidence/watch.log"
redis PUBLISH ax:pubsub:task:default:positive \
  '{"metadata":{"name":"positive","atespace":"default"},"status":{"phase":"Completed"}}'
wait "$watch_pid"
printf 'existing_watch_exit_status=0\n'
cat "$evidence/watch.log"
rg 'Task reached terminal phase "Completed"' "$evidence/watch.log"
for task in missing positive; do
  for attempt in {1..100}; do
    count=$(redis PUBSUB NUMSUB "ax:pubsub:task:default:$task" | tail -n 1)
    if [[ "$count" == 0 ]]; then break; fi
    sleep .02
  done
  test "$count" = 0
  printf 'Redis subscriber_count %s = %s\n' "$task" "$count"
done

Watching a missing Task waits indefinitely after discarding its initial
lookup error. Return that error while preserving watch registration before
the initial read and subscription cleanup on every exit.
@rakyll

rakyll commented Oct 27, 2026

Copy link
Copy Markdown
Member

Closed via #522.

@rakyll rakyll closed this Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ax watch hangs forever on a task that doesn't exist

2 participants