Skip to content

feat(inmem): finalize attached controllers on release - #206

Merged
paralin merged 4 commits into
masterfrom
feat/attached-instance-release
Aug 12, 2026
Merged

feat(inmem): finalize attached controllers on release#206
paralin merged 4 commits into
masterfrom
feat/attached-instance-release

Conversation

@paralin

@paralin paralin commented Jul 30, 2026

Copy link
Copy Markdown
Member

The release function returned by AddController currently cancels Execute's derived context and detaches directive handling. It does not wait for Execute or call Controller.Close. It can therefore return while controller work still runs and resources remain open.

This change gives each in-memory attachment one finalization path. Release signals Execute to stop by canceling its context, detaches directive handling, waits for Execute, calls Close once, and reports joined execution and close errors. RemoveController uses the same path. ExecuteController retains a controller after a nil return and finalizes it after an error or panic. Finalization invokes controller code outside the bus mutex, so callbacks can reenter the bus.

A controller that ignores context cancellation still blocks release. The bus logs that controller every thirty seconds while preserving the unbounded wait. NewBus keeps its existing function signature; NewBusWithLogger lets the core bus route those warnings through its logger.

The AddController release function and existing NewBus callers remain source compatible. Callbacks can inspect ControllerCloseError when cleanup fails.

@paralin
paralin force-pushed the feat/attached-instance-release branch from 20a7441 to 96dc5bd Compare August 12, 2026 08:10
@paralin paralin changed the title feat/attached instance release feat(inmem): finalize attached controllers on release Aug 12, 2026
The release function returned by AddController only canceled Execute's
context and detached directive handling. It returned before Execute stopped
and never called Controller.Close, so release did not establish controller
shutdown.

Make each attached controller coordinate cancellation, detachment, Execute
completion, and once-only Close. Finalization runs outside the bus mutex so
controller callbacks can reenter the bus. RemoveController uses the same
synchronous boundary. ExecuteController keeps controllers attached after a
nil return and finalizes terminal failures.

The release function keeps its no-error signature. Its callback receives a
ControllerCloseError joined with any Execute failure, so existing callers
remain source compatible and can inspect cleanup failures. An Execute method
that ignores cancellation keeps release blocked until it returns.

Signed-off-by: Christian Stewart <christian@aperture.us>
Attached controllers carried a detachOnce guard while Bus.detachController
also guarded membership. The duplicate state split detachment idempotence
across two lifecycle mechanisms.

Use Bus.detachController as the single detachment boundary. The membership
transition now controls handler release and the controller-list broadcast, so
concurrent or repeated finalizers still release and broadcast once.

Signed-off-by: Christian Stewart <christian@aperture.us>
Release waits for Execute even when a controller ignores cancellation. A
stuck controller could therefore stop bus shutdown without reporting which
controller still held the lifecycle boundary.

Log the controller every thirty seconds until Execute returns. The core bus
passes its existing logger into the in-memory bus, while direct in-memory bus
callers receive a local fallback logger.

The warning observes the wait without changing it. Release still cancels,
detaches, waits without a timeout, calls Close once, and then returns.

Signed-off-by: Christian Stewart <christian@aperture.us>
Adding lifecycle diagnostics changed NewBus into a variadic function. Direct
calls still compiled, but code that stored NewBus in its established function
type no longer built.

Restore the original NewBus signature and add NewBusWithLogger for lifecycle
diagnostics. Existing calls and function values remain source compatible,
while the core bus supplies its logger through the new constructor.

Signed-off-by: Christian Stewart <christian@aperture.us>
@paralin
paralin force-pushed the feat/attached-instance-release branch from 96dc5bd to 9963220 Compare August 12, 2026 08:20
@paralin
paralin merged commit 7bd9dce into master Aug 12, 2026
8 checks passed
@paralin
paralin deleted the feat/attached-instance-release branch August 12, 2026 08:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant