Description
Summary
Server and client sessions use an internal initialization state machine combined with various flags and manual AsyncExitStack management. Stateless mode complicates this further by setting an "Initialized" state early as a workaround.
This makes the lifecycle hard to reason about and contributes to subtle bugs (e.g., past issues like #756 in stateless mode).
Problems
- Scattered state management: Initialization and teardown logic are spread across methods and files.
- Special cases for stateless mode: Stateless HTTP sets the session as initialized even though no real protocol negotiation has occurred.
- Resource management risk: If initialization fails midway, transport tasks and resources may not be cleaned up reliably.
- Testing difficulty: Tests often bypass parts of the lifecycle using in-memory transports.
Proposal
-
Introduce an explicit session state machine
Represent distinct states as separate types, for example:
UninitializedSession
NegotiatingSession
ActiveSession
ClosedSession
StatelessSession (for per-request / ephemeral cases)
Each state exposes only the operations that are valid in that state, and transitions return the next state type.
-
Model stateless mode explicitly
- Instead of marking a stateful session as "Initialized" prematurely, model stateless HTTP as a dedicated
StatelessSession type with a simpler lifecycle.
- Make behavior differences clear in code and docs.
-
Centralize resource cleanup
- Ensure that all lifecycle paths (happy path, error, cancellation) lead through code that:
- cancels outstanding tasks,
- closes transports,
- releases resources stored in
AsyncExitStack.
Why this matters
- Reliability: Fewer edge cases and surprise states where messages can be processed incorrectly.
- Debuggability: Easier to reason about what can happen in each state.
- Extensibility: Adding new lifecycle behavior (e.g., resuming sessions) becomes more manageable.
Acceptance criteria
References
No response
Description
Summary
Server and client sessions use an internal initialization state machine combined with various flags and manual
AsyncExitStackmanagement. Stateless mode complicates this further by setting an "Initialized" state early as a workaround.This makes the lifecycle hard to reason about and contributes to subtle bugs (e.g., past issues like #756 in stateless mode).
Problems
Proposal
Introduce an explicit session state machine
Represent distinct states as separate types, for example:
UninitializedSessionNegotiatingSessionActiveSessionClosedSessionStatelessSession(for per-request / ephemeral cases)Each state exposes only the operations that are valid in that state, and transitions return the next state type.
Model stateless mode explicitly
StatelessSessiontype with a simpler lifecycle.Centralize resource cleanup
AsyncExitStack.Why this matters
Acceptance criteria
References
No response