feat(inmem): finalize attached controllers on release - #206
Merged
Conversation
paralin
force-pushed
the
feat/attached-instance-release
branch
from
August 12, 2026 08:10
20a7441 to
96dc5bd
Compare
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
force-pushed
the
feat/attached-instance-release
branch
from
August 12, 2026 08:20
96dc5bd to
9963220
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.