# Buggy code
def _task_from_fun(self, fun, name=None, base=None, bind=False, **options):
    if not self.finalized and not self.autofinalize:
        raise RuntimeError("Contract breach: app not finalized")
    name = name or self.gen_task_name(fun.__name__, fun.__module__)
    base = base or self.Task

    if name not in self._tasks:
        run = fun if bind else staticmethod(fun)
        task = type(
            fun.__name__,
            (base,),
            dict(
                {
                    "app": self,
                    "name": name,
                    "run": run,
                    "_decorated": True,
                    "__doc__": fun.__doc__,
                    "__module__": fun.__module__,
                    "__header__": staticmethod(head_from_fun(fun, bound=bind)),
                    "__wrapped__": run,
                },
                **options,
            ),
        )()
        # for some reason __qualname__ cannot be set in type()
        # so we have to set it here.
        try:
            task.__qualname__ = fun.__qualname__
        except AttributeError:
            pass
        self._tasks[task.name] = task
        task.bind(self)  # connects task to this app

        autoretry_for = tuple(
            options.get("autoretry_for", getattr(task, "autoretry_for", ()))
        )
        retry_kwargs = options.get("retry_kwargs", getattr(task, "retry_kwargs", {}))
        retry_backoff = int(
            options.get("retry_backoff", getattr(task, "retry_backoff", False))
        )
        retry_backoff_max = int(
            options.get("retry_backoff_max", getattr(task, "retry_backoff_max", 600))
        )
        retry_jitter = options.get("retry_jitter", getattr(task, "retry_jitter", True))

        if autoretry_for and not hasattr(task, "_orig_run"):

            @wraps(task.run)
            def run(*args, **kwargs):
                try:
                    return task._orig_run(*args, **kwargs)
                except Ignore:
                    # If Ignore signal occures task shouldn't be retried,
                    # even if it suits autoretry_for list
                    raise
                except autoretry_for as exc:
                    if retry_backoff:
                        retry_kwargs["countdown"] = get_exponential_backoff_interval(
                            factor=retry_backoff,
                            retries=task.request.retries,
                            maximum=retry_backoff_max,
                            full_jitter=retry_jitter,
                        )
                    raise task.retry(exc=exc, **retry_kwargs)

            task._orig_run, task.run = task.run, run
    else:
        task = self._tasks[name]
    return task


# Traceback (Exception)
[2020-05-31 23:28:34,434: INFO/MainProcess] Connected to amqp://remote_worker:**@127.0.0.1:5672//
[2020-05-31 23:28:34,453: INFO/MainProcess] mingle: searching for neighbors
[2020-05-31 23:28:35,487: INFO/MainProcess] mingle: all alone
[2020-05-31 23:28:35,528: WARNING/MainProcess] /home/ubuntu/.local/lib/python3.7/site-packages/celery/fixups/django.py:203: UserWarning: Using settings.DEBUG leads to a memory
leak, never use this setting in production environments!
leak, never use this setting in production environments!''')
[2020-05-31 23:28:35,529: INFO/MainProcess] celery@testroom ready.
[2020-05-31 23:28:47,351: INFO/MainProcess] Received task: api_v3.tests.execute[e97d93b5-b0e5-4b87-96ab-1aab66119906]
[2020-05-31 23:28:47,689: WARNING/ForkPoolWorker-1] started
[2020-05-31 23:28:47,690: WARNING/ForkPoolWorker-1] retry
[2020-05-31 23:28:47,721: INFO/MainProcess] Received task: api_v3.tests.execute[e97d93b5-b0e5-4b87-96ab-1aab66119906]  ETA:[2020-05-31 23:28:57.692348+00:00]
[2020-05-31 23:28:47,722: INFO/MainProcess] Received task: api_v3.tests.execute[e97d93b5-b0e5-4b87-96ab-1aab66119906]  ETA:[2020-05-31 23:28:57.716321+00:00]
[2020-05-31 23:28:47,777: INFO/ForkPoolWorker-1] Task api_v3.tests.execute[e97d93b5-b0e5-4b87-96ab-1aab66119906] retry: Retry in 10s: Retry(Retry(...), Exception('i have filled now'), 10)
[2020-05-31 23:28:57,999: WARNING/ForkPoolWorker-1] started
[2020-05-31 23:28:58,000: WARNING/ForkPoolWorker-1] ended
[2020-05-31 23:28:58,062: INFO/ForkPoolWorker-1] Task api_v3.tests.execute[e97d93b5-b0e5-4b87-96ab-1aab66119906] succeeded in 0.34440315900428686s: None
[2020-05-31 23:28:58,301: WARNING/ForkPoolWorker-1] started
[2020-05-31 23:28:58,302: WARNING/ForkPoolWorker-1] retry
[2020-05-31 23:28:58,304: INFO/MainProcess] Received task: api_v3.tests.execute[e97d93b5-b0e5-4b87-96ab-1aab66119906]  ETA:[2020-05-31 23:29:08.303091+00:00]
[2020-05-31 23:28:58,307: INFO/MainProcess] Received task: api_v3.tests.execute[e97d93b5-b0e5-4b87-96ab-1aab66119906]  ETA:[2020-05-31 23:29:08.306141+00:00]
[2020-05-31 23:28:58,368: INFO/ForkPoolWorker-1] Task api_v3.tests.execute[e97d93b5-b0e5-4b87-96ab-1aab66119906] retry: Retry in 10s: Retry(Retry(...), Exception('i have filled now'), 10)
[2020-05-31 23:29:08,572: WARNING/ForkPoolWorker-1] started
[2020-05-31 23:29:08,573: WARNING/ForkPoolWorker-1] ended
[2020-05-31 23:29:08,633: INFO/ForkPoolWorker-1] Task api_v3.tests.execute[e97d93b5-b0e5-4b87-96ab-1aab66119906] succeeded in 0.3256059319974156s: None
[2020-05-31 23:29:08,872: WARNING/ForkPoolWorker-1] started
[2020-05-31 23:29:08,873: WARNING/ForkPoolWorker-1] retry
[2020-05-31 23:29:08,875: INFO/MainProcess] Received task: api_v3.tests.execute[e97d93b5-b0e5-4b87-96ab-1aab66119906]  ETA:[2020-05-31 23:29:18.873799+00:00]
[2020-05-31 23:29:08,880: INFO/MainProcess] Received task: api_v3.tests.execute[e97d93b5-b0e5-4b87-96ab-1aab66119906]  ETA:[2020-05-31 23:29:18.877550+00:00]
[2020-05-31 23:29:08,940: INFO/ForkPoolWorker-1] Task api_v3.tests.execute[e97d93b5-b0e5-4b87-96ab-1aab66119906] retry: Retry in 10s: Retry(Retry(...), Exception('i have filled now'), 10)
[2020-05-31 23:29:19,144: WARNING/ForkPoolWorker-1] started
[2020-05-31 23:29:19,145: WARNING/ForkPoolWorker-1] ended
[2020-05-31 23:29:19,205: INFO/ForkPoolWorker-1] Task api_v3.tests.execute[e97d93b5-b0e5-4b87-96ab-1aab66119906] succeeded in 0.326258520995907s: None
[2020-05-31 23:29:19,444: WARNING/ForkPoolWorker-1] started
[2020-05-31 23:29:19,445: WARNING/ForkPoolWorker-1] retry
[2020-05-31 23:29:19,505: ERROR/ForkPoolWorker-1] Task api_v3.tests.execute[e97d93b5-b0e5-4b87-96ab-1aab66119906] raised unexpected: Exception('i have filled now')
Traceback (most recent call last):
File "/home/ubuntu/.local/lib/python3.7/site-packages/celery/app/trace.py", line 385, in trace_task
R = retval = fun(*args, **kwargs)
File "/home/ubuntu/.local/lib/python3.7/site-packages/celery/app/trace.py", line 650, in __protected_call__
return self.run(*args, **kwargs)
File "/home/ubuntu/.local/lib/python3.7/site-packages/celery/app/base.py", line 500, in run
raise task.retry(exc=exc, **retry_kwargs)
File "/home/ubuntu/.local/lib/python3.7/site-packages/celery/app/task.py", line 704, in retry
raise_with_context(exc)
File "/home/ubuntu/.local/lib/python3.7/site-packages/celery/app/base.py", line 487, in run
return task._orig_run(*args, **kwargs)
File "/var/www/django_projects/earthalytics-api/api_v3/tests.py", line 26, in execute
self.retry(exc=Exception("i have filled now"), args=[param_a, param_b], kwargs=kwargs)
File "/home/ubuntu/.local/lib/python3.7/site-packages/celery/app/task.py", line 704, in retry
raise_with_context(exc)
File "/home/ubuntu/.local/lib/python3.7/site-packages/celery/utils/serialization.py", line 288, in raise_with_context
_raise_with_context(exc, exc_info[1])
File "<string>", line 1, in _raise_with_context
Exception: i have filled now

# Fixed code
def _task_from_fun(self, fun, name=None, base=None, bind=False, **options):
    if not self.finalized and not self.autofinalize:
        raise RuntimeError("Contract breach: app not finalized")
    name = name or self.gen_task_name(fun.__name__, fun.__module__)
    base = base or self.Task

    if name not in self._tasks:
        run = fun if bind else staticmethod(fun)
        task = type(
            fun.__name__,
            (base,),
            dict(
                {
                    "app": self,
                    "name": name,
                    "run": run,
                    "_decorated": True,
                    "__doc__": fun.__doc__,
                    "__module__": fun.__module__,
                    "__header__": staticmethod(head_from_fun(fun, bound=bind)),
                    "__wrapped__": run,
                },
                **options,
            ),
        )()
        # for some reason __qualname__ cannot be set in type()
        # so we have to set it here.
        try:
            task.__qualname__ = fun.__qualname__
        except AttributeError:
            pass
        self._tasks[task.name] = task
        task.bind(self)  # connects task to this app

        autoretry_for = tuple(
            options.get("autoretry_for", getattr(task, "autoretry_for", ()))
        )
        retry_kwargs = options.get("retry_kwargs", getattr(task, "retry_kwargs", {}))
        retry_backoff = int(
            options.get("retry_backoff", getattr(task, "retry_backoff", False))
        )
        retry_backoff_max = int(
            options.get("retry_backoff_max", getattr(task, "retry_backoff_max", 600))
        )
        retry_jitter = options.get("retry_jitter", getattr(task, "retry_jitter", True))

        if autoretry_for and not hasattr(task, "_orig_run"):

            @wraps(task.run)
            def run(*args, **kwargs):
                try:
                    return task._orig_run(*args, **kwargs)
                except Ignore:
                    # If Ignore signal occures task shouldn't be retried,
                    # even if it suits autoretry_for list
                    raise
                except Retry:
                    raise
                except autoretry_for as exc:
                    if retry_backoff:
                        retry_kwargs["countdown"] = get_exponential_backoff_interval(
                            factor=retry_backoff,
                            retries=task.request.retries,
                            maximum=retry_backoff_max,
                            full_jitter=retry_jitter,
                        )
                    raise task.retry(exc=exc, **retry_kwargs)

            task._orig_run, task.run = task.run, run
    else:
        task = self._tasks[name]
    return task