Skip to content

fix(connection): create default_channel only once under concurrent first access - #2873

Merged
auvipy merged 8 commits into
celery:mainfrom
tony1233200:fix/2869-default-channel-race
Nov 30, 2026
Merged

auvipy merged 8 commits into
celery:mainfrom
tony1233200:fix/2869-default-channel-race

Conversation

@tony1233200

Copy link
Copy Markdown
Contributor

Fixes #2869.

Connection.default_channel was lazily created with an unguarded check-then-act: two threads sharing one Connection and touching .default_channel for the first time concurrently could both read None, both call channel(), 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 Connection may 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:

  • New test test_Connection::test_default_channel_created_once_under_concurrency fails 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.

…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.
@auvipy auvipy added this to the 5.7.x milestone Nov 21, 2026
@codecov

codecov Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.46%. Comparing base (c738c54) to head (651a5e4).
⚠️ Report is 1 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

@auvipy
auvipy self-requested a review November 23, 2026 16:56
@auvipy

auvipy commented Nov 25, 2026

Copy link
Copy Markdown
Member

@MehrazRumman

@MehrazRumman MehrazRumman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread kombu/connection.py Resolved
auvipy and others added 2 commits November 26, 2026 00:06
Co-authored-by: Mehraz Hossain Rumman <59512321+MehrazRumman@users.noreply.github.com>
@tony1233200

tony1233200 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @MehrazRumman — addressed in 3358b77: revive() now returns early when new_channel is default_channel, so it no longer drops the reference without closing the channel; it only clears and closes when the channel really changed. The two out-of-scope notes (_connection_factory() / _do_close_self() lock coverage) are left as-is, as you suggested.

yc25778-droid and others added 2 commits November 26, 2026 22:35
…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
@auvipy

auvipy commented Nov 30, 2026

Copy link
Copy Markdown
Member

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.
@tony1233200

Copy link
Copy Markdown
Contributor Author

@auvipy Done — added test_default_channel_created_once_under_concurrency to BasicFunctionality in t/integration/common.py (651a5e4), so the check now runs against a real broker in every transport integration suite: two threads racing on first default_channel access must share exactly one channel instead of creating (and orphaning) a second one.

Verified locally against memory:// (passes with the fix, fails without it) and the unit suite for the connection module (158 passed); the broker-backed runs will exercise it in CI.

@auvipy
auvipy merged commit ffccd1c into celery:main Nov 30, 2026
47 checks passed
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.

Race: concurrent first access to Connection.default_channel creates and leaks a channel

4 participants