Skip to content

Commit 3f7399c

Browse files
committed
Fix handling ship error state handling
The connection wasn’t closed and thus the device in a stalled process
1 parent efc8640 commit 3f7399c

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

ship/connection.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ func (c *ShipConnection) CloseConnection(safe bool, reason string) {
114114
}
115115

116116
_ = c.sendShipModel(model.MsgTypeEnd, closeMessage)
117-
return
117+
118+
if c.smeState != smeError {
119+
return
120+
}
118121
}
119122

120123
c.DataHandler.CloseDataConnection()

ship/handshake.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,26 @@ func (c *ShipConnection) getState() shipMessageExchangeState {
7777
// handle handshake state transitions
7878
func (c *ShipConnection) handleState(timeout bool, message []byte) {
7979
switch c.getState() {
80+
case smeError:
81+
logging.Log.Debug(c.RemoteSKI, "connection is in error state")
82+
return
83+
8084
// cmiStateInit
8185
case cmiStateInitStart:
8286
// triggered without a message received
8387
c.handshakeInit_cmiStateInitStart()
8488

8589
case cmiStateClientWait:
8690
if timeout {
87-
c.endHandshakeWithError(errors.New("ship handshake timeout"))
91+
c.endHandshakeWithError(errors.New("ship client handshake timeout"))
8892
return
8993
}
9094

9195
c.handshakeInit_cmiStateClientWait(message)
9296

9397
case cmiStateServerWait:
9498
if timeout {
95-
c.endHandshakeWithError(errors.New("ship handshake timeout"))
99+
c.endHandshakeWithError(errors.New("ship server handshake timeout"))
96100
return
97101
}
98102
c.handshakeInit_cmiStateServerWait(message)

ship/hs_helper_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type dataHandlerTest struct {
1313
sentMessage []byte
1414

1515
mux sync.Mutex
16+
17+
handleConnectionClosedInvoked bool
1618
}
1719

1820
func (s *dataHandlerTest) lastMessage() []byte {
@@ -44,9 +46,11 @@ func (s *dataHandlerTest) HandleClosedConnection(connection *ShipConnection) {}
4446

4547
var _ ShipServiceDataProvider = (*dataHandlerTest)(nil)
4648

47-
func (s *dataHandlerTest) IsRemoteServiceForSKIPaired(string) bool { return true }
48-
func (s *dataHandlerTest) HandleConnectionClosed(*ShipConnection, bool) {}
49-
func (s *dataHandlerTest) ReportServiceShipID(string, string) {}
49+
func (s *dataHandlerTest) IsRemoteServiceForSKIPaired(string) bool { return true }
50+
func (s *dataHandlerTest) HandleConnectionClosed(*ShipConnection, bool) {
51+
s.handleConnectionClosedInvoked = true
52+
}
53+
func (s *dataHandlerTest) ReportServiceShipID(string, string) {}
5054

5155
func initTest(role shipRole) (*ShipConnection, *dataHandlerTest) {
5256
localDevice := spine.NewDeviceLocalImpl("TestBrandName", "TestDeviceModel", "TestSerialNumber", "TestDeviceCode",

ship/hs_init_client_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ func (s *InitClientSuite) Test_ClientWait() {
5858
shutdownTest(sut)
5959
}
6060

61+
func (s *InitClientSuite) Test_ClientWait_Timeout() {
62+
sut, data := initTest(s.role)
63+
64+
sut.setState(cmiStateClientWait)
65+
66+
sut.handleState(true, nil)
67+
68+
assert.Equal(s.T(), smeError, sut.getState())
69+
assert.NotNil(s.T(), data.lastMessage())
70+
assert.Equal(s.T(), data.handleConnectionClosedInvoked, true)
71+
72+
shutdownTest(sut)
73+
}
74+
6175
func (s *InitClientSuite) Test_ClientWait_InvalidMsgType() {
6276
sut, data := initTest(s.role)
6377

0 commit comments

Comments
 (0)