{"record":{"id":"94770c404766973a","repo":"XTLS/Xray-core","slug":"no-available-tun-interface-name-in-range-s-d-s-d","errorCode":null,"errorMessage":"no available TUN interface name in range %s%d-%s%d","messagePattern":"no available TUN interface name in range (.+?)(.+?)-(.+?)(.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"infra/conf/tun.go","lineNumber":91,"sourceCode":"\t}\n\n\tstartIndex, err := randomInt(minTunIndex, maxTunIndex)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"fail to generate valid tun name: %w\", err)\n\t}\n\n\trangeSize := maxTunIndex - minTunIndex + 1\n\n\tfor offset := 0; offset < rangeSize; offset++ {\n\t\tindex := minTunIndex + (startIndex-minTunIndex+offset)%rangeSize\n\t\tname := tunNamePrefix + strconv.Itoa(index)\n\n\t\tif _, exists := usedNames[name]; !exists {\n\t\t\treturn name, nil\n\t\t}\n\t}\n\n\treturn \"\", fmt.Errorf(\n\t\t\"no available TUN interface name in range %s%d-%s%d\",\n\t\ttunNamePrefix,\n\t\tminTunIndex,\n\t\ttunNamePrefix,\n\t\tmaxTunIndex,\n\t)\n}\n\nfunc randomInt(min, max int) (int, error) {\n\tvalue, err := rand.Int(\n\t\trand.Reader,\n\t\tbig.NewInt(int64(max-min+1)),\n\t)\n\tif err != nil {\n\t\treturn 0, err\n\t}\n\n\treturn min + int(value.Int64()), nil","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/XTLS/Xray-core/blob/7d214f8b094f75322fa3990f8aadad1c912f24f5/infra/conf/tun.go#L73-L109","documentation":"GetAvailableTunName scans utun10..utun1024 starting from a random offset and fails when every name in that range is already taken by an existing interface. It indicates interface-name exhaustion rather than a permission or syscall failure.","triggerScenarios":"A system (typically macOS or a TUN-using platform with the utun naming convention) where 1015 interfaces named utun10..utun1024 all exist — usually leaked TUN handles from crashed VPN/proxy processes.","commonSituations":"Repeated crashes/restarts of xray or other VPN tools leaking utun devices; macOS systems with many stale utun interfaces from iCloud/VPN services plus leaked ones.","solutions":["List interfaces (`ifconfig -a` / `ip link`) and delete stale utun* devices or reboot to clear them","Kill orphaned processes still holding TUN fds (lsof | grep utun)","If legitimate usage needs >1015 interfaces, that exceeds the design range — reconsider the setup","After cleanup, retry starting xray"],"exampleFix":"# before: 1000+ leaked utuns\nsudo ifconfig utun900 destroy   # macOS, repeat for leaked ids\n# or simply reboot; then restart xray","handlingStrategy":"try-catch","validationCode":"// pre-flight: count free names before starting\nifaces, _ := net.Interfaces()\nused := map[string]bool{}\nfor _, i := range ifaces { used[i.Name] = true }\nfree := 0\nfor n := 10; n <= 1024; n++ { if !used[fmt.Sprintf(\"utun%d\", n)] { free++ } }\nif free == 0 { return errors.New(\"utun range exhausted; clean stale interfaces\") }","typeGuard":null,"tryCatchPattern":"if _, err := conf.GetAvailableTunName(); err != nil {\n    if strings.Contains(err.Error(), \"no available TUN interface name\") {\n        // ops remediation: destroy leaked utuns or reboot\n        log.Fatal(\"TUN names exhausted — remove leaked utun* interfaces and restart\")\n    }\n    log.Fatal(err)\n}","preventionTips":["Periodically clean up leaked TUN devices from crashed VPN/proxy processes","Monitor interface count on long-lived macOS hosts"],"tags":["tun","resource-exhaustion","os","macos"],"backgroundTag":null,"analyzedSha":"7d214f8b094f75322fa3990f8aadad1c912f24f5","analyzedAt":"2026-08-15T14:26:24.325Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}