# Buggy code
def ip_fqdn():
    """
    Return ip address and FQDN grains
    """
    if salt.utils.platform.is_proxy():
        return {}

    ret = {}
    ret["ipv4"] = salt.utils.network.ip_addrs(include_loopback=True)
    ret["ipv6"] = salt.utils.network.ip_addrs6(include_loopback=True)

    _fqdn = hostname()["fqdn"]
    for socket_type, ipv_num in ((socket.AF_INET, "4"), (socket.AF_INET6, "6")):
        key = "fqdn_ip" + ipv_num
        if not ret["ipv" + ipv_num]:
            ret[key] = []
        else:
            try:
                start_time = datetime.datetime.utcnow()
                info = socket.getaddrinfo(_fqdn, None, socket_type)
                ret[key] = list(set(item[4][0] for item in info))
            except socket.error:
                timediff = datetime.datetime.utcnow() - start_time
                if timediff.seconds > 5 and __opts__["__role"] == "master":
                    log.warning(
                        'Unable to find IPv%s record for "%s" causing a %s '
                        "second timeout when rendering grains. Set the dns or "
                        "/etc/hosts for IPv%s to clear this.",
                        ipv_num,
                        _fqdn,
                        timediff,
                        ipv_num,
                    )
                ret[key] = []

    return ret


# Traceback (UnicodeError)
~# salt-master -l debug

....

[ERROR   ] An un-handled exception from the multiprocessing process 'MWorker-1' was caught:
Traceback (most recent call last):
File "/usr/lib/python3.6/encodings/idna.py", line 165, in encode
raise UnicodeError("label empty or too long")
UnicodeError: label empty or too long

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/salt/utils/process.py", line 760, in wrapped_run_func
return run_func()
File "/usr/lib/python3/dist-packages/salt/master.py", line 1139, in run
self.key,
File "/usr/lib/python3/dist-packages/salt/master.py", line 1891, in __init__
ignore_config_errors=True
File "/usr/lib/python3/dist-packages/salt/minion.py", line 898, in __init__
self.opts['grains'] = salt.loader.grains(opts)
File "/usr/lib/python3/dist-packages/salt/loader.py", line 808, in grains
ret = funcs[key]()
File "/usr/lib/python3/dist-packages/salt/grains/core.py", line 2279, in ip_fqdn
info = socket.getaddrinfo(_fqdn, None, socket_type)
File "/usr/lib/python3.6/socket.py", line 745, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
UnicodeError: encoding with 'idna' codec failed (UnicodeError: label empty or too long)
Process MWorker-1:
Traceback (most recent call last):
File "/usr/lib/python3.6/encodings/idna.py", line 165, in encode
raise UnicodeError("label empty or too long")
UnicodeError: label empty or too long

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/lib/python3/dist-packages/salt/utils/process.py", line 771, in wrapped_run_func
six.reraise(*sys.exc_info())
File "/usr/lib/python3/dist-packages/salt/ext/six.py", line 693, in reraise
raise value
File "/usr/lib/python3/dist-packages/salt/utils/process.py", line 760, in wrapped_run_func
return run_func()
File "/usr/lib/python3/dist-packages/salt/master.py", line 1139, in run
self.key,
File "/usr/lib/python3/dist-packages/salt/master.py", line 1891, in __init__
ignore_config_errors=True
File "/usr/lib/python3/dist-packages/salt/minion.py", line 898, in __init__
self.opts['grains'] = salt.loader.grains(opts)
File "/usr/lib/python3/dist-packages/salt/loader.py", line 808, in grains
ret = funcs[key]()
File "/usr/lib/python3/dist-packages/salt/grains/core.py", line 2279, in ip_fqdn
info = socket.getaddrinfo(_fqdn, None, socket_type)
File "/usr/lib/python3.6/socket.py", line 745, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
UnicodeError: encoding with 'idna' codec failed (UnicodeError: label empty or too long)
[ERROR   ] An un-handled exception from the multiprocessing process 'MWorker-0' was caught:
Traceback (most recent call last):
File "/usr/lib/python3.6/encodings/idna.py", line 165, in encode
raise UnicodeError("label empty or too long")
UnicodeError: label empty or too long

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/salt/utils/process.py", line 760, in wrapped_run_func
return run_func()
File "/usr/lib/python3/dist-packages/salt/master.py", line 1139, in run
self.key,
File "/usr/lib/python3/dist-packages/salt/master.py", line 1891, in __init__
ignore_config_errors=True
File "/usr/lib/python3/dist-packages/salt/minion.py", line 898, in __init__
self.opts['grains'] = salt.loader.grains(opts)
File "/usr/lib/python3/dist-packages/salt/loader.py", line 808, in grains
ret = funcs[key]()
File "/usr/lib/python3/dist-packages/salt/grains/core.py", line 2279, in ip_fqdn
info = socket.getaddrinfo(_fqdn, None, socket_type)
File "/usr/lib/python3.6/socket.py", line 745, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
UnicodeError: encoding with 'idna' codec failed (UnicodeError: label empty or too long)
Process MWorker-0:
Traceback (most recent call last):
File "/usr/lib/python3.6/encodings/idna.py", line 165, in encode
raise UnicodeError("label empty or too long")
UnicodeError: label empty or too long

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/lib/python3/dist-packages/salt/utils/process.py", line 771, in wrapped_run_func
six.reraise(*sys.exc_info())
File "/usr/lib/python3/dist-packages/salt/ext/six.py", line 693, in reraise
raise value
File "/usr/lib/python3/dist-packages/salt/utils/process.py", line 760, in wrapped_run_func
return run_func()
File "/usr/lib/python3/dist-packages/salt/master.py", line 1139, in run
self.key,
File "/usr/lib/python3/dist-packages/salt/master.py", line 1891, in __init__
ignore_config_errors=True
File "/usr/lib/python3/dist-packages/salt/minion.py", line 898, in __init__
self.opts['grains'] = salt.loader.grains(opts)
File "/usr/lib/python3/dist-packages/salt/loader.py", line 808, in grains
ret = funcs[key]()
File "/usr/lib/python3/dist-packages/salt/grains/core.py", line 2279, in ip_fqdn
info = socket.getaddrinfo(_fqdn, None, socket_type)
File "/usr/lib/python3.6/socket.py", line 745, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
UnicodeError: encoding with 'idna' codec failed (UnicodeError: label empty or too long)
[ERROR   ] An un-handled exception from the multiprocessing process 'MWorker-3' was caught:
Traceback (most recent call last):
File "/usr/lib/python3.6/encodings/idna.py", line 165, in encode
raise UnicodeError("label empty or too long")
UnicodeError: label empty or too long

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/salt/utils/process.py", line 760, in wrapped_run_func
return run_func()
File "/usr/lib/python3/dist-packages/salt/master.py", line 1139, in run
self.key,
File "/usr/lib/python3/dist-packages/salt/master.py", line 1891, in __init__
ignore_config_errors=True
File "/usr/lib/python3/dist-packages/salt/minion.py", line 898, in __init__
self.opts['grains'] = salt.loader.grains(opts)
File "/usr/lib/python3/dist-packages/salt/loader.py", line 808, in grains
ret = funcs[key]()
File "/usr/lib/python3/dist-packages/salt/grains/core.py", line 2279, in ip_fqdn
info = socket.getaddrinfo(_fqdn, None, socket_type)
File "/usr/lib/python3.6/socket.py", line 745, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
UnicodeError: encoding with 'idna' codec failed (UnicodeError: label empty or too long)
Process MWorker-3:
Traceback (most recent call last):
File "/usr/lib/python3.6/encodings/idna.py", line 165, in encode
raise UnicodeError("label empty or too long")
UnicodeError: label empty or too long

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/lib/python3/dist-packages/salt/utils/process.py", line 771, in wrapped_run_func
six.reraise(*sys.exc_info())
File "/usr/lib/python3/dist-packages/salt/ext/six.py", line 693, in reraise
raise value
File "/usr/lib/python3/dist-packages/salt/utils/process.py", line 760, in wrapped_run_func
return run_func()
File "/usr/lib/python3/dist-packages/salt/master.py", line 1139, in run
self.key,
File "/usr/lib/python3/dist-packages/salt/master.py", line 1891, in __init__
ignore_config_errors=True
File "/usr/lib/python3/dist-packages/salt/minion.py", line 898, in __init__
self.opts['grains'] = salt.loader.grains(opts)
File "/usr/lib/python3/dist-packages/salt/loader.py", line 808, in grains
ret = funcs[key]()
File "/usr/lib/python3/dist-packages/salt/grains/core.py", line 2279, in ip_fqdn
info = socket.getaddrinfo(_fqdn, None, socket_type)
File "/usr/lib/python3.6/socket.py", line 745, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
UnicodeError: encoding with 'idna' codec failed (UnicodeError: label empty or too long)
[ERROR   ] An un-handled exception from the multiprocessing process 'MWorker-2' was caught:
Traceback (most recent call last):
File "/usr/lib/python3.6/encodings/idna.py", line 165, in encode
raise UnicodeError("label empty or too long")
UnicodeError: label empty or too long

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/salt/utils/process.py", line 760, in wrapped_run_func
return run_func()
File "/usr/lib/python3/dist-packages/salt/master.py", line 1139, in run
self.key,
File "/usr/lib/python3/dist-packages/salt/master.py", line 1891, in __init__
ignore_config_errors=True
File "/usr/lib/python3/dist-packages/salt/minion.py", line 898, in __init__
self.opts['grains'] = salt.loader.grains(opts)
File "/usr/lib/python3/dist-packages/salt/loader.py", line 808, in grains
ret = funcs[key]()
File "/usr/lib/python3/dist-packages/salt/grains/core.py", line 2279, in ip_fqdn
info = socket.getaddrinfo(_fqdn, None, socket_type)
File "/usr/lib/python3.6/socket.py", line 745, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
UnicodeError: encoding with 'idna' codec failed (UnicodeError: label empty or too long)
Process MWorker-2:
Traceback (most recent call last):
File "/usr/lib/python3.6/encodings/idna.py", line 165, in encode
raise UnicodeError("label empty or too long")
UnicodeError: label empty or too long

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/lib/python3/dist-packages/salt/utils/process.py", line 771, in wrapped_run_func
six.reraise(*sys.exc_info())
File "/usr/lib/python3/dist-packages/salt/ext/six.py", line 693, in reraise
raise value
File "/usr/lib/python3/dist-packages/salt/utils/process.py", line 760, in wrapped_run_func
return run_func()
File "/usr/lib/python3/dist-packages/salt/master.py", line 1139, in run
self.key,
File "/usr/lib/python3/dist-packages/salt/master.py", line 1891, in __init__
ignore_config_errors=True
File "/usr/lib/python3/dist-packages/salt/minion.py", line 898, in __init__
self.opts['grains'] = salt.loader.grains(opts)
File "/usr/lib/python3/dist-packages/salt/loader.py", line 808, in grains
ret = funcs[key]()
File "/usr/lib/python3/dist-packages/salt/grains/core.py", line 2279, in ip_fqdn
info = socket.getaddrinfo(_fqdn, None, socket_type)
File "/usr/lib/python3.6/socket.py", line 745, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
UnicodeError: encoding with 'idna' codec failed (UnicodeError: label empty or too long)
[ERROR   ] An un-handled exception from the multiprocessing process 'MWorker-4' was caught:
Traceback (most recent call last):
File "/usr/lib/python3.6/encodings/idna.py", line 165, in encode
raise UnicodeError("label empty or too long")
UnicodeError: label empty or too long

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/salt/utils/process.py", line 760, in wrapped_run_func
return run_func()
File "/usr/lib/python3/dist-packages/salt/master.py", line 1139, in run
self.key,
File "/usr/lib/python3/dist-packages/salt/master.py", line 1891, in __init__
ignore_config_errors=True
File "/usr/lib/python3/dist-packages/salt/minion.py", line 898, in __init__
self.opts['grains'] = salt.loader.grains(opts)
File "/usr/lib/python3/dist-packages/salt/loader.py", line 808, in grains
ret = funcs[key]()
File "/usr/lib/python3/dist-packages/salt/grains/core.py", line 2279, in ip_fqdn
info = socket.getaddrinfo(_fqdn, None, socket_type)
File "/usr/lib/python3.6/socket.py", line 745, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
UnicodeError: encoding with 'idna' codec failed (UnicodeError: label empty or too long)
Process MWorker-4:
Traceback (most recent call last):
File "/usr/lib/python3.6/encodings/idna.py", line 165, in encode
raise UnicodeError("label empty or too long")
UnicodeError: label empty or too long

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/lib/python3/dist-packages/salt/utils/process.py", line 771, in wrapped_run_func
six.reraise(*sys.exc_info())
File "/usr/lib/python3/dist-packages/salt/ext/six.py", line 693, in reraise
raise value
File "/usr/lib/python3/dist-packages/salt/utils/process.py", line 760, in wrapped_run_func
return run_func()
File "/usr/lib/python3/dist-packages/salt/master.py", line 1139, in run
self.key,
File "/usr/lib/python3/dist-packages/salt/master.py", line 1891, in __init__
ignore_config_errors=True
File "/usr/lib/python3/dist-packages/salt/minion.py", line 898, in __init__
self.opts['grains'] = salt.loader.grains(opts)
File "/usr/lib/python3/dist-packages/salt/loader.py", line 808, in grains
ret = funcs[key]()
File "/usr/lib/python3/dist-packages/salt/grains/core.py", line 2279, in ip_fqdn
info = socket.getaddrinfo(_fqdn, None, socket_type)
File "/usr/lib/python3.6/socket.py", line 745, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
UnicodeError: encoding with 'idna' codec failed (UnicodeError: label empty or too long)
[INFO    ] Process <class 'salt.master.MWorker'> (9022) died with exit status 1, restarting...
[DEBUG   ] Started 'salt.master.<class 'type'>.MWorker' with pid 9106
[INFO    ] Process <class 'salt.master.MWorker'> (9023) died with exit status 1, restarting...
[DEBUG   ] MasterEvent PUB socket URI: /var/run/salt/master/master_event_pub.ipc
[DEBUG   ] MasterEvent PULL socket URI: /var/run/salt/master/master_event_pull.ipc
[DEBUG   ] Started 'salt.master.<class 'type'>.MWorker' with pid 9107
[INFO    ] Process <class 'salt.master.MWorker'> (9024) died with exit status 1, restarting...


~# salt '*' test.ping
[ERROR   ] Message timed out
Salt request timed out. The master is not responding. You may need to run your command with `--async` in order to bypass the congested event bus. With `--async`, the CLI tool will print the job id (jid) and exit immediately without listening for responses. You can then use `salt-run jobs.lookup_jid` to look up the results of the job in the job cache later.

# Fixed code
def ip_fqdn():
    """
    Return ip address and FQDN grains
    """
    if salt.utils.platform.is_proxy():
        return {}

    ret = {}
    ret["ipv4"] = salt.utils.network.ip_addrs(include_loopback=True)
    ret["ipv6"] = salt.utils.network.ip_addrs6(include_loopback=True)

    _fqdn = hostname()["fqdn"]
    for socket_type, ipv_num in ((socket.AF_INET, "4"), (socket.AF_INET6, "6")):
        key = "fqdn_ip" + ipv_num
        if not ret["ipv" + ipv_num]:
            ret[key] = []
        else:
            try:
                start_time = datetime.datetime.utcnow()
                info = socket.getaddrinfo(_fqdn, None, socket_type)
                ret[key] = list(set(item[4][0] for item in info))
            except (socket.error, UnicodeError):
                timediff = datetime.datetime.utcnow() - start_time
                if timediff.seconds > 5 and __opts__["__role"] == "master":
                    log.warning(
                        'Unable to find IPv%s record for "%s" causing a %s '
                        "second timeout when rendering grains. Set the dns or "
                        "/etc/hosts for IPv%s to clear this.",
                        ipv_num,
                        _fqdn,
                        timediff,
                        ipv_num,
                    )
                ret[key] = []

    return ret