# Buggy code
def connected_ids(self, subset=None, show_ipv4=False, include_localhost=False):
    """
    Return a set of all connected minion ids, optionally within a subset
    """
    minions = set()
    if self.opts.get("minion_data_cache", False):
        search = self.cache.list("minions")
        if search is None:
            return minions
        addrs = salt.utils.network.local_port_tcp(int(self.opts["publish_port"]))
        if "127.0.0.1" in addrs or "0.0.0.0" in addrs:
            # Add in possible ip addresses of a locally connected minion
            addrs.discard("127.0.0.1")
            addrs.discard("0.0.0.0")
            addrs.update(
                set(salt.utils.network.ip_addrs(include_loopback=include_localhost))
            )
        if subset:
            search = subset
        for id_ in search:
            mdata = self.cache.fetch("minions/{0}".format(id_), "data")
            if mdata is None:
                continue
            grains = mdata.get("grains", {})
            for ipv4 in grains.get("ipv4", []):
                if ipv4 == "127.0.0.1" and not include_localhost:
                    continue
                if ipv4 == "0.0.0.0":
                    continue
                if ipv4 in addrs:
                    if show_ipv4:
                        minions.add((id_, ipv4))
                    else:
                        minions.add(id_)
                    break
    return minions


# Traceback (SaltCacheError)
[ERROR   ] An un-handled exception was caught by salt's global exception handler:
SaltCacheError: There was an error reading the cache file "/var/cache/salt/master/minions/apollo/data.p": [Errno 13] Permission denied: '/var/cache/salt/master/minions/apollo/data.p'
Traceback (most recent call last):
File "/opt/local/bin/salt", line 10, in <module>
salt_main()
File "/opt/local/lib/python2.7/site-packages/salt/scripts.py", line 446, in salt_main
client.run()
File "/opt/local/lib/python2.7/site-packages/salt/cli/salt.py", line 163, in run
for full_ret in cmd_func(**kwargs):
File "/opt/local/lib/python2.7/site-packages/salt/client/__init__.py", line 638, in cmd_cli
**kwargs):
File "/opt/local/lib/python2.7/site-packages/salt/client/__init__.py", line 1332, in get_cli_event_returns
connected_minions = salt.utils.minions.CkMinions(self.opts).connected_ids()
File "/opt/local/lib/python2.7/site-packages/salt/utils/minions.py", line 583, in connected_ids
mdata = self.cache.fetch('minions/{0}'.format(id_), 'data')
File "/opt/local/lib/python2.7/site-packages/salt/cache/__init__.py", line 146, in fetch
return self.modules[fun](bank, key)
File "/opt/local/lib/python2.7/site-packages/salt/cache/localfs.py", line 68, in fetch
key_file, exc
SaltCacheError: There was an error reading the cache file "/var/cache/salt/master/minions/apollo/data.p": [Errno 13] Permission denied: '/var/cache/salt/master/minions/apollo/data.p'
Traceback (most recent call last):
File "/opt/local/bin/salt", line 10, in <module>
salt_main()
File "/opt/local/lib/python2.7/site-packages/salt/scripts.py", line 446, in salt_main
client.run()
File "/opt/local/lib/python2.7/site-packages/salt/cli/salt.py", line 163, in run
for full_ret in cmd_func(**kwargs):
File "/opt/local/lib/python2.7/site-packages/salt/client/__init__.py", line 638, in cmd_cli
**kwargs):
File "/opt/local/lib/python2.7/site-packages/salt/client/__init__.py", line 1332, in get_cli_event_returns
connected_minions = salt.utils.minions.CkMinions(self.opts).connected_ids()
File "/opt/local/lib/python2.7/site-packages/salt/utils/minions.py", line 583, in connected_ids
mdata = self.cache.fetch('minions/{0}'.format(id_), 'data')
File "/opt/local/lib/python2.7/site-packages/salt/cache/__init__.py", line 146, in fetch
return self.modules[fun](bank, key)
File "/opt/local/lib/python2.7/site-packages/salt/cache/localfs.py", line 68, in fetch
key_file, exc
salt.exceptions.SaltCacheError: There was an error reading the cache file "/var/cache/salt/master/minions/apollo/data.p": [Errno 13] Permission denied: '/var/cache/salt/master/minions/apollo/data.p'

# Fixed code
def connected_ids(self, subset=None, show_ipv4=False, include_localhost=False):
    """
    Return a set of all connected minion ids, optionally within a subset
    """
    minions = set()
    if self.opts.get("minion_data_cache", False):
        search = self.cache.list("minions")
        if search is None:
            return minions
        addrs = salt.utils.network.local_port_tcp(int(self.opts["publish_port"]))
        if "127.0.0.1" in addrs or "0.0.0.0" in addrs:
            # Add in possible ip addresses of a locally connected minion
            addrs.discard("127.0.0.1")
            addrs.discard("0.0.0.0")
            addrs.update(
                set(salt.utils.network.ip_addrs(include_loopback=include_localhost))
            )
        if subset:
            search = subset
        for id_ in search:
            try:
                mdata = self.cache.fetch("minions/{0}".format(id_), "data")
            except SaltCacheError:
                # If a SaltCacheError is explicitly raised during the fetch operation,
                # permission was denied to open the cached data.p file. Continue on as
                # in the releases <= 2016.3. (An explicit error raise was added in PR
                # #35388. See issue #36867 for more information.
                continue
            if mdata is None:
                continue
            grains = mdata.get("grains", {})
            for ipv4 in grains.get("ipv4", []):
                if ipv4 == "127.0.0.1" and not include_localhost:
                    continue
                if ipv4 == "0.0.0.0":
                    continue
                if ipv4 in addrs:
                    if show_ipv4:
                        minions.add((id_, ipv4))
                    else:
                        minions.add(id_)
                    break
    return minions