Skip to content

Chain-in-chord that fails before the last task in the chain now triggers chord error handling - #6226

Closed
msci-jordancrawford wants to merge 2 commits into
celery:masterfrom
msci-jordancrawford:chord_in_chain_error_propogation
Closed

msci-jordancrawford wants to merge 2 commits into
celery:masterfrom
msci-jordancrawford:chord_in_chain_error_propogation

Conversation

@msci-jordancrawford

@msci-jordancrawford msci-jordancrawford commented Jul 13, 2020 •

Copy link
Copy Markdown
Contributor

Marked with xfail for now.

Note: Before submitting this pull request, please review our contributing
guidelines
.

Description

Fixes #6220.

Marked with xfail for now.
@lgtm-com

lgtm-com Bot commented Jul 13, 2020

Copy link
Copy Markdown

This pull request introduces 2 alerts and fixes 5 when merging 04226b1 into d537be4 - view on LGTM.com

new alerts:

  • 1 for Non-exception in 'except' clause
  • 1 for Wrong number of arguments in a call

fixed alerts:

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

This seems like it's almost certainly not the right way to fix this
problem, though it does cause the tests to turn green...
@msci-jordancrawford

Copy link
Copy Markdown
Contributor Author

I went ahead and added a fix as well, which should at least cause the tests to pass. That said, it really doesn't seem like the correct fix - if any of the maintainers have a suggestion for a more elegant way to fix this, it would be much appreciated. Thanks ahead of time!

I'm also not sure what the 'correct' behavior here is - should the client be expecting a ChordError or an ExpectedException?

@lgtm-com

lgtm-com Bot commented Jul 13, 2020

Copy link
Copy Markdown

This pull request introduces 1 alert and fixes 4 when merging 13c2e06 into d537be4 - view on LGTM.com

new alerts:

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

fixed alerts:

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

Comment thread celery/backends/base.py
if request:
if request.chord:
self.on_chord_part_return(request, state, exc)
elif request.chain and 'chord' in request.chain[-1]['options']:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

does this solve the reported issue?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes. The problem is that this snippet of code is executed for the task in the chain that fails, but only the final task in the chain passes the if request.chord check, so the chord only gets notified if it's the final task in the chain that fails. Adding this check solves the problem as bluntly as possible: by simply checking for a chord associated with the last element in the chain and updating it if necessary. Very much unsure if that is the right way forward.

It looks like it also breaks a couple of other tests. I think fixing those tests would just be a matter of changing this line to:

elif getattr(request, 'chain', None) and 'chord' in request.chain[-1]['options']:

but that just further reinforces my idea that this is maybe not the right way/place to be fixing this problem.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

keep digging with it please

assert len([cr for cr in chord_results if cr[2] != states.SUCCESS]
) == 1

def test_chain_in_chord_on_error(self, manager):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

the integration test has changed upstream, can you rebase, please?

@thedrow

thedrow commented Feb 28, 2021

Copy link
Copy Markdown
Contributor

@matusvalo Please rebase this. If you have the time, please debug this further.

@maxfenv

maxfenv commented Apr 20, 2021

Copy link
Copy Markdown

I don't know whether this is best reported here or on the issue, but this seems to be where the bulk of the discussion is. I hope the following is useful:

I've rebased this PR on top of the v4.4.7 tagged commit here: v4.4.7...maxfenv:chord_in_chain_error_propogation

In the context of one of the projects we maintain that uses celery, I wrote some unit tests that highlight the issue in that context and that fail with celery 4.4.7 but pass with this pr applied. I also applied the change suggested by @msci-jordancrawford (elif getattr(request, 'chain', None) and 'chord' in request.chain[-1]['options']:). With that applied I do in fact get no test failures on this branch: https://github.com/maxfenv/celery/tree/chord_in_chain_error_propogation. For avoidance of doubt, I'm running celery unit tests using docker like so:

cd docker
sudo docker-compose build celery
sudo docker-compose run --rm celery make test

For anyone wanting to replicate that exactly, it is also necessary to pin the dependencies in requirements/dev.txt to kombu<5 and amqp<5 for the docker image to build properly.

I hope to do a large scale integration test of this fix tomorrow and should be able to report on it's outcome by the day after that.

Thank you very much @msci-jordancrawford for your work in hunting down, documenting and fixing this issue. This has been causing us grief for some time!

@thedrow

thedrow commented Apr 21, 2021

Copy link
Copy Markdown
Contributor

Could you please contribute the unit tests?

@maxfenv

maxfenv commented Apr 21, 2021

Copy link
Copy Markdown

Yes I can do, though they are fundamentally not different to the test already included here. I could commit them to my fork or include them as a snipped here, I of course can't push directly to the source branch of this PR.

@thedrow

thedrow commented Apr 21, 2021

Copy link
Copy Markdown
Contributor

If they are not different, then why did you write them?

@thedrow thedrow assigned maybe-sybr and unassigned matusvalo Apr 21, 2021
@maxfenv

maxfenv commented Apr 21, 2021

Copy link
Copy Markdown

I wrote them because the barrier to entry was lower than figuring out how to run the celery project's unit tests from cold. I've since done that anyway, but it seemed like the thing to do at the time. Again, I wrote those tests in another project.

In any case, I've added a those hopefully non controversial test cases to t/integration/test_canvas.py alongside the test written by @msci-jordancrawford. You may find them here: https://github.com/maxfenv/celery/tree/chord_in_chain_error_propogation (exact commit: 1d73847).

I'm unsure about decorating them with @flakey, I suppose they should be, since they rely on the result returning before the timeout.

@maxfenv

maxfenv commented Apr 21, 2021

Copy link
Copy Markdown

Unfortunately error handling appears to not work if the failing task is not in the last two tasks of a chain. That is to say: The chord error fires correctly if the failing task in the chain is either the last or second to last, but not if it is (say) the 1st or 2nd out of 4.

Here are some more tests to highlight that issue: 1a3b36b

We shall have a think about what the fix might look like and report back. Since this is starting to diverge substantially from the commits in this PR at this point, would it be acceptable to open a new PR with the fix (if one is forthcoming). I will still base the branch off of this one, so @msci-jordancrawford's commits remain in the history. I don't want to claim any undue credit here.

@maxfenv

maxfenv commented Apr 21, 2021

Copy link
Copy Markdown

The issue appears to be that the context with the 'chord' key in the 'options' is always the first element of the request.chains list. As such, it should be sufficient to index like so: request.chain[0]['options'], instead of with the -1.

However, I'm not certain on that point, so instead I opted to check for all contexts with a 'chord' and to pick the first among those, if any. I've commited those changes to my version of this branch: https://github.com/maxfenv/celery/tree/chord_in_chain_error_propogation. The code there passes all tests, apart from t/unit/backends/test_database.py:209 test_DatabaseBackend.test_cleanup for reasons that still escape me.

@maxfenv

maxfenv commented Apr 21, 2021

Copy link
Copy Markdown

The test failure mention above only occurs on python < 3.8 and is, I believe, related to mismatching versions of pickle. Something like this: https://stackoverflow.com/questions/63329657/python-3-7-error-unsupported-pickle-protocol-5

@maybe-sybr

maybe-sybr commented Apr 22, 2021 •

Copy link
Copy Markdown
Contributor

Oh my gosh, yes! Thank you for the changeset @msci-jordancrawford and thanks for bumping this @maxfenv . I was smashing my face against this all of yesterday without having seen this.

My gut feel is that this approach will work but I was a bit concerned about reaching deep into the options to find the appropriate group_id to fire a chord_part_complete for. The points which @maxfenv has raised probably help make that point, I'm just not sure how reliable/fragile that data is. The test cases will help and I'll have a solid half day to dig into this tomorrow :)

@maybe-sybr

maybe-sybr commented Apr 27, 2021 •

Copy link
Copy Markdown
Contributor

There is a PR up (#6746) which should fix this issue. I'm going to close this PR in favour of that one.

@maybe-sybr maybe-sybr closed this Apr 27, 2021
@auvipy auvipy removed this from the Future milestone Jun 7, 2023
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.

Chain-in-chord that fails before the last task in the chain doesn't trigger chord error handling

6 participants