# Buggy code
def __init__(
    self,
    message,
    on_ack=noop,
    hostname=None,
    eventer=None,
    app=None,
    connection_errors=None,
    request_dict=None,
    task=None,
    on_reject=noop,
    body=None,
    headers=None,
    decoded=False,
    utc=True,
    maybe_make_aware=maybe_make_aware,
    maybe_iso8601=maybe_iso8601,
    **opts,
):
    if headers is None:
        headers = message.headers
    if body is None:
        body = message.body
    self.app = app
    self.message = message
    self.body = body
    self.utc = utc
    self._decoded = decoded
    if decoded:
        self.content_type = self.content_encoding = None
    else:
        self.content_type, self.content_encoding = (
            message.content_type,
            message.content_encoding,
        )

    self.id = headers["id"]
    type = self.type = self.name = headers["task"]
    self.root_id = headers.get("root_id")
    self.parent_id = headers.get("parent_id")
    if "shadow" in headers:
        self.name = headers["shadow"] or self.name
    if "timelimit" in headers:
        self.time_limits = headers["timelimit"]
    self.argsrepr = headers.get("argsrepr", "")
    self.kwargsrepr = headers.get("kwargsrepr", "")
    self.on_ack = on_ack
    self.on_reject = on_reject
    self.hostname = hostname or gethostname()
    self.eventer = eventer
    self.connection_errors = connection_errors or ()
    self.task = task or self.app.tasks[type]

    # timezone means the message is timezone-aware, and the only timezone
    # supported at this point is UTC.
    eta = headers.get("eta")
    if eta is not None:
        try:
            eta = maybe_iso8601(eta)
        except (AttributeError, ValueError, TypeError) as exc:
            raise InvalidTaskError("invalid ETA value {0!r}: {1}".format(eta, exc))
        self.eta = maybe_make_aware(eta, self.tzlocal)
    else:
        self.eta = None

    expires = headers.get("expires")
    if expires is not None:
        try:
            expires = maybe_iso8601(expires)
        except (AttributeError, ValueError, TypeError) as exc:
            raise InvalidTaskError(
                "invalid expires value {0!r}: {1}".format(expires, exc)
            )
        self.expires = maybe_make_aware(expires, self.tzlocal)
    else:
        self.expires = None

    delivery_info = message.delivery_info or {}
    properties = message.properties or {}
    headers.update(
        {
            "reply_to": properties.get("reply_to"),
            "correlation_id": properties.get("correlation_id"),
            "delivery_info": {
                "exchange": delivery_info.get("exchange"),
                "routing_key": delivery_info.get("routing_key"),
                "priority": properties.get("priority"),
                "redelivered": delivery_info.get("redelivered"),
            },
        }
    )
    self.request_dict = headers


# Traceback (TypeError)
[2018-07-16 06:09:46,229: CRITICAL/MainProcess] Unrecoverable error: TypeError("'NoneType' object is not iterable",)
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/celery/worker/worker.py", line 205, in start
self.blueprint.start(self)
File "/usr/lib/python2.7/site-packages/celery/bootsteps.py", line 119, in start
step.start(parent)
File "/usr/lib/python2.7/site-packages/celery/bootsteps.py", line 369, in start
return self.obj.start()
File "/usr/lib/python2.7/site-packages/celery/worker/consumer/consumer.py", line 322, in start
blueprint.start(self)
File "/usr/lib/python2.7/site-packages/celery/bootsteps.py", line 119, in start
step.start(parent)
File "/usr/lib/python2.7/site-packages/celery/worker/consumer/consumer.py", line 598, in start
c.loop(*c.loop_args())
File "/usr/lib/python2.7/site-packages/celery/worker/loops.py", line 118, in synloop
qos.update()
File "/usr/lib/python2.7/site-packages/kombu/common.py", line 417, in update
return self.set(self.value)
File "/usr/lib/python2.7/site-packages/kombu/common.py", line 410, in set
self.callback(prefetch_count=new_value)
File "/usr/lib/python2.7/site-packages/celery/worker/consumer/tasks.py", line 47, in set_prefetch_count
apply_global=qos_global,
File "/usr/lib/python2.7/site-packages/kombu/messaging.py", line 558, in qos
apply_global)
File "/usr/lib/python2.7/site-packages/amqp/channel.py", line 1812, in basic_qos
wait=spec.Basic.QosOk,
File "/usr/lib/python2.7/site-packages/amqp/abstract_channel.py", line 59, in send_method
return self.wait(wait, returns_tuple=returns_tuple)
File "/usr/lib/python2.7/site-packages/amqp/abstract_channel.py", line 79, in wait
self.connection.drain_events(timeout=timeout)
File "/usr/lib/python2.7/site-packages/amqp/connection.py", line 491, in drain_events
while not self.blocking_read(timeout):
File "/usr/lib/python2.7/site-packages/amqp/connection.py", line 497, in blocking_read
return self.on_inbound_frame(frame)
File "/usr/lib/python2.7/site-packages/amqp/method_framing.py", line 77, in on_frame
callback(channel, msg.frame_method, msg.frame_args, msg)
File "/usr/lib/python2.7/site-packages/amqp/connection.py", line 501, in on_inbound_method
method_sig, payload, content,
File "/usr/lib/python2.7/site-packages/amqp/abstract_channel.py", line 128, in dispatch_method
listener(*args)
File "/usr/lib/python2.7/site-packages/amqp/channel.py", line 1597, in _on_basic_deliver
fun(msg)
File "/usr/lib/python2.7/site-packages/kombu/messaging.py", line 624, in _receive_callback
return on_m(message) if on_m else self.receive(decoded, message)
File "/usr/lib/python2.7/site-packages/celery/worker/consumer/consumer.py", line 572, in on_task_received
callbacks,
File "/usr/lib/python2.7/site-packages/celery/worker/strategy.py", line 200, in task_message_handler
handle(req)
File "/usr/lib/python2.7/site-packages/celery/worker/worker.py", line 228, in _process_task
req.execute_using_pool(self.pool)
File "/usr/lib/python2.7/site-packages/celery/worker/request.py", line 520, in execute_using_pool
time_limit, soft_time_limit = self.time_limits
TypeError: 'NoneType' object is not iterable

# Fixed code
def __init__(
    self,
    message,
    on_ack=noop,
    hostname=None,
    eventer=None,
    app=None,
    connection_errors=None,
    request_dict=None,
    task=None,
    on_reject=noop,
    body=None,
    headers=None,
    decoded=False,
    utc=True,
    maybe_make_aware=maybe_make_aware,
    maybe_iso8601=maybe_iso8601,
    **opts,
):
    if headers is None:
        headers = message.headers
    if body is None:
        body = message.body
    self.app = app
    self.message = message
    self.body = body
    self.utc = utc
    self._decoded = decoded
    if decoded:
        self.content_type = self.content_encoding = None
    else:
        self.content_type, self.content_encoding = (
            message.content_type,
            message.content_encoding,
        )

    self.id = headers["id"]
    type = self.type = self.name = headers["task"]
    self.root_id = headers.get("root_id")
    self.parent_id = headers.get("parent_id")
    if "shadow" in headers:
        self.name = headers["shadow"] or self.name
    timelimit = headers.get("timelimit", None)
    if timelimit:
        self.time_limits = timelimit
    self.argsrepr = headers.get("argsrepr", "")
    self.kwargsrepr = headers.get("kwargsrepr", "")
    self.on_ack = on_ack
    self.on_reject = on_reject
    self.hostname = hostname or gethostname()
    self.eventer = eventer
    self.connection_errors = connection_errors or ()
    self.task = task or self.app.tasks[type]

    # timezone means the message is timezone-aware, and the only timezone
    # supported at this point is UTC.
    eta = headers.get("eta")
    if eta is not None:
        try:
            eta = maybe_iso8601(eta)
        except (AttributeError, ValueError, TypeError) as exc:
            raise InvalidTaskError("invalid ETA value {0!r}: {1}".format(eta, exc))
        self.eta = maybe_make_aware(eta, self.tzlocal)
    else:
        self.eta = None

    expires = headers.get("expires")
    if expires is not None:
        try:
            expires = maybe_iso8601(expires)
        except (AttributeError, ValueError, TypeError) as exc:
            raise InvalidTaskError(
                "invalid expires value {0!r}: {1}".format(expires, exc)
            )
        self.expires = maybe_make_aware(expires, self.tzlocal)
    else:
        self.expires = None

    delivery_info = message.delivery_info or {}
    properties = message.properties or {}
    headers.update(
        {
            "reply_to": properties.get("reply_to"),
            "correlation_id": properties.get("correlation_id"),
            "delivery_info": {
                "exchange": delivery_info.get("exchange"),
                "routing_key": delivery_info.get("routing_key"),
                "priority": properties.get("priority"),
                "redelivered": delivery_info.get("redelivered"),
            },
        }
    )
    self.request_dict = headers