Repository navigation
Redis Result Consumer: unsubscribe on message success - #4666
Conversation
| ) | ||
| return self._pubsub | ||
|
|
||
| def on_after_fork(self): |
There was a problem hiding this comment.
I think that this method should be called to possibly avoid re-use of the same object references (and thus perpetuate the leaks), but I am not entirely sure. Perhaps I can make it a separate pull request.
There was a problem hiding this comment.
I believe the re-instantiation of Redis client objects however also addresses this comment: #3808 (comment)
| self._pubsub = None | ||
| super(ResultConsumer, self).on_after_fork() | ||
|
|
||
| def on_state_change(self, meta, message): |
There was a problem hiding this comment.
The actual fix is in this method.
auvipy
left a comment
There was a problem hiding this comment.
Kindly fix the lint errors to trigger travis test matrix
/home/travis/build/celery/celery/celery/backends/redis.py:8:1: F401 'kombu.utils.objects.cached_property' imported but unused
/home/travis/build/celery/celery/celery/backends/redis.py:81:9: F841 local variable '_' is assigned to but never used
|
Good observation @auvipy , thanks. |
Codecov Report
@@ Coverage Diff @@
## master #4666 +/- ##
==========================================
+ Coverage 82.52% 82.56% +0.04%
==========================================
Files 140 140
Lines 15894 15905 +11
Branches 1980 1982 +2
==========================================
+ Hits 13116 13132 +16
+ Misses 2586 2580 -6
- Partials 192 193 +1
Continue to review full report at Codecov.
|
thedrow
left a comment
There was a problem hiding this comment.
At this point I'm mostly neatpicking.
This is good work!
| @property | ||
| def client(self): | ||
| return self._create_client(**self.connparams) | ||
| if self._client is None: |
There was a problem hiding this comment.
Why does this change help to resolve the issue?
There was a problem hiding this comment.
@thedrow PubSub objects seem to maintain an internal channel registry, which they use to reconnect to all channels, in the event of a disconnection. I wanted to reset both the client connection pool and the pubsub instance, just to make sure that these do not introduce any leaks.
| self._pubsub = None | ||
|
|
||
| @property | ||
| def pubsub(self): |
There was a problem hiding this comment.
Why does this change resolve the issue?
I'd rather not introduce new API endpoints if they are not needed.
There was a problem hiding this comment.
I will remove it and use PubSub.close() which resets all internal state.
|
|
||
| def on_after_fork(self): | ||
| self.backend._client = None | ||
| self._pubsub = None |
There was a problem hiding this comment.
You can still use a cached_property. If you del self._pubsub a new value will be created (as far as I recall).
There was a problem hiding this comment.
Yes, that seems to be the case, thanks for pointing it out!
|
|
||
| def on_state_change(self, meta, message): | ||
| super(ResultConsumer, self).on_state_change(meta, message) | ||
| if meta['status'] in states.READY_STATES: |
There was a problem hiding this comment.
Please extract this logic to a private method. maybe_cancel_ready_task() or something similar.
It will convey the meaning of the code more clearly.
| for _ in range(total_chords) | ||
| ] | ||
|
|
||
| for _ in range(TIMEOUT): |
There was a problem hiding this comment.
While we're working on this code, we could use manager.join() instead of the custom logic in this test.
There was a problem hiding this comment.
manager.join() seems to block until the result set is completed. I just wanted to test for tasks in STARTED state. Perhaps I am missing something.
There was a problem hiding this comment.
You are correct. The manager has a wait_for method that could be used to wait for such state.
Also, maybe we should add is_started and assert_started to the manager and use that instead.
Whatever you choose is fine with me but the logic as it is now could be replaced with something generic.
|
|
||
| for _ in range(TIMEOUT): | ||
| if async_result.state == 'STARTED': | ||
| if all([result.state == 'STARTED' for result in async_results]): |
There was a problem hiding this comment.
There's no need to create a list here.
As a general rule, avoid lists when possible.
all(result.state == 'STARTED' for result in async_results)will work and finish faster if the test fails.
| channels_before = \ | ||
| len(redis_client.execute_command('PUBSUB CHANNELS')) | ||
| assert async_result.get(timeout=TIMEOUT) == 24 | ||
| assert channels_before == 2 * total_chords + initial_channels |
There was a problem hiding this comment.
Can you add a comment about this assert? It's not clear why that would be the case without diving into the problem this test proves that it no longer exists.
|
@thedrow thank you very much for the review. I agree on all comments. |
|
@thedrow I have completed the changes that I could think of, I believe I simplified the code to a large extent and improved the test case to be more explicit, but let me know if you find further issues. |
thedrow
left a comment
There was a problem hiding this comment.
Where did the unit tests disappear to?
Please add them back so we won't reduce our coverage.
|
@thedrow I will add unit tests for the new function. |
|
@thedrow I have added methods to the Manager mixin and some unit tests for |
thedrow
left a comment
There was a problem hiding this comment.
Aside from the minor branch coverage reduction which we can resolve in another PR this LGTM.
Please open an issue about it before we merge.
|
@georgepsarakis The issue is about the technical debt created by merging this PR. |
- Use on_after_fork in consumer to reset PubSub and connection pool internal states. - Improve Canvas integration test.
|
@thedrow I believe I also fixed code coverage for the diff ( |
thedrow
left a comment
There was a problem hiding this comment.
Wonderful! Merging :)
Great job everybody in tracking this down and fixing this bug.
|
I have rerun @mleginus's sample code from issue #3812 and I still get the failure. My pip3 freeze is: The output of his script is: Redis channels count before chord execution 0 Several minutes after running the script, running 'redis-cli pubsub channels' still shows the celery-task-meta-xxx elements as still subscribed. I've tried this on two machines, one Ubuntu 16 and the other mac os X. Redis 4.0.7. I have confirmed that the tasks are actually succeeding: AsyncResult(task_id.id).status == "SUCCESS". Therefore, I believe the issue is still that the pubsub client is not receiving the task_succeed message from the work. Today is my first day looking at celery code, so my beliefs should not be trusted. I have found a workaround for now, which is not very nice: for tasks that have no return value, I call Let me know if I can provide any other info. |
Addresses issues discussed in #3812 and #3808 .