Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Preserve call/errbacks of replaced tasks #6770

Merged
merged 6 commits into from Jun 15, 2021

Conversation

maybe-sybr
Copy link
Contributor

Description

This change fixes the handling of callbacks and errbacks on tasks which get replaced to
ensure they get called as expected if the replacement signature succeeds or fails.

Fixes #6441

@maybe-sybr
Copy link
Contributor Author

This is a bugfix but I think we can land it on master once we sort out 5.1 since it's been broken for a while. :) It'll improve my life but FWICT there are no issues from other users tracking this misbehaviour.

@lgtm-com
Copy link

lgtm-com bot commented May 14, 2021

This pull request introduces 1 alert when merging d8bf93f into 2411504 - view on LGTM.com

new alerts:

  • 1 for Non-exception in 'except' clause

Copy link
Member

@thedrow thedrow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll review the integration tests later on.
Please check the build log as we have a genuine test failure, I believe.

celery/app/task.py Show resolved Hide resolved
celery/canvas.py Outdated Show resolved Hide resolved
@maybe-sybr
Copy link
Contributor Author

t/unit/tasks/test_tasks.py::test_tasks::test_replace_callback() is failing because I'm linking the callbacks and errbacks of the replaced signature to a replacement group even though they should should only be linked to the accumulate task that we chain onto the end of the group signature. Since a callback/errback was being set for a group, when that group was eventually applied it would die due to the TypeError() we were commenting on above:

self = group([t.unit.tasks.test_tasks.return_True()]), args = (), kwargs = None, add_to_parent = True, producer = None
link = ['callbacks'], link_error = ['errbacks']
options = {'group_id': 'group', 'group_index': None, 'root_id': 'root_id', 'task_id': '739d4d46-a741-4db5-aa5b-eacdb75de6bf'}

    def apply_async(self, args=None, kwargs=None, add_to_parent=True,
                    producer=None, link=None, link_error=None, **options):
        args = args if args else ()
        if link is not None or link_error is not None:
>           raise TypeError('Cannot add linked tasks to groups: use a chord')
E           TypeError: Cannot add linked tasks to groups: use a chord

celery/canvas.py:1076: TypeError

I've changed the logic in replace() to only link call/errbacks to the replacement signature if it's not a group. I'll push a new diff once I've done some more sanity checking on the integration tests.

@maybe-sybr maybe-sybr marked this pull request as draft May 16, 2021 23:25
@maybe-sybr maybe-sybr force-pushed the fix/signature-replace-callbacks branch from d8bf93f to 00d5516 Compare May 17, 2021 00:28
@codecov
Copy link

codecov bot commented May 17, 2021

Codecov Report

Merging #6770 (5370fdb) into master (536849c) will increase coverage by 0.01%.
The diff coverage is 53.33%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #6770      +/-   ##
==========================================
+ Coverage   70.71%   70.73%   +0.01%     
==========================================
  Files         138      138              
  Lines       16604    16605       +1     
  Branches     2094     2094              
==========================================
+ Hits        11741    11745       +4     
+ Misses       4667     4663       -4     
- Partials      196      197       +1     
Flag Coverage Δ
unittests 70.73% <53.33%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
celery/canvas.py 94.82% <ø> (ø)
celery/app/task.py 94.65% <53.33%> (+0.81%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 536849c...5370fdb. Read the comment docs.

@lgtm-com
Copy link

lgtm-com bot commented May 17, 2021

This pull request introduces 2 alerts and fixes 1 when merging 00d5516 into 2411504 - view on LGTM.com

new alerts:

  • 2 for Unused import

fixed alerts:

  • 1 for Module is imported with 'import' and 'import from'

@maybe-sybr maybe-sybr force-pushed the fix/signature-replace-callbacks branch from 00d5516 to ff9f31f Compare May 17, 2021 00:47
@lgtm-com
Copy link

lgtm-com bot commented May 17, 2021

This pull request introduces 1 alert when merging ff9f31f into 2411504 - view on LGTM.com

new alerts:

  • 1 for Unused import

@maybe-sybr
Copy link
Contributor Author

I've reworked the replacement logic entirely now. It could do with a re-review. AFAICT, removing the early replacement_sig.freeze(self.request.id) hasn't disrupted any of our unit or integration tests (against redis) so I'm reasonably confident that the diff is fine to merge.

@maybe-sybr maybe-sybr marked this pull request as ready for review May 17, 2021 01:06
@maybe-sybr maybe-sybr marked this pull request as draft May 17, 2021 02:09
@maybe-sybr maybe-sybr force-pushed the fix/signature-replace-callbacks branch from ff9f31f to d705a73 Compare May 17, 2021 04:16
@maybe-sybr maybe-sybr marked this pull request as ready for review May 17, 2021 04:16
@maybe-sybr
Copy link
Contributor Author

This now behaves as I expect and we have some new integration tests to catch regressions or misbehaviours. That actually exposed an issue with chord header result ordering which I've also fixed in the commit on the tip of this branch.

@lgtm-com
Copy link

lgtm-com bot commented May 17, 2021

This pull request introduces 1 alert when merging d705a73 into 2411504 - view on LGTM.com

new alerts:

  • 1 for Non-exception in 'except' clause

celery/app/task.py Outdated Show resolved Hide resolved
@maybe-sybr maybe-sybr force-pushed the fix/signature-replace-callbacks branch 2 times, most recently from 639ba23 to 1095f05 Compare May 18, 2021 04:22
@lgtm-com
Copy link

lgtm-com bot commented May 18, 2021

This pull request introduces 1 alert and fixes 2 when merging 1095f05 into 2411504 - view on LGTM.com

new alerts:

  • 1 for Unused import

fixed alerts:

  • 1 for Non-exception in 'except' clause
  • 1 for Module is imported with 'import' and 'import from'

@maybe-sybr maybe-sybr force-pushed the fix/signature-replace-callbacks branch from 1095f05 to 1e74fb1 Compare May 19, 2021 00:38
@maybe-sybr
Copy link
Contributor Author

maybe-sybr commented May 19, 2021

That last fixup broke a unit test, it (should be) fixed again now.

@thedrow thedrow self-requested a review May 19, 2021 06:52
thedrow
thedrow previously approved these changes May 19, 2021
Copy link
Member

@thedrow thedrow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

This code is much cleaner than before.

@maybe-sybr maybe-sybr added this to the 5.2 milestone May 19, 2021
@maybe-sybr
Copy link
Contributor Author

LGTM.

This code is much cleaner than before.

Great thanks :) I've just marked this for 5.2 so we can come back and merge it once we drop 5.1

@maybe-sybr
Copy link
Contributor Author

I'll mark this as needs rebase to remind me to rebase on top of 5.1 as prep for merge

@lgtm-com
Copy link

lgtm-com bot commented May 23, 2021

This pull request introduces 1 alert when merging fcfbe43 into 025bad6 - view on LGTM.com

new alerts:

  • 1 for Unused import

Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rebase please. as it's a bug fix we can land this on 5.1.x as well

Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rebase please. as it's a bug fix we can land this on 5.1.x as well

This change modifies a bunch of the tests to use unique keys for the
`redis_echo` and `redis_count` tasks which are used to validate that
callbacks and errbacks are made. We also introduce helper functions for
validating that messages/counts are seen to reduce duplicate code.
This change adds some tests to ensure that when a task is replaced, it
runs as expected. This exposed a bug where the group index of a task
would be lost when replaced with a chain since chains would not pass
their `group_index` option down to the final task when applied. This
manifested as the results of chords being mis-ordered on the redis
backend since the group index would default to `+inf`. Other backends
may have had similar issues.
@maybe-sybr
Copy link
Contributor Author

rebase please. as it's a bug fix we can land this on 5.1.x as well

rebased on master

@lgtm-com
Copy link

lgtm-com bot commented Jun 15, 2021

This pull request introduces 2 alerts when merging 5370fdb into 536849c - view on LGTM.com

new alerts:

  • 1 for Non-exception in 'except' clause
  • 1 for Module is imported with 'import' and 'import from'

@auvipy auvipy removed this from the 5.2 milestone Jun 15, 2021
@auvipy auvipy merged commit 5d72aee into master Jun 15, 2021
@auvipy auvipy added this to the 5.1.x milestone Jun 15, 2021
@thedrow thedrow deleted the fix/signature-replace-callbacks branch June 15, 2021 09:04
thedrow pushed a commit that referenced this pull request Jun 27, 2021
* style: Remove unused var from canvas unit tests

* test: Check task ID re-freeze on replacement

* refac: Remove duped task ID preservation logic

* test: Rework canvas call/errback integration tests

This change modifies a bunch of the tests to use unique keys for the
`redis_echo` and `redis_count` tasks which are used to validate that
callbacks and errbacks are made. We also introduce helper functions for
validating that messages/counts are seen to reduce duplicate code.

* fix: Preserve call/errbacks of replaced tasks

Fixes #6441

* fix: Ensure replacement tasks get the group index

This change adds some tests to ensure that when a task is replaced, it
runs as expected. This exposed a bug where the group index of a task
would be lost when replaced with a chain since chains would not pass
their `group_index` option down to the final task when applied. This
manifested as the results of chords being mis-ordered on the redis
backend since the group index would default to `+inf`. Other backends
may have had similar issues.
jeyrce pushed a commit to jeyrce/celery that referenced this pull request Aug 25, 2021
* style: Remove unused var from canvas unit tests

* test: Check task ID re-freeze on replacement

* refac: Remove duped task ID preservation logic

* test: Rework canvas call/errback integration tests

This change modifies a bunch of the tests to use unique keys for the
`redis_echo` and `redis_count` tasks which are used to validate that
callbacks and errbacks are made. We also introduce helper functions for
validating that messages/counts are seen to reduce duplicate code.

* fix: Preserve call/errbacks of replaced tasks

Fixes celery#6441

* fix: Ensure replacement tasks get the group index

This change adds some tests to ensure that when a task is replaced, it
runs as expected. This exposed a bug where the group index of a task
would be lost when replaced with a chain since chains would not pass
their `group_index` option down to the final task when applied. This
manifested as the results of chords being mis-ordered on the redis
backend since the group index would default to `+inf`. Other backends
may have had similar issues.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Errback inherited from an encapsulating chain is dropped when replacing tasks
3 participants