Skip to content

[BUG] Infinite loop when iterating over ResourceSet<IncomingPhoneNumber>.iterator() and using next() method #914

@solaechea

Description

@solaechea

Describe the bug

After upgrading to 11.0.2 (from 10.9.2) we noticed an Infinite loop when iterating over ResourceSet.iterator() and using next() and hasNext() methods

Here is a project that reproduces the issue
https://github.com/solaechea/twilio_11_iterator_issue

Code snippet

package com.example;

import com.twilio.base.ResourceSet;
import com.twilio.http.TwilioRestClient;
import com.twilio.rest.api.v2010.account.IncomingPhoneNumber;

/**
 * Simple program that lists Incoming Phone Numbers for the configured Twilio account.
 *
 * It expects the environment variables TWILIO_ACCOUNT_SID, TWILIO_KEY_SID, and TWILIO_KEY_SECRET to be set.
 */
public class TwilioListNumbers {
    public static void main(String[] args) {
        String TWILIO_KEY_SID = System.getenv("TWILIO_KEY_SID");
        String TWILIO_KEY_SECRET = System.getenv("TWILIO_KEY_SECRET");
        String TWILIO_ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");

        if (TWILIO_KEY_SID == null || TWILIO_KEY_SECRET == null || TWILIO_ACCOUNT_SID == null) {
            System.out.println("Hello, World.\n\nTo list Twilio phone numbers, set environment variables TWILIO_ACCOUNT_SID, TWILIO_KEY_SID and TWILIO_KEY_SECRET and run again.");
            System.out.println("Example (PowerShell): $env:TWILIO_ACCOUNT_SID='AC...'; $env:TWILIO_KEY_SID=''; $env:TWILIO_KEY_SECRET=''; mvn compile exec:java");
            return;
        }

        System.out.println("Hello, Twilio! Building client and listing incoming phone numbers for account " + TWILIO_ACCOUNT_SID);

        TwilioRestClient client = new TwilioRestClient.Builder(TWILIO_KEY_SID, TWILIO_KEY_SECRET)
            .accountSid(TWILIO_ACCOUNT_SID)
            .build();

        ResourceSet<IncomingPhoneNumber> numbers = IncomingPhoneNumber.reader().read(client);

        // This will create an infinite loop when using Twilio Java SDK version 11.0.2
        while (numbers.iterator().hasNext()) {
            IncomingPhoneNumber number = numbers.iterator().next();
            System.out.printf("%s  —  %s (sid: %s)\n", number.getPhoneNumber(), number.getFriendlyName(), number.getSid());
        }
    }
}

Actual behavior

There is an infinite loop, looks like calling the next() method is not advancing the iterator to the next element in the collection.

Expected behavior

Calling the next() method on a java.util.Iterator should advance the iterator to the next element in the collection.

twilio-java version

11.0.2

Java version

17.0.9+11-LTS-201

Logs or error messages

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugbug in the library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions