[VOL-5263] - fix for openolt adpter crash issue

Change-Id: Ieeb22b562837b741a5ef902d0cf92e252bc5d1b4
Signed-off-by: Akash Soni <akash.soni@radisys.com>
diff --git a/internal/pkg/core/device_handler.go b/internal/pkg/core/device_handler.go
index f41ab4d..725acec 100755
--- a/internal/pkg/core/device_handler.go
+++ b/internal/pkg/core/device_handler.go
@@ -2513,13 +2513,6 @@
 
 	dh.StopAllFlowRoutines(ctx)
 
-	err := dh.cleanupDeviceResources(ctx)
-	if err != nil {
-		logger.Errorw(ctx, "could-not-remove-device-from-KV-store", log.Fields{"device-id": dh.device.Id, "err": err})
-	} else {
-		logger.Debugw(ctx, "successfully-removed-device-from-Resource-manager-KV-store", log.Fields{"device-id": dh.device.Id})
-	}
-
 	dh.lockDevice.RLock()
 	// Stop the Stats collector
 	if dh.isCollectorActive {
@@ -2534,6 +2527,14 @@
 		dh.stopIndications <- true
 	}
 	dh.lockDevice.RUnlock()
+
+	err := dh.cleanupDeviceResources(ctx)
+	if err != nil {
+		logger.Errorw(ctx, "could-not-remove-device-from-KV-store", log.Fields{"device-id": dh.device.Id, "err": err})
+	} else {
+		logger.Debugw(ctx, "successfully-removed-device-from-Resource-manager-KV-store", log.Fields{"device-id": dh.device.Id})
+	}
+
 	dh.removeOnuIndicationChannels(ctx)
 	//Reset the state
 	if dh.Client != nil {