lionsoul2014/ip2region · error · Exception

xdb file IP version mismatch: expected ${ipVersion.name}, go

Error message

xdb file IP version mismatch: expected ${ipVersion.name}, got ${detectedVersion.name}

What it means

During Config initialization the xdb file's header is parsed and its detected IP version is compared with the ipVersion the caller requested. If they differ, the exception names both versions, because a searcher built for one address family cannot correctly query a database of the other.

Source

Thrown at binding/cangjie/src/service/config.cj:37

    public let searchers: Int64

    public init(cachePolicy: Int64, ipVersion: Version, xdbPath: String, searchers: Int64) {
        if (searchers <= 0) {
            throw Exception("searchers must be > 0")
        }

        this.cachePolicy = cachePolicy
        this.ipVersion = ipVersion
        this.xdbPath = xdbPath
        this.searchers = searchers

        let content = File.readFrom(Path(xdbPath))
        this.header = newHeaderFromBytes(content)

        // Verify IP version matches
        let detectedVersion = versionFromHeader(this.header)
        if (detectedVersion.id != ipVersion.id) {
            throw Exception("xdb file IP version mismatch: expected ${ipVersion.name}, got ${detectedVersion.name}")
        }

        if (cachePolicy == VectorIndex) {
            this.vIndex = loadVectorIndex(content)
            this.cBuffer = Array<Byte>(0, repeat: 0)
        } else if (cachePolicy == ContentBuff) {
            this.vIndex = Array<Byte>(0, repeat: 0)
            this.cBuffer = content
        } else {
            this.vIndex = Array<Byte>(0, repeat: 0)
            this.cBuffer = Array<Byte>(0, repeat: 0)
        }
    }

    public init(cachePolicy: Int64, ipVersion: Version, xdbPath: String) {
        this(cachePolicy, ipVersion, xdbPath, 20)
    }
}

View on GitHub (pinned to c1a1fc7d59)

Solutions

  1. Make ipVersion match the actual file (check the message's 'got' side to see what the file is)
  2. Download/load the correct .xdb variant for the configured version (v4 file for V4, v6 file for V6)
  3. Detect the version from the header first (versionFromHeader) and pass that into Config instead of hardcoding
  4. Separate config keys for v4 and v6 database paths if both are needed

Example fix

// before
let cfg = Config(policy, Version.V6, "ip.v4.xdb", 4) // mismatch
// after
let header = newHeaderFromBytes(File.readFrom(Path(xdbPath)))
let ver = versionFromHeader(header)
let cfg = Config(policy, ver, xdbPath, 4)
Defensive patterns

Strategy: validation

Validate before calling

// Cangjie: detect version from file before constructing Config
let content = File.readFrom(Path(xdbPath))
let ver = versionFromHeader(newHeaderFromBytes(content))
if (ver.id != desiredVersion.id) { log.error("xdb is ${ver.name}, config wants ${desiredVersion.name}") }

Type guard

func versionMatches(path: String, want: Version): Bool {
    let h = newHeaderFromBytes(File.readFrom(Path(path)))
    return versionFromHeader(h).id == want.id
}

Try / catch

try {
    let cfg = Config(policy, ipVersion, xdbPath, poolSize)
} catch (e: Exception) {
    if (e.message.contains("version mismatch")) { /* reload correct xdb or fix ipVersion */ }
    throw e
}

Prevention

When it happens

Trigger: Constructing Config with ipVersion = V6 while pointing xdbPath at an IPv4-only .xdb (or vice versa); reusing a config/path constant after switching database files.

Common situations: Deploying the v6 database but leaving the v4 path in the config, upgrading from an IPv4 xdb to an IPv6 one without updating ipVersion, mixing files from different downloads.

Related errors


AI-assisted analysis of lionsoul2014/ip2region@c1a1fc7d59 (2026-09-02). Data as JSON: /api/errors/dea2e0fa566dccf4. Report an issue: GitHub.