hostd
hostd copied to clipboard
fix alerts panic
Fixes a panic when encoding the alerts array during high frequency updates. Honestly surprised we haven't run into this before.
fatal error: concurrent map iteration and map write
goroutine 13842512 [running]:
internal/runtime/maps.fatal({0x10d45e2?, 0x475113?})
runtime/panic.go:1058 +0x18
internal/runtime/maps.(*Iter).Next(0xc00a9ced50?)
internal/runtime/maps/table.go:683 +0x86
reflect.mapIterNext(0xf6e640?)
reflect/map_swiss.go:203 +0x13
reflect.(*MapIter).Next(0xc001728fd0)
reflect/map_swiss.go:358 +0x3e
encoding/json.mapEncoder.encode({0x15?}, 0xc000132940, {0xf9e080?, 0xc001e8efb0?, 0xc001e8efb0?}, {0x7?, 0x0?})
encoding/json/encode.go:768 +0x329
encoding/json.structEncoder.encode({{{0xc0048ec008, 0x5, 0x8}, 0xc00602a7b0, 0xc00602a7e0}}, 0xc000132940, {0x10313c0?, 0xc001e8ef78?, 0xc001729338?}, {0x0, ...})
encoding/json/encode.go:727 +0x30a
encoding/json.arrayEncoder.encode({0xc001729320?}, 0xc000132940, {0xf78140?, 0xc00a519560?, 0x0?}, {0x30?, 0x93?})
encoding/json/encode.go:870 +0xcf
encoding/json.sliceEncoder.encode({0x4170d4?}, 0xc000132940, {0xf78140?, 0xc00a519560?, 0xf78140?}, {0x3d?, 0x60?})
encoding/json/encode.go:843 +0x335
encoding/json.(*encodeState).reflectValue(0xc000132940, {0xf78140?, 0xc00a519560?, 0xc0017294f8?}, {0xc0?, 0xc3?})
encoding/json/encode.go:333 +0x73
encoding/json.(*encodeState).marshal(0xc001e8ef78?, {0xf78140?, 0xc00a519560?}, {0x28?, 0x4f?})
encoding/json/encode.go:309 +0xb2
encoding/json.(*Encoder).Encode(0xc001729638, {0xf78140, 0xc00a519560})
encoding/json/stream.go:210 +0xd0
go.sia.tech/jape.Context.Encode({{0x1a20e00, 0xc009cc1500}, 0xc00a659040, {0x0, 0x0, 0x0}}, {0xf78140, 0xc00a519560})
go.sia.tech/[email protected]/server.go:57 +0x32b
go.sia.tech/hostd/v2/api.(*api).writeResponse(0xc00621c000, {{0x1a20e00, 0xc009cc1500}, 0xc00a659040, {0x0, 0x0, 0x0}}, {0xf78140, 0xc00a519560})
go.sia.tech/hostd/v2/api/endpoints.go:66 +0x16e
go.sia.tech/hostd/v2/api.(*api).handleGETAlerts(0xc00621c000, {{0x1a20e00, 0xc009cc1500}, 0xc00a659040, {0x0, 0x0, 0x0}})
go.sia.tech/hostd/v2/api/endpoints.go:155 +0x86
go.sia.tech/jape.Mux.adaptor.func1({0x1a20e00?, 0xc009cc1500?}, 0xc00a510dc8?, {0x0?, 0x18?, 0xc007780008?})
go.sia.tech/[email protected]/server.go:171 +0x52
github.com/julienschmidt/httprouter.(*Router).ServeHTTP(0xc005f4e240, {0x1a20e00, 0xc009cc1500}, 0xc00a659040)
github.com/julienschmidt/[email protected]/router.go:387 +0x7cb
main.runRootCmd.runRootCmd.BasicAuth.func41.func42({0x1a20e00, 0xc009cc1500}, 0xc00a659040)
go.sia.tech/[email protected]/server.go:230 +0x99
net/http.HandlerFunc.ServeHTTP(0xc001729b10?, {0x1a20e00?, 0xc009cc1500?}, 0x47cff2?)
net/http/server.go:2294 +0x29
main.webRouter.ServeHTTP({{0x1a19f20?, 0xc0004f2a20?}, {0x1a185e0?, 0xc0004f2360?}}, {0x1a20e00?, 0xc009cc1500?}, 0x1?)
go.sia.tech/hostd/v2/cmd/hostd/web.go:18 +0xd8
net/http.serverHandler.ServeHTTP({0xc002680720?}, {0x1a20e00?, 0xc009cc1500?}, 0x6?)
net/http/server.go:3301 +0x8e
net/http.(*conn).serve(0xc00583e000, {0x1a23638, 0xc0136a7dd0})
net/http/server.go:2102 +0x625
created by net/http.(*Server).Serve in goroutine 228
net/http/server.go:3454 +0x485
goroutine 1 [chan receive, 2594 minutes]:
main.runRootCmd({_, _}, {{0x0, 0x0}, {0xc00049a1f0, 0xe}, {0xc0004f0050, 0x46}, 0x0, {{0xc00049a257, ...}, ...}, ...}, ...)
go.sia.tech/hostd/v2/cmd/hostd/run.go:531 +0x60c5
main.main()
go.sia.tech/hostd/v2/cmd/hostd/main.go:534 +0x2239
goroutine 5 [syscall, 3292 minutes]:
os/signal.signal_recv()