Repository navigation
fix(server): keep remote connections alive during recovery #34190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
juliusmarminge
wants to merge
4
commits into
main
Choose a base branch
from
factory/pr-25540
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+315
−24
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a650a9b
fix(server): keep remote connections alive during recovery
maria-rcks 0e2ddd2
fix(web): preserve the splash while recovery holds startup
maria-rcks 91e8e07
Merge remote-tracking branch 'origin/main' into maria/takeover-pr-25053
juliusmarminge cc3b342
refactor(server): drop the removed mint route and give readiness test…
juliusmarminge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import { expect, it } from "@effect/vitest"; | ||
| import { WS_METHODS } from "@t3tools/contracts"; | ||
| import * as Deferred from "effect/Deferred"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Fiber from "effect/Fiber"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as Ref from "effect/Ref"; | ||
| import * as Schema from "effect/Schema"; | ||
| import * as Stream from "effect/Stream"; | ||
| import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http"; | ||
| import { Rpc, RpcGroup, RpcTest } from "effect/rpc"; | ||
|
|
||
| import * as ServerReadiness from "./serverReadiness.ts"; | ||
| import * as ServerRuntimeStartup from "./serverRuntimeStartup.ts"; | ||
|
|
||
| it.effect.each([ | ||
| ["GET", "/.well-known/t3/environment"], | ||
| ["GET", "/ws"], | ||
| ] as const)("serves %s %s while recovery holds commands", ([method, url]) => | ||
| Effect.gen(function* () { | ||
| const waitedForCommands = yield* Deferred.make<void>(); | ||
| const handler = yield* HttpRouter.toHttpEffect( | ||
| HttpRouter.add(method, url, HttpServerResponse.empty()).pipe( | ||
| Layer.provide(ServerReadiness.layerHttp), | ||
| Layer.provide( | ||
| Layer.mock(ServerRuntimeStartup.ServerRuntimeStartup)({ | ||
| awaitConnectionReady: Effect.void, | ||
| awaitCommandReady: Deferred.succeed(waitedForCommands, undefined).pipe( | ||
| Effect.andThen(Effect.never), | ||
| ), | ||
| }), | ||
| ), | ||
| ), | ||
| ); | ||
| const result = yield* Effect.raceFirst( | ||
| handler.pipe( | ||
| Effect.provideService( | ||
| HttpServerRequest.HttpServerRequest, | ||
| HttpServerRequest.fromWeb( | ||
| new Request(`http://localhost${url}?orchestrationProtocolVersion=2`, { method }), | ||
| ), | ||
| ), | ||
| Effect.as("answered"), | ||
| ), | ||
| Deferred.await(waitedForCommands).pipe(Effect.as("blocked")), | ||
| ); | ||
| expect(result).toBe("answered"); | ||
| }).pipe(Effect.scoped), | ||
| ); | ||
|
|
||
| it.effect.each([ | ||
| ["GET", "/api/orchestration/shell"], | ||
| ["POST", "/ws"], | ||
| ] as const)("holds %s %s until recovery completes", ([method, url]) => | ||
| Effect.gen(function* () { | ||
| const commands = yield* Deferred.make<void>(); | ||
| const waiting = yield* Deferred.make<void>(); | ||
| const handled = yield* Ref.make(false); | ||
| const handler = yield* HttpRouter.toHttpEffect( | ||
| HttpRouter.add( | ||
| method, | ||
| url, | ||
| Ref.set(handled, true).pipe(Effect.as(HttpServerResponse.empty())), | ||
| ).pipe( | ||
| Layer.provide(ServerReadiness.layerHttp), | ||
| Layer.provide( | ||
| Layer.mock(ServerRuntimeStartup.ServerRuntimeStartup)({ | ||
| awaitConnectionReady: Effect.void, | ||
| awaitCommandReady: Deferred.succeed(waiting, undefined).pipe( | ||
| Effect.andThen(Deferred.await(commands)), | ||
| ), | ||
| }), | ||
| ), | ||
| ), | ||
| ); | ||
| const request = yield* handler.pipe( | ||
| Effect.provideService( | ||
| HttpServerRequest.HttpServerRequest, | ||
| HttpServerRequest.fromWeb(new Request(`http://localhost${url}`, { method })), | ||
| ), | ||
| Effect.forkChild, | ||
| ); | ||
| yield* Deferred.await(waiting); | ||
| expect(yield* Ref.get(handled)).toBe(false); | ||
| yield* Deferred.succeed(commands, undefined); | ||
| expect((yield* Fiber.join(request)).status).toBe(204); | ||
| expect(yield* Ref.get(handled)).toBe(true); | ||
| }).pipe(Effect.scoped), | ||
| ); | ||
|
|
||
| it.effect("answers connection RPCs while application calls and streams wait for recovery", () => | ||
| Effect.gen(function* () { | ||
| const commands = yield* Deferred.make<void>(); | ||
| const waiting = yield* Deferred.make<void>(); | ||
| const handled = yield* Ref.make(0); | ||
| const group = RpcGroup.make( | ||
| Rpc.make(WS_METHODS.serverProbe, { success: Schema.String }), | ||
| Rpc.make(WS_METHODS.subscribeServerConfig, { success: Schema.String, stream: true }), | ||
| Rpc.make("application.write", { success: Schema.Number }), | ||
| Rpc.make("application.stream", { success: Schema.Number, stream: true }), | ||
| ).middleware(ServerReadiness.RpcCommandReadiness); | ||
| const client = yield* RpcTest.makeClient(group).pipe( | ||
| Effect.provide( | ||
| Layer.mergeAll( | ||
| group.toLayer({ | ||
| [WS_METHODS.serverProbe]: () => Effect.succeed("alive"), | ||
| [WS_METHODS.subscribeServerConfig]: () => Stream.make("config"), | ||
| "application.write": () => Ref.updateAndGet(handled, (count) => count + 1), | ||
| "application.stream": () => | ||
| Stream.fromEffect(Ref.updateAndGet(handled, (count) => count + 1)), | ||
| }), | ||
| ServerReadiness.layerRpc.pipe( | ||
| Layer.provide( | ||
| Layer.mock(ServerRuntimeStartup.ServerRuntimeStartup)({ | ||
| awaitCommandReady: Deferred.succeed(waiting, undefined).pipe( | ||
| Effect.andThen(Deferred.await(commands)), | ||
| ), | ||
| }), | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
| const write = yield* client["application.write"]().pipe(Effect.forkChild); | ||
| const stream = yield* client["application.stream"]().pipe(Stream.runCollect, Effect.forkChild); | ||
| yield* Deferred.await(waiting); | ||
| expect(yield* client[WS_METHODS.serverProbe]()).toBe("alive"); | ||
| expect(yield* client[WS_METHODS.subscribeServerConfig]().pipe(Stream.runCollect)).toEqual([ | ||
| "config", | ||
| ]); | ||
| expect(yield* Ref.get(handled)).toBe(0); | ||
| yield* Deferred.succeed(commands, undefined); | ||
| yield* Fiber.join(write); | ||
| yield* Fiber.join(stream); | ||
| expect(yield* Ref.get(handled)).toBe(2); | ||
| }).pipe(Effect.scoped), | ||
| ); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { WS_METHODS } from "@t3tools/contracts"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Layer from "effect/Layer"; | ||
| import { HttpRouter, HttpServerRequest } from "effect/http"; | ||
| import * as RpcMiddleware from "effect/rpc/RpcMiddleware"; | ||
|
|
||
| import * as ServerRuntimeStartup from "./serverRuntimeStartup.ts"; | ||
|
|
||
| const connectionHttpRoutes: ReadonlySet<string> = new Set([ | ||
| "GET /.well-known/t3/environment", | ||
| "GET /api/auth/session", | ||
| "POST /api/auth/browser-session", | ||
| "POST /oauth/token", | ||
| "POST /api/auth/websocket-ticket", | ||
| "POST /api/auth/websocket-ticket/rpc", | ||
| "POST /api/t3-connect/health", | ||
| "POST /api/t3-connect/mint-credential", | ||
| "GET /ws", | ||
| ]); | ||
|
|
||
| export const layerHttp = HttpRouter.middleware( | ||
| Effect.gen(function* () { | ||
| const startup = yield* ServerRuntimeStartup.ServerRuntimeStartup; | ||
| return (httpEffect) => | ||
| Effect.gen(function* () { | ||
| const request = yield* HttpServerRequest.HttpServerRequest; | ||
| const route = `${request.method} ${request.url.split("?", 1)[0]}`; | ||
| yield* ( | ||
| connectionHttpRoutes.has(route) ? startup.awaitConnectionReady : startup.awaitCommandReady | ||
| ).pipe(Effect.orDie); | ||
| return yield* httpEffect; | ||
| }); | ||
| }), | ||
| { global: true }, | ||
| ); | ||
|
|
||
| const connectionRpcMethods: ReadonlySet<string> = new Set([ | ||
| WS_METHODS.serverProbe, | ||
| WS_METHODS.serverGetConfig, | ||
| WS_METHODS.subscribeServerConfig, | ||
| WS_METHODS.subscribeServerLifecycle, | ||
| ]); | ||
|
|
||
| export class RpcCommandReadiness extends RpcMiddleware.Service<RpcCommandReadiness>()( | ||
| "t3/server/RpcCommandReadiness", | ||
| ) {} | ||
|
|
||
| export const layerRpc = Layer.effect( | ||
| RpcCommandReadiness, | ||
| Effect.gen(function* () { | ||
| const startup = yield* ServerRuntimeStartup.ServerRuntimeStartup; | ||
| return RpcCommandReadiness.of((effect, { rpc }) => | ||
| connectionRpcMethods.has(rpc._tag) | ||
| ? effect | ||
| : startup.awaitCommandReady.pipe(Effect.orDie, Effect.andThen(effect)), | ||
| ); | ||
| }), | ||
| ); |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shutdown does not fail
connectionReady.The shutdown finalizer fails the command gate with a "shutting down" error, so command waiters unblock. It does not touch
connectionReady. If the scope closes beforehttpListeningis set, the startup fiber is interrupted, and theExit.isSuccesshandler never runs.connectionReadythen stays pending. AnyawaitConnectionReadycaller keeps waiting. Callers include the allowlisted HTTP routes inServerReadiness.layerHttp.Fail
connectionReadyin the same finalizer.Deferred.failon an already-completed deferred has no effect, so this is safe after a normal start.Proposed change
yield* Effect.addFinalizer(() => Effect.gen(function* () { - yield* commandGate.failCommandReady( - new ServerRuntimeStartupError({ - mode: serverConfig.mode, - host: serverConfig.host ?? null, - port: serverConfig.port, - cause: "Server runtime is shutting down.", - }), - ); + const shutdownError = new ServerRuntimeStartupError({ + mode: serverConfig.mode, + host: serverConfig.host ?? null, + port: serverConfig.port, + cause: "Server runtime is shutting down.", + }); + yield* Deferred.fail(connectionReady, shutdownError); + yield* commandGate.failCommandReady(shutdownError);