iflytek/astron-agent · error · ValueError

Bad IP !!

Error message

Bad IP !! 

What it means

The SID generator's __init__ derives a short hex representation from the last two octets of the local IP. If the IP string does not split into the expected 4 dotted segments (ip[0..3]), a ValueError 'Bad IP !! <ip>' is raised. A related guard raises 'Bad Port!!' if the port is missing or shorter than 4 chars.

Solutions

  1. Pass a valid dotted-quad IPv4 address (e.g. '192.168.1.10'), not a hostname or IPv6
  2. Resolve the hostname to an IPv4 via socket.gethostbyname before constructing the generator
  3. Verify the IP source (env var/config) contains the expected format; log the offending value
  4. Also ensure local_port is provided and at least 4 characters to avoid the adjacent 'Bad Port!!' error

Example fix

// before
sid = SidGenerator(local_ip=socket.gethostname(), local_port=port)
// after
ip = socket.gethostbyname(socket.gethostname())
assert len(ip.split(".")) == 4, ip
sid = SidGenerator(local_ip=ip, local_port=port)
Defensive patterns

Strategy: validation

Validate before calling

def is_ipv4(ip: str) -> bool:
    parts = ip.split(".")
    return len(parts) == 4 and all(p.isdigit() and 0 <= int(p) <= 255 for p in parts)

Type guard

def is_valid_sid_ip(ip: object) -> bool:
    return isinstance(ip, str) and is_ipv4(ip)

Try / catch

try:
    gen = SidGenerator(local_ip=ip, local_port=port)
except ValueError as e:
    logger.error("invalid IP/port for SID: %s", e)
    raise

Prevention

When it happens

Trigger: Constructing the SID generator with a local_ip that is not a valid dotted-quad IPv4 (e.g. 'localhost', '127.0.0.1 extra', an IPv6 address like '::1', empty string from a failed lookup).

Common situations: get_host_ip returning a hostname or IPv6 address on odd network setups; env var holding a bad IP; passing 'localhost' instead of a numeric IP; misconfigured multi-NIC hosts returning an unexpected format.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of iflytek/astron-agent@5e758547a8 (2026-09-12). Data as JSON: /api/errors/4de0acbdee02aca9. Report an issue: GitHub.

Appendix: source

Thrown at core/plugin/link/utils/sid/sid_generator2.py:111

    sid2 = 2

    def __init__(
        self,
        sub: Optional[str],
        location: Optional[str],
        local_ip: str,
        local_port: Optional[str],
    ) -> None:
        self.index = 0
        ip = socket.inet_aton(local_ip)
        if ip:
            ip_sec3 = ip[2]
            ip_sec4 = ip[3]
            ip3 = ip_sec3 & 0xFF
            ip4 = ip_sec4 & 0xFF
            self.short_local_ip = f"{ip3:02x}{ip4:02x}"
        else:
            raise ValueError("Bad IP !! " + local_ip)
        if local_port is None or len(local_port) < 4:
            raise ValueError("Bad Port!! ")
        self.port = local_port
        self.location = location
        self.sub = sub
        print("sid init success")

    def gen(self) -> str:
        """Generate a unique service ID.

        Returns:
            str: Unique service ID in format:
            {sub}{pid}{index}@{location}{timestamp}{ip}{port}{version}
        """
        if self.sub is None or len(self.sub) == 0:
            self.sub = "src"
        pid = os.getpid() & 0xFF
        self.index = (self.index + 1) & 0xFFFF

View on GitHub (pinned to 5e758547a8)