Skip to content

Commit efe20cd

Browse files
author
Marco Trettner
authored
Merge pull request #21 from grid-x/fix/fix_close_nil_channel_panic_on_stop
Improve Handling of stopped channel stopping the Server
2 parents 28c3102 + 62271c7 commit efe20cd

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

ocppj/central_system_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ func (suite *OcppJTestSuite) TestServerStoppedError() {
5454
assert.Error(t, err, "ocppj server is not started, couldn't send request")
5555
}
5656

57+
func (suite *OcppJTestSuite) TestServerStopBeforeStart() {
58+
t := suite.T()
59+
60+
// Stop server
61+
suite.mockServer.On("Stop").Return(nil)
62+
suite.centralSystem.Stop()
63+
64+
// Start server (should return)
65+
suite.mockServer.On("Start", mock.AnythingOfType("int"), mock.AnythingOfType("string")).Return(nil)
66+
suite.centralSystem.Start(8887, "/{ws}")
67+
68+
assert.True(t, suite.serverDispatcher.IsRunning())
69+
}
70+
5771
// ----------------- SendRequest tests -----------------
5872

5973
func (suite *OcppJTestSuite) TestCentralSystemSendRequest() {

ocppj/dispatcher.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ func NewDefaultServerDispatcher(queueMap ServerQueueMap) *DefaultServerDispatche
381381
requestChannel: nil,
382382
readyForDispatch: make(chan string, 1),
383383
timeout: defaultMessageTimeout,
384+
stoppedC: make(chan struct{}, 1),
384385
}
385386
d.pendingRequestState = NewServerState(&d.mutex)
386387
return d
@@ -389,7 +390,6 @@ func NewDefaultServerDispatcher(queueMap ServerQueueMap) *DefaultServerDispatche
389390
func (d *DefaultServerDispatcher) Start() {
390391
d.requestChannel = make(chan string, 20)
391392
d.timerC = make(chan string, 10)
392-
d.stoppedC = make(chan struct{}, 1)
393393
d.running = true
394394
go d.messagePump()
395395
}
@@ -404,7 +404,12 @@ func (d *DefaultServerDispatcher) Stop() {
404404
d.mutex.Lock()
405405
defer d.mutex.Unlock()
406406
d.running = false
407-
close(d.stoppedC)
407+
408+
select {
409+
case <-d.stoppedC:
410+
default:
411+
close(d.stoppedC)
412+
}
408413
}
409414

410415
func (d *DefaultServerDispatcher) SetTimeout(timeout time.Duration) {

ocppj/server.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ func NewServer(wsServer ws.WsServer, dispatcher ServerDispatcher, stateHandler S
6767
server: wsServer,
6868
RequestState: stateHandler,
6969
dispatcher: dispatcher,
70-
stopped: make(chan struct{})}
70+
stopped: make(chan struct{}),
71+
}
7172
for _, profile := range profiles {
7273
s.AddProfile(profile)
7374
}
@@ -148,7 +149,11 @@ func (s *Server) Start(listenPort int, listenPath string) {
148149
// Stops the server.
149150
// This clears all pending requests and causes the Start function to return.
150151
func (s *Server) Stop() {
151-
close(s.stopped)
152+
select {
153+
case <-s.stopped:
154+
default:
155+
close(s.stopped)
156+
}
152157
s.waitGroup.Wait()
153158
s.server.Stop()
154159
s.dispatcher.Stop()

0 commit comments

Comments
 (0)