go-echarts/statsview

Page won't show any graph and there is data race

bentcoder opened this issue · 2 comments

Hi,

There is no error in Network tab but the graph won't show any data on http://localhost:18066/debug/statsview. However, http://localhost:18066/debug/pprof/ is fine otherwise. Not sure if it is related to the data race but when I start the app, I see data race at the beginning.

Thanks

Part of my code.

	...
	go func() {
		if err := srv.Start(); err != nil { // Running on 0.0.0.0:8001
			log.Fatal(err.Error())
		}
	}()

	mgr := statsview.New()
	go mgr.Start() // <-- DATARACE
	defer mgr.Stop()
	...
==================
WARNING: DATA RACE
Write at 0x00c0000deb00 by goroutine 26:
  github.com/go-echarts/statsview/viewer.(*StatsMgr).Tick()
      /Users/me/go/pkg/mod/github.com/go-echarts/statsview@v0.3.4/viewer/viewer.go:193 +0xbd
  github.com/go-echarts/statsview/viewer.(*StackViewer).Serve()
      /Users/me/go/pkg/mod/github.com/go-echarts/statsview@v0.3.4/viewer/stack.go:51 +0x69
  github.com/go-echarts/statsview/viewer.Viewer.Serve-fm()
      <autogenerated>:1 +0x75
  net/http.HandlerFunc.ServeHTTP()
      /usr/local/go/src/net/http/server.go:2109 +0x4d
  net/http.(*ServeMux).ServeHTTP()
      /usr/local/go/src/net/http/server.go:2487 +0xc5
  github.com/rs/cors.(*Cors).Handler.func1()
      /Users/me/go/pkg/mod/github.com/rs/cors@v1.8.3/cors.go:236 +0x343
  net/http.HandlerFunc.ServeHTTP()
      /usr/local/go/src/net/http/server.go:2109 +0x4d
  net/http.serverHandler.ServeHTTP()
      /usr/local/go/src/net/http/server.go:2947 +0x641
  net/http.(*conn).serve()
      /usr/local/go/src/net/http/server.go:1991 +0xbe4
  net/http.(*Server).Serve.func3()
      /usr/local/go/src/net/http/server.go:3102 +0x58

Previous write at 0x00c0000deb00 by goroutine 27:
  github.com/go-echarts/statsview/viewer.(*StatsMgr).Tick()
      /Users/me/go/pkg/mod/github.com/go-echarts/statsview@v0.3.4/viewer/viewer.go:193 +0xbd
  github.com/go-echarts/statsview/viewer.(*GCNumViewer).Serve()
      /Users/me/go/pkg/mod/github.com/go-echarts/statsview@v0.3.4/viewer/gcnum.go:48 +0x69
  github.com/go-echarts/statsview/viewer.Viewer.Serve-fm()
      <autogenerated>:1 +0x75
  net/http.HandlerFunc.ServeHTTP()
      /usr/local/go/src/net/http/server.go:2109 +0x4d
  net/http.(*ServeMux).ServeHTTP()
      /usr/local/go/src/net/http/server.go:2487 +0xc5
  github.com/rs/cors.(*Cors).Handler.func1()
      /Users/me/go/pkg/mod/github.com/rs/cors@v1.8.3/cors.go:236 +0x343
  net/http.HandlerFunc.ServeHTTP()
      /usr/local/go/src/net/http/server.go:2109 +0x4d
  net/http.serverHandler.ServeHTTP()
      /usr/local/go/src/net/http/server.go:2947 +0x641
  net/http.(*conn).serve()
      /usr/local/go/src/net/http/server.go:1991 +0xbe4
  net/http.(*Server).Serve.func3()
      /usr/local/go/src/net/http/server.go:3102 +0x58

Goroutine 26 (running) created at:
  net/http.(*Server).Serve()
      /usr/local/go/src/net/http/server.go:3102 +0x837
  net/http.(*Server).ListenAndServe()
      /usr/local/go/src/net/http/server.go:2999 +0xc4
  github.com/go-echarts/statsview.(*ViewManager).Start()
      /Users/me/go/pkg/mod/github.com/go-echarts/statsview@v0.3.4/statsview.go:36 +0x3d
  main.main.func3()
      /Users/me/dev/golang/blog/cmd/api/main.go:100 +0x39

Goroutine 27 (running) created at:
  net/http.(*Server).Serve()
      /usr/local/go/src/net/http/server.go:3102 +0x837
  net/http.(*Server).ListenAndServe()
      /usr/local/go/src/net/http/server.go:2999 +0xc4
  github.com/go-echarts/statsview.(*ViewManager).Start()
      /Users/me/go/pkg/mod/github.com/go-echarts/statsview@v0.3.4/statsview.go:36 +0x3d
  main.main.func3()
      /Users/me/dev/golang/blog/cmd/api/main.go:100 +0x39
==================

==================
WARNING: DATA RACE
Write at 0x000002015408 by goroutine 15:
  github.com/go-echarts/statsview/viewer.(*StatsMgr).polling()
      /Users/me/go/pkg/mod/github.com/go-echarts/statsview@v0.3.4/viewer/viewer.go:205 +0x329
  github.com/go-echarts/statsview/viewer.NewStatsMgr.func1()
      /Users/me/go/pkg/mod/github.com/go-echarts/statsview@v0.3.4/viewer/viewer.go:187 +0x39

Previous read at 0x000002015408 by goroutine 23:
  github.com/go-echarts/statsview/viewer.(*HeapViewer).Serve()
      /Users/me/go/pkg/mod/github.com/go-echarts/statsview@v0.3.4/viewer/heap.go:58 +0x372
  github.com/go-echarts/statsview/viewer.Viewer.Serve-fm()
      <autogenerated>:1 +0x75
  net/http.HandlerFunc.ServeHTTP()
      /usr/local/go/src/net/http/server.go:2109 +0x4d
  net/http.(*ServeMux).ServeHTTP()
      /usr/local/go/src/net/http/server.go:2487 +0xc5
  github.com/rs/cors.(*Cors).Handler.func1()
      /Users/me/go/pkg/mod/github.com/rs/cors@v1.8.3/cors.go:236 +0x343
  net/http.HandlerFunc.ServeHTTP()
      /usr/local/go/src/net/http/server.go:2109 +0x4d
  net/http.serverHandler.ServeHTTP()
      /usr/local/go/src/net/http/server.go:2947 +0x641
  net/http.(*conn).serve()
      /usr/local/go/src/net/http/server.go:1991 +0xbe4
  net/http.(*Server).Serve.func3()
      /usr/local/go/src/net/http/server.go:3102 +0x58

Goroutine 15 (running) created at:
  github.com/go-echarts/statsview/viewer.NewStatsMgr()
      /Users/me/go/pkg/mod/github.com/go-echarts/statsview@v0.3.4/viewer/viewer.go:187 +0x184
  github.com/go-echarts/statsview.New()
      /Users/me/go/pkg/mod/github.com/go-echarts/statsview@v0.3.4/statsview.go:87 +0x8c9
  main.main()
      /Users/me/dev/golang/blog/cmd/api/main.go:99 +0xb0e

Goroutine 23 (running) created at:
  net/http.(*Server).Serve()
      /usr/local/go/src/net/http/server.go:3102 +0x837
  net/http.(*Server).ListenAndServe()
      /usr/local/go/src/net/http/server.go:2999 +0xc4
  github.com/go-echarts/statsview.(*ViewManager).Start()
      /Users/me/go/pkg/mod/github.com/go-echarts/statsview@v0.3.4/statsview.go:36 +0x3d
  main.main.func3()
      /Users/me/dev/golang/blog/cmd/api/main.go:100 +0x39
==================

Screenshot 2023-02-25 at 12 40 04

Screenshot 2023-02-25 at 12 40 14

adlion commented

any solution
I have same problem

There are two variables which concurrently reads and writes StatsMgr.last and memstats.T. It`s easy fixes by adding lock or atomic operation.