awslabs / aws-crt-java

Java bindings for the AWS Common Runtime

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

withTimeoutMs will throw a null pointer exception if there is no SocketOptions previously set

MikeDombo opened this issue · comments

Describe the bug

public TlsConnectionOptions withTimeoutMs(int timeoutMs) {

Expected Behavior

withTimeoutMs (and similar APIs) should check for null and create default socket options if required.

Current Behavior

Throws a NPE

Reproduction Steps

N/A

Possible Solution

No response

Additional Information/Context

No response

aws-crt-java version used

N/A

Java version used

N/A

Operating System and version

N/A

Did you have a minimal code sample that reproduces this?

try (AwsIotMqttConnectionBuilder connectionBuilder = AwsIotMqttConnectionBuilder.newMtlsBuilder(
                        thing.getCertificate().getCertificatePem(), thing.getCertificate().getKeyPair().privateKey())
                .withClientId(thing.getThingName())
                .withPort(port)
                .withTimeoutMs(30_000)
                .withEndpoint(hostAddress)
                .withBootstrap(clientBootstrap)

you mean this function https://github.com/aws/aws-iot-device-sdk-java-v2/blob/main/sdk/src/main/java/software/amazon/awssdk/iot/AwsIotMqttConnectionBuilder.java#L362 instead, right?

It will be nice to have a default socket options if withTimeoutMs called before withSocketOptions, but what if user invoked withTimeoutMs and then withSocketOptions called afterward. It seems not like a good api to me.

From my point of view, it should be either:

  • Remove withTimeoutMs, you need to specific set the socket options to set TCP socket timeout
  • Or withSocketOptionsTImeoutMs, so it just gives you the default socket options with timeout.

It is a very simple fix:

if (this.config.getSocketOptions() == null) {
   this.withSocketOptions(new SocketOptions());
}
this.config.getSocketOptions().connectTimeoutMs = timeoutMs;