Skip to content

IndexOutOfBoundsException when calling ConnectionFactory.newConnection() #1081

Closed
@Logic-32

Description

@Logic-32

Describe the bug

In v5.16.0 a behavior change was introduced in ConnectionFactory.createAddressResolver().

Note the different if-conditions in the body. In the case where addresses.size() == 0 (isEmpty()) then:

  • v5.15.0 will create a ListAddressResolver
  • v5.16.0 will try to pull the first item from addresses and cause an IndexOutOfBoundsException

Example stack trace while using v5.18.0, where the bug is still present:

Index 0 out of bounds for length 0
java.lang.IndexOutOfBoundsException
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
	at java.base/java.util.Objects.checkIndex(Objects.java:373)
	at java.base/java.util.ArrayList.get(ArrayList.java:426)
	at com.rabbitmq.client.ConnectionFactory.createAddressResolver(ConnectionFactory.java:1361)
	at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1184)
	at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1061)
...

Reproduction steps

  1. Create a ConnectionFactory with an empty List of addresses (e.g. use List.of())
  2. Call newConnection()
  3. Fail.

Expected behavior

While I realize it is absurd to try and connect to RMQ with no addresses provided we had unit tests that did exactly that for some reason. Interestingly, none of the usages of AddressResolver actually seem to care if an address is returned or not. So, the tests ran fine.

With that said, my expectations here would be either:

  1. An isEmpty() (or equivalent) check is added and ListAddressResolver continues to be used since it seems to work fine, or
  2. A check is still added and a useful Exception is thrown indicating that this is an invalid state

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions