Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: data race issues with api.Server #11724

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Bug Fixes

* [\#11724](https://github.com/cosmos/cosmos-sdk/pull/11724) Fix data race issues with api.Server
* [\#11693](https://github.com/cosmos/cosmos-sdk/pull/11693) Add validation for gentx cmd.
* [\#11645](https://github.com/cosmos/cosmos-sdk/pull/11645) Fix `--home` flag ignored when running help.
* [\#11558](https://github.com/cosmos/cosmos-sdk/pull/11558) Fix `--dry-run` not working when using tx command.
Expand Down
13 changes: 13 additions & 0 deletions server/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net"
"net/http"
"strings"
"sync"
"time"

"github.com/gogo/gateway"
Expand Down Expand Up @@ -33,6 +34,11 @@ type Server struct {
logger log.Logger
metrics *telemetry.Metrics
listener net.Listener
// Start() is blocking and generally called from a separate goroutine.
// Close() can be called asynchrounously and access shared memory
// via the listener. Therefore, we sync access to Start and Close with
// this mutex to avoid data races.
startCloseMx sync.Mutex
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
}

// CustomGRPCHeaderMatcher for mapping request headers to
Expand Down Expand Up @@ -83,9 +89,11 @@ func New(clientCtx client.Context, logger log.Logger) *Server {
// and are delegated to the Tendermint JSON RPC server. The process is
// non-blocking, so an external signal handler must be used.
func (s *Server) Start(cfg config.Config) error {
s.startCloseMx.Lock()
if cfg.Telemetry.Enabled {
m, err := telemetry.New(cfg.Telemetry)
if err != nil {
s.startCloseMx.Unlock()
return err
}

Expand All @@ -101,6 +109,7 @@ func (s *Server) Start(cfg config.Config) error {

listener, err := tmrpcserver.Listen(cfg.API.Address, tmCfg.MaxOpenConnections)
if err != nil {
s.startCloseMx.Unlock()
return err
}

Expand All @@ -111,15 +120,19 @@ func (s *Server) Start(cfg config.Config) error {

if cfg.API.EnableUnsafeCORS {
allowAllCORS := handlers.CORS(handlers.AllowedHeaders([]string{"Content-Type"}))
s.startCloseMx.Unlock()
return tmrpcserver.Serve(s.listener, allowAllCORS(h), s.logger, tmCfg)
}

s.logger.Info("starting API server...")
s.startCloseMx.Unlock()
return tmrpcserver.Serve(s.listener, s.Router, s.logger, tmCfg)
}

// Close closes the API server.
func (s *Server) Close() error {
s.startCloseMx.Lock()
defer s.startCloseMx.Unlock()
return s.listener.Close()
}

Expand Down