0002 — Exclusive session: explicit session handle¶
ExclusiveSession gives its operation a session handle, IChannelSession<TFrame>. Only session.Send(...)
writes on the wire the session holds. The overloads that take an operation without a handle,
ExclusiveSession(Func<CancellationToken, Task<TResult>>, ct) and ExclusiveSession(Func<CancellationToken, Task>, ct),
are [Obsolete] build errors. If your adapter calls them, it does not build until you follow the steps below.
For the session model these steps refer to, see Exclusive sessions and compliance rule COMM-010.
Why it changed¶
The obsolete overloads handed out wire ownership through the async context. Every Send whose context
came from inside the session went straight to the wire. Two ordinary patterns broke that:
- A task started inside the session and not awaited (fire-and-forget) kept the context. Its
Sendcould run after the session ended, next to the next queued request. - Two sends started together (
Task.WhenAll) both went straight to the wire.
In both cases two frames could be on the wire at the same time. Most panels answer that with garbage or stop answering. The session handle closes both holes:
- Only a call on the handle reaches the wire past the queue.
- The handle sends one frame at a time. A second call waits for the first reply.
- The handle stops working when the session ends.
Fix the build error¶
Step 1 — Change the operation signature¶
Give the operation a second parameter, the session handle. In a TcpProtocolBase subclass the type is
IChannelSession<TFrame>, where TFrame is your frame class. Add using Pq.Adapters.Framework.Communication.Protocol;
where you name the type.
| Change this | To this |
|---|---|
ExclusiveSession(ct2 => Work(ct2), ct) |
ExclusiveSession((session, ct2) => Work(session, ct2), ct) |
ExclusiveSession(WorkCore, ct) with WorkCore(CancellationToken ct) |
ExclusiveSession(WorkCore, ct) with WorkCore(IChannelSession<MyFrame> session, CancellationToken ct) |
Func<CancellationToken, Task<TResult>> in your own wrapper |
Func<IChannelSession<MyFrame>, CancellationToken, Task<TResult>> |
Step 2 — Send through the session¶
Inside the operation, replace every Send(...) with session.Send(...). The handle has the same overloads
as the protocol base:
| Change this | To this |
|---|---|
Send(request, ct) |
session.Send(request, ct) |
Send(request, timeout, ct) |
session.Send(request, timeout, ct) |
Send(request, options, ct) |
session.Send(request, options, ct) |
Send(request, options, followUpFactory, ct) |
session.Send(request, options, followUpFactory, ct) |
Step 3 — Pass the session to inner methods¶
A read or enumeration method that the operation calls takes the session as a parameter and sends through it. Pass the session down every level that sends.
Before:
internal Task<TResult> RunSession<TResult>(Func<CancellationToken, Task<TResult>> operation, CancellationToken ct)
=> ExclusiveSession(operation, ct);
internal async Task<DiscoveredDevice?> ReadDeviceName(DeviceNameType type, int number, CancellationToken ct)
{
var response = await Send(CreateReadNameRequest(type, number), ct);
return Decode(response);
}
public static Task<AdapterDeviceCollection> Discover(Protocol protocol, CancellationToken ct)
=> protocol.RunSession(async leaseCt =>
{
var zone = await protocol.ReadDeviceName(DeviceNameType.Zone, 1, leaseCt);
return BuildTree(zone);
}, ct);
After:
internal Task<TResult> RunSession<TResult>(Func<IChannelSession<MyFrame>, CancellationToken, Task<TResult>> operation, CancellationToken ct)
=> ExclusiveSession(operation, ct);
internal async Task<DiscoveredDevice?> ReadDeviceName(IChannelSession<MyFrame> session, DeviceNameType type, int number, CancellationToken ct)
{
var response = await session.Send(CreateReadNameRequest(type, number), ct);
return Decode(response);
}
public static Task<AdapterDeviceCollection> Discover(Protocol protocol, CancellationToken ct)
=> protocol.RunSession(async (session, leaseCt) =>
{
var zone = await protocol.ReadDeviceName(session, DeviceNameType.Zone, 1, leaseCt);
return BuildTree(zone);
}, ct);
The build error is gone once no call to the overloads without a handle remains.
Step 4 — Check methods you call both inside and outside a session¶
A method that sends through a session handle can run only inside a session. If you also call it outside a session, wrap that call in a short session of its own:
var newest = await Protocol.RunSession((session, ct2) => Protocol.ReadEvent(session, null, ct2), ct);
What happens if you miss a Send¶
A plain Send(...) or SendBulk(...) on the same protocol, called from inside an active session, throws
InvalidOperationException with a message that says to use session.Send. The call does not hang: without
this check the queued request would wait for the session, and the session would wait for the request.
Two related cases:
- A task you started inside the session that sends with plain
Sendafter the session ended queues normally. No exception. - A session handle used after its session ended throws
ObjectDisposedException. Do not store the handle in a field or hand it to work that outlives the session.
A nested ExclusiveSession on the same protocol inside an active session runs at once and receives the same
handle.