apache/flink · error · UnsupportedOperationException

Remove not supported

Error message

Remove not supported

What it means

The anonymous Iterator<Integer> that NetUtils.getPortRangeFromString creates for a 'start-end' range implements remove() as UnsupportedOperationException. The port-range iterator is read-only by design. Flink code itself only iterates; this surfaces when user code holds the iterator and calls remove().

Source

Thrown at flink-core/src/main/java/org/apache/flink/util/NetUtils.java:474

                                    + range);
                }
                rangeIterator =
                        new Iterator<Integer>() {
                            int i = start;

                            @Override
                            public boolean hasNext() {
                                return i <= end;
                            }

                            @Override
                            public Integer next() {
                                return i++;
                            }

                            @Override
                            public void remove() {
                                throw new UnsupportedOperationException("Remove not supported");
                            }
                        };
            }
            iterators.add(rangeIterator);
        }

        return iterators;
    }

    /**
     * Tries to allocate a socket from the given sets of ports.
     *
     * @param portsIterator A set of ports to choose from.
     * @param factory A factory for creating the SocketServer
     * @return null if no port was available or an allocated socket.
     */
    public static ServerSocket createSocketFromPorts(
            Iterator<Integer> portsIterator, SocketFactory factory) {

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Do not mutate the port iterator; collect ports into a list and filter that instead.
  2. If you need to skip ports, maintain your own Set of used ports and check membership while iterating.

Example fix

// before
Iterator<Integer> it = new PortRange(range).getPortsIterator();
it.next();
it.remove(); // throws

// after
List<Integer> ports = new ArrayList<>();
new PortRange(range).getPortsIterator().forEachRemaining(ports::add);
ports.removeIf(p -> p == skipPort);
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Calling .remove() on the Iterator obtained from NetUtils.getPortRangeFromString or PortRange.getPortsIterator() while trying to prune candidate ports.

Common situations: Generic iterator-manipulation utility applied to the port iterator; port-selection code adapted from a mutable-list implementation.

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/89fc6a46aeb82a2e. Report an issue: GitHub.