Skip to content

net: manager owns net lifecycle (auto-start/close, ownership-gated)#3118

Merged
dpkp merged 2 commits into
masterfrom
dpkp/manager-owns-net
Jul 12, 2026
Merged

net: manager owns net lifecycle (auto-start/close, ownership-gated)#3118
dpkp merged 2 commits into
masterfrom
dpkp/manager-owns-net

Conversation

@dpkp

@dpkp dpkp commented Jul 11, 2026

Copy link
Copy Markdown
Owner

Make KafkaConnectionManager self-sufficient for direct use: track net
ownership (owned iff the manager created it -- net arg is None or a name
string, not a passed-in NetBackend instance), auto-start the owned net lazily
on the blocking entry points (run/bootstrap), and close it in close(). This
lets a direct manager user do KafkaConnectionManager(bootstrap_servers=...)
then bootstrap()/run() with no explicit _net.start()/close() -- and advances
Phase-D (manager as the entry point, not the compat shim).

Ownership gating is what keeps it safe:

  • Auto-start only on run()/bootstrap() (blocking user entry points), NOT on
    the internal fire-and-forget call_soon() -- which cluster/sender/coordinator
    call and which must not spin up a thread.
  • on_io_thread() guard on the auto-close so close() from a produce callback
    (on the IO thread) doesn't try to join that thread.
  • The unit-test harness passes an instance (unstarted NetworkSelector it
    drives via drain()/poll()) -> not owned -> untouched.

KafkaNetClient (the legacy poll()-driving shim) manages its net lifecycle
explicitly (start/poll/close), so it now resolves + creates its own net and
hands the manager a ready instance -- ownership follows creation, and the
manager does NOT auto-start/close it (which would collide with the shim's
net.poll() driving). Direct KafkaConnectionManager(net=None) users get the
auto lifecycle instead.

Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com

dpkp and others added 2 commits July 11, 2026 10:53
Make KafkaConnectionManager self-sufficient for direct use: track net
ownership (owned iff the manager created it -- net arg is None or a name
string, not a passed-in NetBackend instance), auto-start the owned net lazily
on the blocking entry points (run/bootstrap), and close it in close(). This
lets a direct manager user do KafkaConnectionManager(bootstrap_servers=...)
then bootstrap()/run() with no explicit _net.start()/close() -- and advances
Phase-D (manager as the entry point, not the compat shim).

Ownership gating is what keeps it safe:
  - Auto-start only on run()/bootstrap() (blocking user entry points), NOT on
    the internal fire-and-forget call_soon() -- which cluster/sender/coordinator
    call and which must not spin up a thread.
  - on_io_thread() guard on the auto-close so close() from a produce callback
    (on the IO thread) doesn't try to join that thread.
  - The unit-test harness passes an *instance* (unstarted NetworkSelector it
    drives via drain()/poll()) -> not owned -> untouched.

KafkaNetClient (the legacy poll()-driving shim) manages its net lifecycle
explicitly (start/poll/close), so it now resolves + creates its own net and
hands the manager a ready instance -- ownership follows creation, and the
manager does NOT auto-start/close it (which would collide with the shim's
net.poll() driving). Direct KafkaConnectionManager(net=None) users get the
auto lifecycle instead.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@dpkp
dpkp merged commit dd4a178 into master Jul 12, 2026
19 checks passed
@dpkp
dpkp deleted the dpkp/manager-owns-net branch July 12, 2026 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant