Skip to content

Potential Goroutine Leak Due to Unbuffered Channel #25180

@CertiK-Geth

Description

@CertiK-Geth

Locations

Description

The Start function in the Server struct initiates a goroutine that sends errors to an unbuffered channel errCh. If the ctx.Done() branch wins the race condition, the goroutine will block indefinitely, leading to a resource leak. Below is the relevant code snippet:

errCh := make(chan error)

go func(enableUnsafeCORS bool) {
    s.logger.Info("starting API server...", "address", cfg.API.Address)

    if enableUnsafeCORS {
        allowAllCORS := handlers.CORS(handlers.AllowedHeaders([]string{"Content-Type"}))
        errCh <- cmtrpcserver.Serve(s.listener, allowAllCORS(s.Router), cmtlogwrapper.CometLoggerWrapper{Logger: s.logger}, cmtCfg)
    } else {
        errCh <- cmtrpcserver.Serve(s.listener, s.Router, cmtlogwrapper.CometLoggerWrapper{Logger: s.logger}, cmtCfg)
    }
}(cfg.API.EnableUnsafeCORS)
	// Start a blocking select to wait for an indication to stop the server or that
	// the server failed to start properly.
	select {
	case <-ctx.Done():
		// The calling process canceled or closed the provided context, so we must
		// gracefully stop the API server.
		s.logger.Info("stopping API server...", "address", cfg.API.Address)
		return s.Close()

	case err := <-errCh:
		s.logger.Error("failed to start API server", "err", err)
		return err
	}

The same issue exists in the function StartGRPCServer.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions