{"record":{"id":"a0616b026ff22b2a","repo":"apache/cordova-android","slug":"could-not-find-an-available-avd-port","errorCode":null,"errorMessage":"Could not find an available avd port","messagePattern":"Could not find an available avd port","errorType":"exception","errorClass":"CordovaError","httpStatus":null,"severity":"error","filePath":"lib/emulator.js","lineNumber":185,"sourceCode":"    return (await Adb.devices())\n        .filter(id => id.startsWith('emulator-'));\n};\n\n/*\n * Gets unused port for android emulator, between 5554 and 5584\n * Returns a promise.\n */\nmodule.exports.get_available_port = function () {\n    const self = this;\n\n    return self.list_started().then(function (emulators) {\n        for (let p = 5584; p >= 5554; p -= 2) {\n            if (emulators.indexOf('emulator-' + p) === -1) {\n                events.emit('verbose', 'Found available port: ' + p);\n                return p;\n            }\n        }\n        throw new CordovaError('Could not find an available avd port');\n    });\n};\n\n/*\n * Starts an emulator with the given ID,\n * and returns the started ID of that emulator.\n * If no boot timeout is given or the value is negative it will wait forever for\n * the emulator to boot\n *\n * Returns a promise.\n */\nmodule.exports.start = function (emulatorId, boot_timeout) {\n    const self = this;\n\n    return Promise.resolve().then(function () {\n        if (!emulatorId) {\n            throw new CordovaError('No emulator ID given');\n        }","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/apache/cordova-android/blob/7c1e190064e349ffa4bbc6ac37b77cd773e4dbd3/lib/emulator.js#L167-L203","documentation":"emulator.get_available_port() lists started emulators (from adb devices) and scans adb's console port range 5584 down to 5554 in steps of 2 (16 ports, the adb convention emulator-5554..emulator-5584). Throws when every port in the range is already occupied by a running emulator.","triggerScenarios":"emulator.start() -> get_available_port() when 16 or more emulators are already running (emulator-5554 through emulator-5584 all appear in `adb devices`). Typical of parallel CI farms or long-lived emulator processes that were never killed.","commonSituations":"CI running many concurrent Android jobs against one adb server; leaked detached emulator processes (the start() flow detaches with .unref()); local machines accumulating emulator instances over days.","solutions":["Free ports by stopping unneeded emulators: `adb -s emulator-5554 emu kill`, close emulator windows, or `pkill -f qemu-system`.","If you genuinely need more than 16 concurrent emulators on one host, split them across separate adb servers (ANDROID_ADB_SERVER_PORT) or machines - the port range is fixed.","Retry `cordova emulate android` once ports are free."],"exampleFix":"# before\n$ cordova emulate android\nCould not find an available avd port\n\n# after\n$ adb devices | grep emulator-   # identify stale emulators\n$ adb -s emulator-5554 emu kill\n$ cordova emulate android","handlingStrategy":"retry","validationCode":"const started = await emulator.list_started(); // e.g. ['emulator-5554', 'emulator-5556']\nif (started.filter(a => /^emulator-\\d+$/.test(a)).length >= 16) {\n  throw new Error('All 16 adb emulator ports in use - stop unneeded emulators before starting another');\n}","typeGuard":null,"tryCatchPattern":"try {\n  await emulator.start(avdId, timeout);\n} catch (e) {\n  if (/Could not find an available avd port/.test(e.message)) {\n    await execa('adb', ['-s', (await emulator.list_started())[0], 'emu', 'kill']);\n  return emulator.start(avdId, timeout); // retry once after freeing a port\n  }\n  throw e;\n}","preventionTips":["Tear emulators down at the end of every CI job (adb emu kill / pkill qemu-system) instead of leaking them.","Cap parallel emulator jobs per host at well under 16; shard across hosts or adb servers beyond that.","Monitor `adb devices` count in long-lived test farms."],"tags":["emulator","adb","port-exhaustion","parallel-ci"],"backgroundTag":"port-exhaustion","analyzedSha":"7c1e190064e349ffa4bbc6ac37b77cd773e4dbd3","analyzedAt":"2026-08-22T04:57:58.868Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}