Azure / Azure/azure-iot-sdk-java

[Bug Report] ClassCastException: class com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection cannot be cast to class com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection

Open
#1,853 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
210
Forks
238
Avg merge
4d 9h
Merged PRs (30d)
7

Description

# Context

- **OS and version used:** amazoncorretto:25-alpine-jdk (Dockerfile)
- **Java runtime used:** 25.0.2
- **SDK version used:** 2.5.1

## Description of the issue

We have some DeviceClients - using the IotHubClientProtocol.MQTT - that connects, sends and close their connection afterwards.

In case the IoTHub is not available at the moment that DeviceClients try to reconnect to it causing the following exception:

```
java.lang.ClassCastException: class com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection cannot be cast to class com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection and com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection are in unnamed module of loader org.springframework.boot.loader.launch.LaunchedClassLoader @3c27f72)
at com.microsoft.azure.sdk.iot.device.transport.IotHubTransport.singleDeviceReconnectAttemptAsync(IotHubTransport.java:1464)
at com.microsoft.azure.sdk.iot.device.transport.IotHubTransport.reconnect(IotHubTransport.java:649)
at com.microsoft.azure.sdk.iot.device.transport.IotHubReconnectTask.run(IotHubReconnectTask.java:94)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:545)
at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:369)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:310)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1090)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:614)
at java.base/java.lang.Thread.run(Thread.java:1474)
```

Since the IoTHub is seldom out of order we do not see that exception very often. But when it is there are a lot of devices that try to reconnect causing a lot of exceptions for a very short time range.

Contributor guide

Open the contributing guide

Research direction

Start in IotHubTransport.java at singleDeviceReconnectAttemptAsync (line 1464 in the reported stack trace), then trace how reconnect handles an MQTT DeviceClient when IoT Hub is unavailable. The report provides no test file; look for existing reconnect tests and run the relevant ones. Done means the reported MQTT reconnect scenario no longer throws this ClassCastException.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, java
Domain
backend, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.