Skip to content

Commit 04c4314

Browse files
fix(server): access list keeps changes made while its stream opens
subscribeAuthAccess read its snapshot before subscribing to the pairing link and session change streams. Those streams subscribe to non-replaying PubSubs only when consumed, so a link created or removed, or a session revoked, while the snapshot loaded never reached the client. Subscribe to each store into a buffer before reading the snapshot, as the server lifecycle stream already does. Changes are idempotent upserts and removals, so one that is also in the snapshot is safe to replay. Fixes #35623 AI-assisted; verified with the tests and tools listed above.
1 parent 8549556 commit 04c4314

2 files changed

Lines changed: 31 additions & 17 deletions

File tree

‎apps/server/src/ws.test.ts‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,20 +1158,19 @@ it.effect("access changes made while the snapshot loads reach the client after i
11581158
);
11591159
const revokedElsewhere = AuthSessionId.make("revoked-elsewhere");
11601160
const responses = yield* Queue.unbounded<AuthAccessStreamEvent>();
1161-
const server = yield* RpcServer.makeNoSerialization(
1162-
WsRpcGroup.omit(
1163-
...[...WsRpcGroup.requests.keys()].filter((tag) => tag !== WS_METHODS.subscribeAuthAccess),
1164-
),
1165-
{
1166-
...WS_RPC_SERVER_OPTIONS,
1167-
onFromServer: (response) =>
1168-
response._tag === "Chunk"
1169-
? Effect.forEach(response.values, (event) => Queue.offer(responses, event), {
1161+
const server = yield* RpcServer.makeNoSerialization(WsRpcGroup, {
1162+
...WS_RPC_SERVER_OPTIONS,
1163+
onFromServer: (response) =>
1164+
response._tag === "Chunk"
1165+
? Effect.forEach(
1166+
response.values,
1167+
(event) => Queue.offer(responses, event as AuthAccessStreamEvent),
1168+
{
11701169
discard: true,
1171-
})
1172-
: Effect.void,
1173-
},
1174-
).pipe(
1170+
},
1171+
)
1172+
: Effect.void,
1173+
}).pipe(
11751174
Effect.provide(
11761175
layerWsRpc(
11771176
{

‎apps/server/src/ws.ts‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3424,13 +3424,28 @@ export const layerWsRpc = (
34243424
[WS_METHODS.subscribeAuthAccess]: (_input) =>
34253425
Stream.unwrap(
34263426
Effect.gen(function* () {
3427+
const liveBuffer = yield* Queue.unbounded<
3428+
PairingGrantStore.BootstrapCredentialChange | SessionStore.SessionCredentialChange
3429+
>();
3430+
// Each store subscribes only when consumed, so start both before the snapshot read.
3431+
yield* Effect.forkScoped(
3432+
Stream.runForEach(bootstrapCredentials.streamChanges, (change) =>
3433+
Queue.offer(liveBuffer, change),
3434+
),
3435+
{ startImmediately: true },
3436+
);
3437+
yield* Effect.forkScoped(
3438+
Stream.runForEach(sessions.streamChanges, (change) =>
3439+
Queue.offer(liveBuffer, change),
3440+
),
3441+
{ startImmediately: true },
3442+
);
34273443
const initialSnapshot = yield* loadAuthAccessSnapshot();
34283444
const revisionRef = yield* Ref.make(1);
3429-
const accessChanges: Stream.Stream<
3430-
PairingGrantStore.BootstrapCredentialChange | SessionStore.SessionCredentialChange
3431-
> = Stream.merge(bootstrapCredentials.streamChanges, sessions.streamChanges);
34323445

3433-
const liveEvents: Stream.Stream<AuthAccessStreamEvent> = accessChanges.pipe(
3446+
const liveEvents: Stream.Stream<AuthAccessStreamEvent> = Stream.fromQueue(
3447+
liveBuffer,
3448+
).pipe(
34343449
Stream.mapEffect((change) =>
34353450
Ref.updateAndGet(revisionRef, (revision) => revision + 1).pipe(
34363451
Effect.map((revision) =>

0 commit comments

Comments
 (0)