#7325·kubeedge

router: non-200 responses from edge cause the REST request to be replayed to the edge node 3 times

Author: tarone-saloniCreated Sep 22, 2026Updated Sep 22, 2026
Labelskind/bug

What happened:

RestHandler.httpHandler wraps the entire request handling, including dispatching the message to the edge node, in retry.Do with 3 attempts (http.go#L127-L231). Inside that function, any response whose status is not exactly 200 is turned into an error:

go
if response.StatusCode != http.StatusOK {
    errMsg := string(body)
    return errors.New(errMsg)
}

Each retry calls the rule handler again. That creates a new message ID and sends a new message to the edge application. So whenever the edge app answers with anything other than 200:

  1. The request is delivered to the edge application 3 times. A non-idempotent POST (e.g. "create", "actuate device", "open valve") runs 3 times, even though the edge app handled the first one successfully.
  2. Valid 2xx responses are treated as failures. 201 Created, 202 Accepted and 204 No Content all fall into this branch.
  3. The client gets 500 with a concatenated body (All attempts fail:\n#1: ...\n#2: ...\n#3: ...) instead of the status and body the edge actually returned.
  4. Response headers are duplicated, because w.Header().Add runs on every attempt before the status check.
  5. On timeout, the wait triples. Rest.Forward returns 408 after restTimeout (60s by default), and that is retried as well. The client waits ~3 × 60s + 2s, and the edge node receives the request 3 times.

requestForward (used when the edge node is connected to another cloudcore instance) has the same != http.StatusOK check (http.go#L287), so a non-200 reply there also re-forwards the whole request.

What you expected to happen:

A response from the edge application should be returned to the client once, with its original status code, headers and body, whatever the status is. Retrying makes sense only for failures that happen before the request reaches the edge (for example, looking up the node's cloudcore IP). It should not happen after the edge has already processed the request.

How to reproduce it (as minimally and precisely as possible):

Add this to cloud/pkg/router/listener and run go test -gcflags=all=-l -run TestNon200Replayed -v ./cloud/pkg/router/listener/:

go
func TestNon200Replayed(t *testing.T) {
	p := gomonkey.ApplyFunc(GetEdgeToCloudCoreIP, func(context.Context, string) (string, error) { return "10.0.0.1", nil })
	defer p.Reset()
	p.ApplyFunc(util.GetLocalIP, func(string) (string, error) { return "10.0.0.1", nil })

	rh := &RestHandler{}
	calls := 0
	rh.AddListener("/default/api", Handle(func(interface{}) (interface{}, error) {
		calls++
		h := http.Header{}
		h.Set("X-Edge", "v")
		return &http.Response{StatusCode: http.StatusCreated, Header: h, Body: io.NopCloser(strings.NewReader("created"))}, nil
	}))

	rec := httptest.NewRecorder()
	rh.httpHandler(rec, httptest.NewRequest(http.MethodPost, "/node1/default/api", strings.NewReader(`{}`)))
	t.Logf("handler invoked %d times, client got %d, X-Edge=%q", calls, rec.Code, rec.Header().Values("X-Edge"))
}

Output:

handler invoked 3 times, client got 500, X-Edge=["v" "v" "v"]

204 and 408 give the same result: 3 invocations, and a 500 to the client.

Anything else we need to know?:

  • The retry was introduced in 41206a690 ("Supports high availability of router messages"). It looks like it was meant to cover a failed cloudcore IP lookup during HA failover, but it wraps the edge dispatch as well.
  • Related but separate: #5428 / #6811 change the timeout status from 408 to 504 and preserve the final non-200 status. #6811 still returns an error for non-200 responses, so the request is still replayed to the edge 3 times with that change applied.
  • A possible direction: limit the retry to the cloudcore IP resolution (and the connection error from requestForward), and pass any response from the edge or the peer cloudcore straight through without retrying.

Environment:

  • KubeEdge version: master (d062d2983)
  • Component: cloudcore, router module (cloud/pkg/router/listener/http.go)