rancher: [BUG] Cluster Agent restarts because of concurrent map write

Rancher Server Setup

  • Rancher version: 2.7.8
  • Installation option (Docker install/Helm Chart):
    • If Helm Chart, Kubernetes Cluster and version (RKE1, RKE2, k3s, EKS, etc): RKE1
  • Proxy/Cert Details: -

Information about the Cluster

  • Kubernetes version: 1.25.13
  • Cluster Type (Local/Downstream):
    • If downstream, what type of cluster? (Custom/Imported or specify provider for Hosted/Infrastructure Provider): Imported, multiple cloud providers

User Information

  • What is the role of the user logged in? Admin

Describe the bug

The cluster agents restarts because of a failed concurrent operation in a goroutine.

To Reproduce

  • Install Rancher 2.7.8
  • Import a cluster
  • Random concurrent bug happens and cluster agent restarts

Result

Degraded UI & UX.

Expected Result

Goroutines are thread safe → No race conditions

Screenshots

Additional context

The first couple of line from the logs of a failed cluster agent:

time="2023-11-09T11:00:00Z" level=error msg="Error during subscribe io: read/write on closed pipe"
time="2023-11-09T11:00:11Z" level=error msg="Error during subscribe websocket: close sent"
W1109 11:00:57.969924      39 transport.go:313] Unable to cancel request for *client.addQuery
W1109 11:00:58.004467      39 transport.go:313] Unable to cancel request for *client.addQuery
time="2023-11-09T11:01:29Z" level=error msg="Error during subscribe io: read/write on closed pipe"
time="2023-11-09T11:01:49Z" level=error msg="Error during subscribe websocket: close sent"
time="2023-11-09T11:02:38Z" level=error msg="Error during subscribe websocket: close sent"
time="2023-11-09T11:02:39Z" level=error msg="Error during subscribe websocket: close sent"
time="2023-11-09T11:02:48Z" level=error msg="Error during subscribe io: read/write on closed pipe"
time="2023-11-09T11:02:48Z" level=error msg="Error during subscribe websocket: close sent"
time="2023-11-09T11:02:48Z" level=error msg="Error during subscribe websocket: close sent"
time="2023-11-09T11:02:48Z" level=error msg="Error during subscribe websocket: close sent"
time="2023-11-09T11:02:48Z" level=error msg="Error during subscribe websocket: close sent"
time="2023-11-09T11:02:48Z" level=error msg="Error during subscribe io: read/write on closed pipe"
fatal error: concurrent map iteration and map write

goroutine 7134961 [running]:
reflect.mapiternext(0x4cb98f?)
        /usr/lib64/go/1.19/src/runtime/map.go:1380 +0x19
reflect.(*MapIter).Next(0xc08bcb4f08?)
        /usr/lib64/go/1.19/src/reflect/value.go:1846 +0x7e
encoding/json.mapEncoder.encode({0xc08bcb4fc8?}, 0xc0ea1ee800, {0x3ba4000?, 0xc14d117ca0?, 0xc14d117c80?}, {0xd?, 0x0?})
        /usr/lib64/go/1.19/src/encoding/json/encode.go:798 +0x33e
encoding/json.structEncoder.encode({{{0xc0ff19ac60?, 0x0?, 0xc05997b8d0?}, 0xc0ff1813e0?}}, 0xc0ea1ee800, {0x3fe8820?, 0xc14d117c80?, 0x4?}, {0x0, 0x1})
        /usr/lib64/go/1.19/src/encoding/json/encode.go:760 +0x1f4
encoding/json.mapEncoder.encode({0x38f20a0?}, 0xc0ea1ee800, {0x3ba3fa0?, 0xc0bf6b35d0?, 0xb8b11cad1332583a?}, {0x9?, 0x0?})
        /usr/lib64/go/1.19/src/encoding/json/encode.go:813 +0x5f5
encoding/json.structEncoder.encode({{{0xc0ff19ab40?, 0x692025?, 0xc18c2cd950?}, 0xc0ff181440?}}, 0xc0ea1ee800, {0x3ecaa80?, 0xc0bf6b35c0?, 0x48?}, {0x0, 0x1})
        /usr/lib64/go/1.19/src/encoding/json/encode.go:760 +0x1f4
encoding/json.(*encodeState).reflectValue(0xc08bcb54e8?, {0x3ecaa80?, 0xc0bf6b35c0?, 0x0?}, {0x0?, 0x31?})
        /usr/lib64/go/1.19/src/encoding/json/encode.go:359 +0x78
encoding/json.(*encodeState).marshal(0xc08bcb5550?, {0x3ecaa80?, 0xc0bf6b35c0?}, {0x1?, 0x0?})
        /usr/lib64/go/1.19/src/encoding/json/encode.go:331 +0xfa
encoding/json.Marshal({0x3ecaa80, 0xc0bf6b35c0})
        /usr/lib64/go/1.19/src/encoding/json/encode.go:160 +0x45
github.com/rancher/apiserver/pkg/types.(*RawResource).MarshalJSON(0xc0c0480900)
        /go/pkg/mod/github.com/rancher/apiserver@v0.0.0-20230515173455-c3b182bdbf7d/pkg/types/server_types.go:50 +0x74
encoding/json.marshalerEncoder(0xc1060bec00, {0x3c46840?, 0xc0c0480900?, 0xc1060bec00?}, {0x44?, 0xca?})
        /usr/lib64/go/1.19/src/encoding/json/encode.go:478 +0xbe
encoding/json.(*encodeState).reflectValue(0x3ad7500?, {0x3c46840?, 0xc0c0480900?, 0x38f20a0?}, {0x70?, 0xa8?})
        /usr/lib64/go/1.19/src/encoding/json/encode.go:359 +0x78
encoding/json.interfaceEncoder(0xc1060bec00, {0x3ad7500?, 0xc0bf6ca8d8?, 0x477297?}, {0xe0?, 0xf?})
        /usr/lib64/go/1.19/src/encoding/json/encode.go:715 +0xc8
encoding/json.structEncoder.encode({{{0xc03a289200?, 0xc0bf6ab020?, 0x3c46840?}, 0xc067d56990?}}, 0xc1060bec00, {0x433f760?, 0xc0bf6ca820?, 0x20302f?}, {0x0, 0x1})
        /usr/lib64/go/1.19/src/encoding/json/encode.go:760 +0x1f4
encoding/json.(*encodeState).reflectValue(0xc08bcb58d0?, {0x433f760?, 0xc0bf6ca820?, 0x0?}, {0x0?, 0x31?})
        /usr/lib64/go/1.19/src/encoding/json/encode.go:359 +0x78
encoding/json.(*encodeState).marshal(0xc8?, {0x433f760?, 0xc0bf6ca820?}, {0x0?, 0x4?})
        /usr/lib64/go/1.19/src/encoding/json/encode.go:331 +0xfa
encoding/json.(*Encoder).Encode(0xc08bcb5b80, {0x433f760, 0xc0bf6ca820})
        /usr/lib64/go/1.19/src/encoding/json/stream.go:206 +0x7e
github.com/rancher/apiserver/pkg/subscribe.writeData(0x451c000?, 0xc15b577350?, 0x451c000?, {{0x45a2070, 0xf}, {0x0, 0x0}, {0x456ca44, 0x6}, {0x0, ...}, ...})
        /go/pkg/mod/github.com/rancher/apiserver@v0.0.0-20230515173455-c3b182bdbf7d/pkg/subscribe/handler.go:103 +0x2de
github.com/rancher/apiserver/pkg/subscribe.handler(0xc106377770, 0xa?, {0x4565ebb, 0x3})
        /go/pkg/mod/github.com/rancher/apiserver@v0.0.0-20230515173455-c3b182bdbf7d/pkg/subscribe/handler.go:72 +0x3f1
github.com/rancher/apiserver/pkg/subscribe.Handler(0x40d93d?, 0x6?, {0x4565ebb?, 0x28?})
        /go/pkg/mod/github.com/rancher/apiserver@v0.0.0-20230515173455-c3b182bdbf7d/pkg/subscribe/handler.go:38 +0x47
github.com/rancher/apiserver/pkg/subscribe.NewHandler.func1(0x3?)
        /go/pkg/mod/github.com/rancher/apiserver@v0.0.0-20230515173455-c3b182bdbf7d/pkg/subscribe/handler.go:33 +0x7b
github.com/rancher/apiserver/pkg/server.handleList(0x8?, 0xe9d1c63a78?, 0x7fe9db6dba48?)
        /go/pkg/mod/github.com/rancher/apiserver@v0.0.0-20230515173455-c3b182bdbf7d/pkg/server/server.go:208 +0x82
github.com/rancher/apiserver/pkg/server.(*Server).handleOp(0xc0a1ab6b88?, 0xc106377770)
        /go/pkg/mod/github.com/rancher/apiserver@v0.0.0-20230515173455-c3b182bdbf7d/pkg/server/server.go:185 +0x32e
github.com/rancher/apiserver/pkg/server.(*Server).handle(0xc005ed2060, 0xc106377770, 0xc106377770?)
        /go/pkg/mod/github.com/rancher/apiserver@v0.0.0-20230515173455-c3b182bdbf7d/pkg/server/server.go:145 +0x652
github.com/rancher/apiserver/pkg/server.(*Server).Handle(...)
        /go/pkg/mod/github.com/rancher/apiserver@v0.0.0-20230515173455-c3b182bdbf7d/pkg/server/server.go:101
github.com/rancher/steve/pkg/server/handler.(*apiServer).apiHandler.func1({0x4f606a0?, 0xc1068f6240?}, 0x4f16a01?)
        /go/pkg/mod/github.com/rancher/steve@v0.0.0-20230717160251-d040cffef385/pkg/server/handler/apiserver.go:97 +0x8c
net/http.HandlerFunc.ServeHTTP(0x4f7eb60?, {0x4f606a0?, 0xc1068f6240?}, 0x4f16a08?)
        /usr/lib64/go/1.19/src/net/http/server.go:2109 +0x2f
github.com/rancher/steve/pkg/auth.ToMiddleware.func1.1({0x4f606a0, 0xc1068f6240}, 0xc106c9e200)
        /go/pkg/mod/github.com/rancher/steve@v0.0.0-20230717160251-d040cffef385/pkg/auth/filter.go:172 +0x30d
net/http.HandlerFunc.ServeHTTP(0xc1068bd0b0?, {0x4f606a0?, 0xc1068f6240?}, 0x449f220?)
        /usr/lib64/go/1.19/src/net/http/server.go:2109 +0x2f
github.com/rancher/apiserver/pkg/urlbuilder.RedirectRewrite.func1({0x4f734b0?, 0xc0ff3c9ea0}, 0xc106c9e200)
        /go/pkg/mod/github.com/rancher/apiserver@v0.0.0-20230515173455-c3b182bdbf7d/pkg/urlbuilder/redirect.go:24 +0x129
net/http.HandlerFunc.ServeHTTP(0xc106c9e100?, {0x4f734b0?, 0xc0ff3c9ea0?}, 0x7fe9d6af9fff?)
        /usr/lib64/go/1.19/src/net/http/server.go:2109 +0x2f
github.com/gorilla/mux.(*Router).ServeHTTP(0xc001716300, {0x4f734b0, 0xc0ff3c9ea0}, 0xc106c9e000)
        /go/pkg/mod/github.com/gorilla/mux@v1.8.0/mux.go:210 +0x1cf
github.com/rancher/rancher/pkg/auth/requests.authedFilter.ServeHTTP({{0x4f32b20, 0xc00a513520}, {0x456669f, 0x4}, {0xc001057f30, 0x1, 0x1}}, {0x4f734b0, 0xc0ff3c9ea0}, 0xc106c9e000)
        /go/src/github.com/rancher/rancher/pkg/auth/requests/filter.go:105 +0x22e
github.com/gorilla/mux.(*Router).ServeHTTP(0xc001716480, {0x4f734b0, 0xc0ff3c9ea0}, 0xc10666be00)
        /go/pkg/mod/github.com/gorilla/mux@v1.8.0/mux.go:210 +0x1cf
github.com/gorilla/mux.(*Router).ServeHTTP(0xc001716540, {0x4f734b0, 0xc0ff3c9ea0}, 0xc10666bc00)
        /go/pkg/mod/github.com/gorilla/mux@v1.8.0/mux.go:210 +0x1cf
github.com/gorilla/mux.(*Router).ServeHTTP(0xc0007d6600, {0x4f734b0, 0xc0ff3c9ea0}, 0xc10666ba00)
        /go/pkg/mod/github.com/gorilla/mux@v1.8.0/mux.go:210 +0x1cf
github.com/gorilla/mux.(*Router).ServeHTTP(0xc0007d69c0, {0x4f734b0, 0xc0ff3c9ea0}, 0xc10666b800)
        /go/pkg/mod/github.com/gorilla/mux@v1.8.0/mux.go:210 +0x1cf
github.com/rancher/rancher/pkg/multiclustermanager.(*DeferredServer).Middleware.func1({0x4f734b0, 0xc0ff3c9ea0}, 0xc0a1ab7518?)
        /go/src/github.com/rancher/rancher/pkg/multiclustermanager/deferred.go:110 +0xa2
net/http.HandlerFunc.ServeHTTP(0x192dd90?, {0x4f734b0?, 0xc0ff3c9ea0?}, 0x38deae0?)
        /usr/lib64/go/1.19/src/net/http/server.go:2109 +0x2f
github.com/rancher/rancher/pkg/api/steve/aggregation.(*aggregationHandler).Middleware.func1({0x4f734b0, 0xc0ff3c9ea0}, 0xc1068bd200?)
        /go/src/github.com/rancher/rancher/pkg/api/steve/aggregation/aggregation.go:52 +0x55
net/http.HandlerFunc.ServeHTTP(0xc10666b700?, {0x4f734b0?, 0xc0ff3c9ea0?}, 0x51c869?)
        /usr/lib64/go/1.19/src/net/http/server.go:2109 +0x2f
github.com/gorilla/mux.(*Router).ServeHTTP(0xc0017163c0, {0x4f734b0, 0xc0ff3c9ea0}, 0xc10666b600)
        /go/pkg/mod/github.com/gorilla/mux@v1.8.0/mux.go:210 +0x1cf
github.com/rancher/rancher/pkg/api/steve/proxy.NewProxyMiddleware.func1.1({0x4f734b0?, 0xc0ff3c9ea0?}, 0xc094084ea8?)
        /go/src/github.com/rancher/rancher/pkg/api/steve/proxy/proxy.go:77 +0x68
net/http.HandlerFunc.ServeHTTP(0x0?, {0x4f734b0?, 0xc0ff3c9ea0?}, 0xc0f28ea8d8?)
        /usr/lib64/go/1.19/src/net/http/server.go:2109 +0x2f
github.com/rancher/rancher/pkg/api/steve/proxy.RewriteLocalCluster.func1({0x4f734b0, 0xc0ff3c9ea0}, 0xc10666b600)
        /go/src/github.com/rancher/rancher/pkg/api/steve/proxy/proxy.go:43 +0x177
net/http.HandlerFunc.ServeHTTP(0xc10666b600?, {0x4f734b0?, 0xc0ff3c9ea0?}, 0x457276b?)
        /usr/lib64/go/1.19/src/net/http/server.go:2109 +0x2f
github.com/rancher/rancher/pkg/websocket.websocketHandler.ServeHTTP({{0x4f3c860?, 0xc00a521f20?}}, {0x4f734b0, 0xc0ff3c9ea0}, 0xc10666b600)
        /go/src/github.com/rancher/rancher/pkg/websocket/handler.go:35 +0xa8
github.com/rancher/apiserver/pkg/middleware.NoCache.func1({0x4f734b0, 0xc0ff3c9ea0}, 0xc12810e6e0?)
        /go/pkg/mod/github.com/rancher/apiserver@v0.0.0-20230515173455-c3b182bdbf7d/pkg/middleware/cache.go:33 +0xf4
net/http.HandlerFunc.ServeHTTP(0x40f5be0?, {0x4f734b0?, 0xc0ff3c9ea0?}, 0x16?)
        /usr/lib64/go/1.19/src/net/http/server.go:2109 +0x2f
github.com/rancher/apiserver/pkg/middleware.ContentTypeOptions.func1({0x4f734b0, 0xc0ff3c9ea0}, 0xc12810e6e0?)
        /go/pkg/mod/github.com/rancher/apiserver@v0.0.0-20230515173455-c3b182bdbf7d/pkg/middleware/cache.go:47 +0xf4
net/http.HandlerFunc.ServeHTTP(0x40f5be0?, {0x4f734b0?, 0xc0ff3c9ea0?}, 0x11?)
        /usr/lib64/go/1.19/src/net/http/server.go:2109 +0x2f
github.com/rancher/rancher/pkg/auth.SetXAPICattleAuthHeader.func1({0x4f734b0, 0xc0ff3c9ea0}, 0xc10666b600)
        /go/src/github.com/rancher/rancher/pkg/auth/server.go:180 +0x1ad
net/http.HandlerFunc.ServeHTTP(0x4f7eab8?, {0x4f734b0?, 0xc0ff3c9ea0?}, 0x4f16a08?)
        /usr/lib64/go/1.19/src/net/http/server.go:2109 +0x2f
github.com/rancher/steve/pkg/auth.ToMiddleware.func1.1({0x4f734b0, 0xc0ff3c9ea0}, 0xc10666b500)
        /go/pkg/mod/github.com/rancher/steve@v0.0.0-20230717160251-d040cffef385/pkg/auth/filter.go:172 +0x30d
net/http.HandlerFunc.ServeHTTP(0xc0f28ea882?, {0x4f734b0?, 0xc0ff3c9ea0?}, 0x46c86e?)
        /usr/lib64/go/1.19/src/net/http/server.go:2109 +0x2f
net/http.serverHandler.ServeHTTP({0xc1068bd050?}, {0x4f734b0, 0xc0ff3c9ea0}, 0xc10666b500)
        /usr/lib64/go/1.19/src/net/http/server.go:2947 +0x30c
net/http.(*conn).serve(0xc105f154a0, {0x4f7eb60, 0xc069c88030})
        /usr/lib64/go/1.19/src/net/http/server.go:1991 +0x607
created by net/http.(*Server).Serve
        /usr/lib64/go/1.19/src/net/http/server.go:3102 +0x4db

goroutine 1 [select, 49 minutes]:
github.com/rancher/remotedialer.ConnectToProxy({0x4f7eaf0, 0xc00007e028}, {0xc001934840, 0x35}, 0xc000a55a20?, 0x0?, 0x0, 0xc001b697d0)
        /go/pkg/mod/github.com/rancher/remotedialer@v0.2.6-0.20220624190122-ea57207bf2b8/client.go:71 +0x7bf
github.com/rancher/remotedialer.ClientConnect({0x4f7eaf0?, 0xc00007e028?}, {0xc001934840?, 0x2c?}, 0xc0017bf710?, 0x2?, 0x2?, 0xc000893400?)
        /go/pkg/mod/github.com/rancher/remotedialer@v0.2.6-0.20220624190122-ea57207bf2b8/client.go:19 +0x38
main.run({0x4f7eaf0, 0xc00007e028})
        /go/src/github.com/rancher/rancher/cmd/agent/main.go:374 +0x1625
main.main()
        /go/src/github.com/rancher/rancher/cmd/agent/main.go:88 +0x2e5

I’ve can provide the whole log if this isn’t enough.

SURE-7615

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (6 by maintainers)

Commits related to this issue

Most upvoted comments

That didn’t solve the issue unfortunately. Could someone from rancher take a look in the logs to at least try and point us in some direction? This issue is still affecting multiple of our clusters, all of which are configured almost the in the same way.

We’re on different cloud providers but the RKE config is practically the same and the apps deployed (from the cluster admin perspective) are also the same.

We have been doing some debugging on the issue the last couple of weeks. We’ve build own own cluster-agent image with a debug log on the writeData function of the apiserver module. This is where the fatal error happens; a fatal error when the MarshallJson of some RawResource (https://github.com/rancher/apiserver/blob/e93bfa07e5f1d4e4a7dc81930041152061af22cf/pkg/types/server_types.go#L38). It appears that some resource is supposed to be encoded to JSON to be sent in the websocket connection to the upstream rancher but the marshalling fails catastrophically.

We first thought this was a load problem; running a local copy of the apiserver and sending millions of parallel requests seemed to pose no problem with a simple “foo-bar” APIResource (as documented in the examples of the apiserver module).

We then tried to understand when & why that happens. The debug log was also panicking / throwing a fatal concurrent map read write error. We finally managed to identify the cuplrit: the counts APIResource (as defined in the steve module https://github.com/rancher/steve/blob/17bd14c5b538de3c87324628400bdf050ff2e352/pkg/resources/counts/counts.go#L46). For some reason unbeknownst to us (we cannot print / log the resource to see its contents as this crashes the agent with the fatal error).

What we’re trying now is to find a way to visualize the counts resource so we at least know about its contents and try to figure out why they would make the MarshalJSON call fail that way. We’ve reduced the frequency in which steve sends updates for the counts - the debounceDuration in https://github.com/rancher/steve/blob/17bd14c5b538de3c87324628400bdf050ff2e352/pkg/resources/counts/buffer.go, hoping that this will mitigate the problem.

Any suggestions from your side?

@JonCrowther We’ve been running your fix for the last week, and it works like a charm. We just installed rancher 2.8.3 today and were surprised to the new steve version missing from the release; I guess the PR was too close to the release.

Any hints on which rancher version will include the fix? We must ship our own version of the cluster agent if we want to update to 2.8.3 on our production servers, and would really appreciate having to avoid the hassle of building the images for the 2.8 release branch.

Sorry for the late reply and thanks again for your work ❤️

@JonCrowther Crash still happens, once per day with the debounce set to 60s. We’ll keep on monitoring for a couple of days to get a better feeling of the mean crash rate 😊