Repository navigation
fix(connection): create default_channel only once under concurrent first access - #2873
Conversation
…rst access Guard the lazy creation of Connection.default_channel with a lock (double-checked locking) so two threads sharing one Connection do not both call channel() and orphan one of the created channels. The same lock also guards revive(), which clears the field, so a revive cannot drop a channel created concurrently. Fixes celery#2869.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2873 +/- ##
=======================================
Coverage 86.45% 86.46%
=======================================
Files 80 80
Lines 11853 11860 +7
Branches 1443 1444 +1
=======================================
+ Hits 10248 10255 +7
Misses 1390 1390
Partials 215 215 ☔ View full report in Codecov by Harness. |
MehrazRumman
left a comment
There was a problem hiding this comment.
Locking around the lazy creation in default_channel looks right to me. The inner is None check, inside the lock, is the one that matters, and the new test fails on main (two channels created) because channel() now runs under the lock.
One change is needed, in revive(): it now clears _default_channel in more cases than before (inline). That can orphan a channel, which is the problem this PR sets out to fix.
Connection.revive() only runs when the connection itself is the object passed to ensure() (conn.ensure(conn, fun)), so this is a narrow path. Still, ensure() revives with channel = self.default_channel, which is exactly the case where new_channel is default_channel.
Not for this PR: _connection_factory() (self._default_channel = None) and _do_close_self() still read and write the field without the lock. As far as I can see, neither can create a second channel, so the issue the PR targets is covered.
CI is green on 1c9cef9.
Co-authored-by: Mehraz Hossain Rumman <59512321+MehrazRumman@users.noreply.github.com>
|
Thanks for the review, @MehrazRumman — addressed in 3358b77: |
…no-op revive(conn.default_channel) used to drop the reference without closing the channel, so the next access opened a second channel and orphaned the first one. The early return inside the lock (restoring main's condition) fixes it; this test pins that behavior. Refs celery#2873
|
should we also try integration tests to verify this changes? |
Add the concurrent-first-access check to BasicFunctionality so every broker-backed integration suite verifies it against a real broker: two threads racing on default_channel must end up sharing exactly one channel instead of creating (and orphaning) a second one. Addresses auvipy's review question on celery#2873 about integration-test coverage; the same scenario is already pinned at unit level in t/unit/test_connection.py. Refs celery#2869.
|
@auvipy Done — added Verified locally against |
Fixes #2869.
Connection.default_channelwas lazily created with an unguarded check-then-act: two threads sharing oneConnectionand touching.default_channelfor the first time concurrently could both readNone, both callchannel(), and only one result was kept — the other channel was orphaned (never closed, never reachable).This change guards the lazy creation with a lock (double-checked locking), so the second caller reuses the channel created by the first. The same lock guards
revive(), which clears the field, so a revive cannot drop a channel created concurrently.Note on scope: whether a
Connectionmay be shared across threads is currently undocumented either way (see #671). This fix assumes sharing is possible (as the issue does) and is a no-op for single-threaded use: one extra lock acquisition per first access, no behavior change otherwise. The_ensure_connection/reconnect interplay is unchanged.Verification:
test_Connection::test_default_channel_created_once_under_concurrencyfails on unpatched code (2 channels created, one orphaned) and passes with the fix.python -m pytest t/unit/test_connection.py: 155 passed, 6 skipped.flake8 kombu/connection.py t/unit/test_connection.py: clean.