<p dir="auto">Hello,</p>
<p dir="auto">When using a method based task, celery leaks the class instance causing a <code class="notranslate">TypeError</code> because it passes the argument twice. This problem seems to only occur when using the task retry feature. Here's the code you can use to reproduce this bug:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="class SampleTask(object):
    def test_celery_bug(self):
        for i in range(10):
            self._test_celery_bug.apply_async(args=[i])

    @current_app.task(filter=task_method, default_retry_delay=2, max_retries=3)
    def _test_celery_bug(self, asins):
        try:
            raise CustomError('ERROR D:&lt;')
        except CustomError as error:
            self._test_celery_bug.retry(exc=error)"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-v">SampleTask</span>(<span class="pl-s1">object</span>):
    <span class="pl-k">def</span> <span class="pl-en">test_celery_bug</span>(<span class="pl-s1">self</span>):
        <span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-c1">10</span>):
            <span class="pl-s1">self</span>.<span class="pl-s1">_test_celery_bug</span>.<span class="pl-en">apply_async</span>(<span class="pl-s1">args</span><span class="pl-c1">=</span>[<span class="pl-s1">i</span>])

    <span class="pl-en">@<span class="pl-s1">current_app</span>.<span class="pl-en">task</span>(<span class="pl-s1">filter</span><span class="pl-c1">=</span><span class="pl-s1">task_method</span>, <span class="pl-s1">default_retry_delay</span><span class="pl-c1">=</span><span class="pl-c1">2</span>, <span class="pl-s1">max_retries</span><span class="pl-c1">=</span><span class="pl-c1">3</span>)</span>
    <span class="pl-k">def</span> <span class="pl-en">_test_celery_bug</span>(<span class="pl-s1">self</span>, <span class="pl-s1">asins</span>):
        <span class="pl-k">try</span>:
            <span class="pl-k">raise</span> <span class="pl-v">CustomError</span>(<span class="pl-s">'ERROR D:&lt;'</span>)
        <span class="pl-k">except</span> <span class="pl-v">CustomError</span> <span class="pl-k">as</span> <span class="pl-s1">error</span>:
            <span class="pl-s1">self</span>.<span class="pl-s1">_test_celery_bug</span>.<span class="pl-en">retry</span>(<span class="pl-s1">exc</span><span class="pl-c1">=</span><span class="pl-s1">error</span>)</pre></div>
<p dir="auto">The code below causes the following error:</p>
<p dir="auto"><code class="notranslate">TypeError: _test_celery_bug() takes exactly 2 arguments (3 given)</code></p>
<p dir="auto">I'm currently using celery from master and was able to reproduce the bug on celery 2.3.23 . I'm also using Python 2.7.5.</p>
<p dir="auto">Hi,</p>
<p dir="auto">I get a lot of <a href="http://dev.1flow.net/webapps/1flow/group/956/" rel="nofollow">traces like this one</a> when tasks are re-launched after I called a <code class="notranslate">.retry()</code> from inside them.</p>
<p dir="auto">The code I use (basic version):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from celery.contrib.methods import task as celery_task_method


class test_retry(object):

    @celery_task_method(name='test_retry.test1')
    def test1(self, arg1=None, arg2=False):

        print 'in test1, %s, %s, %s' % (self, arg1, arg2)

        raise self.test1.retry(exc=RuntimeError(&quot;bla bla&quot;))

t = test()
t.test1.delay()  # OK
# 1st retry will print &quot;self, None, False&quot;
# 2nd retry will print &quot;self, self, False&quot;
# 3rd retry will print &quot;self, self, self&quot;
# 4th retry will raise an exception inside celery &quot; TypeError('test1() takes at most 3 arguments (4 given)',) !&quot;
"><pre class="notranslate"><code class="notranslate">from celery.contrib.methods import task as celery_task_method


class test_retry(object):

    @celery_task_method(name='test_retry.test1')
    def test1(self, arg1=None, arg2=False):

        print 'in test1, %s, %s, %s' % (self, arg1, arg2)

        raise self.test1.retry(exc=RuntimeError("bla bla"))

t = test()
t.test1.delay()  # OK
# 1st retry will print "self, None, False"
# 2nd retry will print "self, self, False"
# 3rd retry will print "self, self, self"
# 4th retry will raise an exception inside celery " TypeError('test1() takes at most 3 arguments (4 given)',) !"

</code></pre></div>
<p dir="auto">NOTE: don't know it that matters or not, but IRL the <code class="notranslate">class test_retry</code> doesn't inherit from <code class="notranslate">object</code> but from <code class="notranslate">mongoengine.Document</code>.</p>
<p dir="auto">PS: as both concern tasks as methods, perhaps <a href="https://github.com/celery/celery/issues/1459" data-hovercard-type="issue" data-hovercard-url="/celery/celery/issues/1459/hovercard">my other bug</a> could be related ? by the way, I had no problems with <code class="notranslate">.delay()</code> as far as I used it.</p>
<p dir="auto">Regards,</p>