<commit_before>package rpc

import (
	"errors"
	"github.com/mitchellh/packer/packer"
	"net"
	"net/rpc"
)

// A Server is a Golang RPC server that has helper methods for automatically
// setting up the endpoints for Packer interfaces.
type Server struct {
	listener net.Listener
	server *rpc.Server
}

// Creates and returns a new Server.
func NewServer() *Server {
	return &Server{
		server: rpc.NewServer(),
	}
}

func (s *Server) Address() string {
	if s.listener == nil {
		panic("Server not listening.")
	}

	return s.listener.Addr().String()
}

func (s *Server) RegisterUi(ui packer.Ui) {
	s.server.RegisterName("Ui", &UiServer{ui})
}

func (s *Server) Start() error {
	if s.listener != nil {
		return errors.New("Server already started.")
	}

	// Start the TCP listener and a goroutine responsible for cleaning up the
	// listener.
	s.listener = netListenerInRange(portRangeMin, portRangeMax)
	if s.listener == nil {
		return errors.New("Could not open a port ot listen on.")
	}

	// Start accepting connections
	go func() {
		for {
			conn, err := s.listener.Accept()
			if err != nil {
				break
			}

			go s.server.ServeConn(conn)
		}
	}()

	return nil
}

func (s *Server) Stop() {
	if s.listener != nil {
		s.listener.Close()
		s.listener = nil
	}
}<commit_msg>Fix race condition issue where listner could become nil<commit_after>package rpc

import (
	"errors"
	"github.com/mitchellh/packer/packer"
	"net"
	"net/rpc"
)

// A Server is a Golang RPC server that has helper methods for automatically
// setting up the endpoints for Packer interfaces.
type Server struct {
	listener net.Listener
	server *rpc.Server
}

// Creates and returns a new Server.
func NewServer() *Server {
	return &Server{
		server: rpc.NewServer(),
	}
}

func (s *Server) Address() string {
	if s.listener == nil {
		panic("Server not listening.")
	}

	return s.listener.Addr().String()
}

func (s *Server) RegisterUi(ui packer.Ui) {
	s.server.RegisterName("Ui", &UiServer{ui})
}

func (s *Server) Start() error {
	if s.listener != nil {
		return errors.New("Server already started.")
	}

	// Start the TCP listener and a goroutine responsible for cleaning up the
	// listener.
	s.listener = netListenerInRange(portRangeMin, portRangeMax)
	if s.listener == nil {
		return errors.New("Could not open a port ot listen on.")
	}

	// Start accepting connections
	go func(l net.Listener) {
		for {
			conn, err := l.Accept()
			if err != nil {
				break
			}

			go s.server.ServeConn(conn)
		}
	}(s.listener)

	return nil
}

func (s *Server) Stop() {
	if s.listener != nil {
		s.listener.Close()
		s.listener = nil
	}
}
{'fraction_non_alphanumeric': 0.12956555171088044, 'fraction_numerical': 0.0, 'mean_word_length': 3.387858347386172, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1638192', 'n_tokens_mistral': 932, 'n_tokens_neox': 878, 'n_words': 278}