{"record":{"id":"a02d2fcd20eba62d","repo":"decolua/9router","slug":"eaddrinuse","errorCode":"EADDRINUSE","errorMessage":"Port ${LOCAL_PORT} already in use","messagePattern":"Port (.+?) already in use","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/mitm/server.js","lineNumber":385,"sourceCode":"      }\n    });\n    log(`Killed ${pidList.length} process(es) on port ${port}`);\n  } catch (e) {\n    if (e.status !== 1) throw e;\n  }\n}\n\ntry {\n  killPort(LOCAL_PORT);\n} catch (e) {\n  err(`Cannot kill process on port ${LOCAL_PORT}: ${e.message}`);\n  process.exit(1);\n}\n\nserver.listen(LOCAL_PORT, () => log(`🚀 Server ready on :${LOCAL_PORT}`));\n\nserver.on(\"error\", (e) => {\n  if (e.code === \"EADDRINUSE\") err(`Port ${LOCAL_PORT} already in use`);\n  else if (e.code === \"EACCES\") err(`Permission denied for port ${LOCAL_PORT}`);\n  else err(e.message);\n  process.exit(1);\n});\n\nconst { removeAllDNSEntriesSync } = require(\"./dns/dnsConfig\");\nlet isShuttingDown = false;\nconst shutdown = () => {\n  if (isShuttingDown) return;\n  isShuttingDown = true;\n  // Strip tool hosts from /etc/hosts so other apps aren't broken after exit\n  removeAllDNSEntriesSync();\n  const forceExit = setTimeout(() => process.exit(0), 1500);\n  server.close(() => { clearTimeout(forceExit); process.exit(0); });\n};\nprocess.on(\"SIGTERM\", shutdown);\nprocess.on(\"SIGINT\", shutdown);\nif (process.platform === \"win32\") process.on(\"SIGBREAK\", shutdown);","sourceCodeStart":367,"sourceCodeEnd":403,"githubUrl":"https://github.com/decolua/9router/blob/90b52e06ffd666b7929554211474d01588f6b1f8/src/mitm/server.js#L367-L403","documentation":"The MITM proxy server fails to bind its local listening port because another socket already holds it (errno EADDRINUSE). The server logs 'Port <LOCAL_PORT> already in use' and exits with code 1.","triggerScenarios":"server.listen(LOCAL_PORT) while another 9Router MITM instance, the gateway itself, or any unrelated process is already bound to the same local port.","commonSituations":"Two MITM instances launched (e.g. tray + manual); previous instance not fully shut down (lingering listener, the code even kills LISTENING processes on LOCAL_PORT for this reason); LOCAL_PORT collides with the gateway port 20128 or another dev server.","solutions":["Find and stop the process holding the port: lsof -i :<LOCAL_PORT> (or netstat) and kill the stale instance.","Restart the tray/launcher which automatically kills processes LISTENING on LOCAL_PORT before starting.","Change LOCAL_PORT (env/config) to a free port if the collision is with another service.","Wait a moment and retry — TIME_WAIT sockets usually clear within a minute."],"exampleFix":"// before\nPORT=20128 (same as gateway) → EADDRINUSE\n// after\nMITM_LOCAL_PORT=20129 npm run start  # distinct from gateway port","handlingStrategy":"validation","validationCode":"const net = require('net');\nconst port = Number(process.env.MITM_LOCAL_PORT || LOCAL_PORT);\nconst probe = net.createServer().once('error', (e) => {\n  if (e.code === 'EADDRINUSE') console.error(`Port ${port} in use — pick another or stop the other instance`);\n});\nprobe.listen(port, () => probe.close(() => startServer(port)));","typeGuard":null,"tryCatchPattern":"server.on('error', (e) => {\n  if (e.code === 'EADDRINUSE') {\n    err(`Port ${LOCAL_PORT} already in use`);\n    // optionally retry once after killing stale listeners on LOCAL_PORT\n    process.exit(1);\n  }\n});","preventionTips":["Run only one MITM instance — use the tray/launcher rather than manual starts.","Choose a LOCAL_PORT distinct from the gateway port (20128).","Probe port availability with a temporary net.createServer before binding.","Ensure previous instances are killed on shutdown (the server already kills LISTENERS on LOCAL_PORT at startup)."],"tags":["eaddrinuse","port-conflict","mitm","startup"],"backgroundTag":"port-already-in-use","analyzedSha":"90b52e06ffd666b7929554211474d01588f6b1f8","analyzedAt":"2026-08-30T21:05:45.952Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}