diff --git a/application/src/main/java/org/thingsboard/server/service/state/DefaultDeviceStateService.java b/application/src/main/java/org/thingsboard/server/service/state/DefaultDeviceStateService.java index 40f453de42..5a2b106339 100644 --- a/application/src/main/java/org/thingsboard/server/service/state/DefaultDeviceStateService.java +++ b/application/src/main/java/org/thingsboard/server/service/state/DefaultDeviceStateService.java @@ -358,16 +358,26 @@ public class DefaultDeviceStateService extends AbstractPartitionBasedService() { @Override public void onSuccess(DeviceStateData state) { - TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, tenantId, device.getId()); - Set deviceIds = partitionedEntities.get(tpi); - boolean isMyPartition = deviceIds != null; - if (isMyPartition) { - deviceIds.add(state.getDeviceId()); - initializeActivityState(deviceId, state); + try { + TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, tenantId, device.getId()); + Set deviceIds = partitionedEntities.get(tpi); + boolean isMyPartition = deviceIds != null; + if (isMyPartition) { + deviceIds.add(state.getDeviceId()); + initializeActivityState(deviceId, state); + callback.onSuccess(); + } else { + log.debug("[{}][{}] Device belongs to external partition. Probably rebalancing is in progress. Topic: {}", tenantId, deviceId, tpi.getFullTopicName()); + callback.onFailure(new RuntimeException("Device belongs to external partition " + tpi.getFullTopicName() + "!")); + } + } catch (TenantNotFoundException e) { + // Tenant was removed (e.g. during shutdown / test tearDown) between fetchDeviceState completing + // and partition resolution. Device-state registration is moot in that case. + log.debug("[{}][{}] Tenant not found while registering device to the state service; skipping.", tenantId, deviceId); callback.onSuccess(); - } else { - log.debug("[{}][{}] Device belongs to external partition. Probably rebalancing is in progress. Topic: {}", tenantId, deviceId, tpi.getFullTopicName()); - callback.onFailure(new RuntimeException("Device belongs to external partition " + tpi.getFullTopicName() + "!")); + } catch (Throwable t) { + log.warn("[{}][{}] Failed to register device to the state service", tenantId, deviceId, t); + callback.onFailure(t); } }