gkopff/mina-ftpserver

FTPSERVER-424 Leak of allowed passive ports

Closed this issue · 1 comments

From: https://issues.apache.org/jira/browse/FTPSERVER-424

In several hours after start the server cannot accept passive connections if allowed passive ports are configured. This is caused by FTPSERVER-420, org.apache.ftpserver.impl.PassivePorts.reserveNextPort() lines 218-219, where the value removed from the freeList might be different from the value added to the usedList, if checkPortUnbound returns false before.

    public synchronized int reserveNextPort() {
        // create a copy of the free ports, so that we can keep track of the tested ports
        List<Integer> freeCopy = new ArrayList<Integer>(freeList);

        // Loop until we have found a port, or exhausted all available ports
        while (freeCopy.size() > 0) {
            // Otherwise, pick one at random
            int i = r.nextInt(freeCopy.size());
            Integer ret = freeCopy.get(i);

            if (ret == 0) {
                // "Any" port should not be removed from our free list,
                // nor added to the used list
                return 0;

            } else if (checkPortUnbound(ret)) {
                // Not used by someone else, so lets reserve it and return it
                freeList.remove(ret);
                usedList.add(ret);
                return ret;

            } else {
                freeCopy.remove(ret);
                // log port unavailable, but left in pool
                log.warn("Passive port in use by another process: " + ret);
            }
        }

        return -1;
    }