Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Commit a7c279d

Browse files
author
Jay Logue
authored
Merge pull request #278 from openweave/issue/device-manager-rendezvous-hang
Fixed hang in Device Manager on unexpected connection close
2 parents eb0da22 + 7bd935a commit a7c279d

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/device-manager/WeaveDeviceManager.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3394,15 +3394,7 @@ void WeaveDeviceManager::HandleSessionError(WeaveSecurityManager *sm, WeaveConne
33943394
DeviceStatus devStatus;
33953395
DeviceStatus *devStatusArg = NULL;
33963396

3397-
// Bail immediately if not in the correct state. May occur if the connection closes abruptly and the
3398-
// SecurityManager's HandleConnectionClosed callback fires _after_ the DeviceManager's own callback.
3399-
// In this case, con is already closed and mOnError has already been called, so we should just exit.
3400-
if (devMgr->mConState != kConnectionState_StartSession)
3401-
{
3402-
return;
3403-
}
3404-
3405-
// Report the result.
3397+
// Log the error.
34063398
if (localErr == WEAVE_ERROR_STATUS_REPORT_RECEIVED && statusReport != NULL)
34073399
{
34083400
WeaveLogProgress(DeviceManager, "Secure session failed: %s", StatusReportStr(statusReport->mProfileId, statusReport->mStatusCode));
@@ -3416,6 +3408,24 @@ void WeaveDeviceManager::HandleSessionError(WeaveSecurityManager *sm, WeaveConne
34163408
WeaveLogProgress(DeviceManager, "Secure session failed: %s", ErrorStr(localErr));
34173409
}
34183410

3411+
// Sanity check that the connection state is expected.
3412+
//
3413+
// HandleSessionError() is called by the WeaveSecurityManager when there is a
3414+
// failure to establish a security session. This can occur in two cases relative
3415+
// to the Device Manager's connection state value (mConState):
3416+
// 1) if the session failed because the underlying connection was closed unexpectedly
3417+
// AND the ordering of events in the network caused HandleConnectionClosed() to be
3418+
// called before HandleSessionError(), then the connection state will be NotConnected
3419+
// 2) if the session failed for any other reason, then the connection state will be
3420+
// StartSession.
3421+
// Any other connection state value is unexpected and likely signals a logic bug.
3422+
if (devMgr->mConState != kConnectionState_StartSession &&
3423+
devMgr->mConState != kConnectionState_NotConnected)
3424+
{
3425+
WeaveLogError(DeviceManager, "Wrong connection state in HandleSessionError()");
3426+
return;
3427+
}
3428+
34193429
// If the device returned a Common:Busy response, it likely means it's in a state where it can't perform
34203430
// the crypto operations necessary to initiate a new session (e.g. because it's busy establishing a secure
34213431
// session with the service). In this situation, we wait a little bit and retry the operation, but
@@ -3499,8 +3509,7 @@ void WeaveDeviceManager::RestartRemotePassiveRendezvousListen()
34993509
// Nobody else is allowed to try anything while we're reconnecting to the assisting device.
35003510
mOpState = kOpState_RestartRemotePassiveRendezvous;
35013511

3502-
// Reconnect to assisting device and attempt to reuse existing secure session. Establish new secure session from
3503-
// scratch if necessary.
3512+
// Reconnect to assisting device.
35043513
err = StartReconnectToAssistingDevice();
35053514
}
35063515

0 commit comments

Comments
 (0)