# Buggy code
def loads(self, msg, encoding=None, raw=False):
    """
    Run the correct loads serialization format

    :param encoding: Useful for Python 3 support. If the msgpack data
                     was encoded using "use_bin_type=True", this will
                     differentiate between the 'bytes' type and the
                     'str' type by decoding contents with 'str' type
                     to what the encoding was set as. Recommended
                     encoding is 'utf-8' when using Python 3.
                     If the msgpack data was not encoded using
                     "use_bin_type=True", it will try to decode
                     all 'bytes' and 'str' data (the distinction has
                     been lost in this case) to what the encoding is
                     set as. In this case, it will fail if any of
                     the contents cannot be converted.
    """
    try:
        gc.disable()  # performance optimization for msgpack
        if msgpack.version >= (0, 4, 0):
            # msgpack only supports 'encoding' starting in 0.4.0.
            # Due to this, if we don't need it, don't pass it at all so
            # that under Python 2 we can still work with older versions
            # of msgpack.
            ret = msgpack.loads(msg, use_list=True, encoding=encoding)
        else:
            ret = msgpack.loads(msg, use_list=True)
        if six.PY3 and encoding is None and not raw:
            ret = salt.transport.frame.decode_embedded_strs(ret)
    except Exception as exc:
        log.critical(
            "Could not deserialize msgpack message. This often happens "
            "when trying to read a file not in binary mode. "
            "To see message payload, enable debug logging and retry. "
            "Exception: %s",
            exc,
        )
        log.debug("Msgpack deserialization failure on message: %s", msg)
        gc.collect()
        raise
    finally:
        gc.enable()
    return ret


# Traceback (SaltClientError)
2018-02-26 18:02:15,266 [salt.minion      :204 ][WARNING ][16492] Master ip address changed from 10.1.51.25 to 10.1.51.24
2018-02-26 18:02:15,616 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:16,613 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:17,611 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:18,611 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:19,611 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:20,288 [salt.minion      :204 ][WARNING ][16492] Master ip address changed from 10.1.51.24 to 10.1.51.25
2018-02-26 18:02:20,611 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:21,611 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:22,610 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:23,619 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:24,620 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:25,300 [salt.minion      :644 ][ERROR   ][16492] No master could be reached or all masters denied the minion's connection attempt.
2018-02-26 18:02:25,301 [salt.scripts     :146 ][WARNING ][16492] Fatal functionality error caught by minion handler:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/scripts.py", line 144, in minion_process
minion.start()
File "/usr/lib/python2.7/site-packages/salt/cli/daemons.py", line 350, in start
raise SaltClientError('Minion could not connect to Master')
SaltClientError: Minion could not connect to Master
2018-02-26 18:02:25,301 [salt.scripts     :147 ][WARNING ][16492] ** Restarting minion **

# Fixed code
def loads(self, msg, encoding=None, raw=False):
    """
    Run the correct loads serialization format

    :param encoding: Useful for Python 3 support. If the msgpack data
                     was encoded using "use_bin_type=True", this will
                     differentiate between the 'bytes' type and the
                     'str' type by decoding contents with 'str' type
                     to what the encoding was set as. Recommended
                     encoding is 'utf-8' when using Python 3.
                     If the msgpack data was not encoded using
                     "use_bin_type=True", it will try to decode
                     all 'bytes' and 'str' data (the distinction has
                     been lost in this case) to what the encoding is
                     set as. In this case, it will fail if any of
                     the contents cannot be converted.
    """
    try:

        def ext_type_decoder(code, data):
            if code == 78:
                data = salt.utils.stringutils.to_unicode(data)
                return datetime.datetime.strptime(data, "%Y%m%dT%H:%M:%S.%f")
            return data

        gc.disable()  # performance optimization for msgpack
        if msgpack.version >= (0, 4, 0):
            # msgpack only supports 'encoding' starting in 0.4.0.
            # Due to this, if we don't need it, don't pass it at all so
            # that under Python 2 we can still work with older versions
            # of msgpack.
            ret = msgpack.loads(
                msg, use_list=True, ext_hook=ext_type_decoder, encoding=encoding
            )
        else:
            ret = msgpack.loads(msg, use_list=True, ext_hook=ext_type_decoder)
        if six.PY3 and encoding is None and not raw:
            ret = salt.transport.frame.decode_embedded_strs(ret)
    except Exception as exc:
        log.critical(
            "Could not deserialize msgpack message. This often happens "
            "when trying to read a file not in binary mode. "
            "To see message payload, enable debug logging and retry. "
            "Exception: %s",
            exc,
        )
        log.debug("Msgpack deserialization failure on message: %s", msg)
        gc.collect()
        raise
    finally:
        gc.enable()
    return ret