Repository navigation
fix(server): one failing RPC handler no longer ends the client's other requests #15515
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3fd5091
fix(server): one failing RPC handler no longer ends the client's othe…
juliusmarminge 3d26833
chore(server): keep the defect reporter constructor module-private
juliusmarminge 06944d7
fix(server): ACP requests whose handler dies get their own error
juliusmarminge f963d36
test(server): drop an unused lint directive
juliusmarminge 20fddd2
fix(effect-acp): a dying extension or notification handler no longer …
juliusmarminge 3afd764
fix(effect-acp): log handler defects at Error and terminate on reader…
juliusmarminge fc9ee16
fix(effect-acp): an interrupt inside the reader terminates the connec…
juliusmarminge 06e13f2
Merge remote-tracking branch 'origin/main' into t3code/rpc-contain-de…
juliusmarminge 15fe68c
Merge branch 'main' into t3code/rpc-contain-defects
juliusmarminge 820d0f1
Merge remote-tracking branch 'origin/main' into t3code/rpc-contain-de…
juliusmarminge 3d828a9
Merge remote-tracking branch 'origin/main' into t3code/rpc-contain-de…
juliusmarminge 6d36ef7
fix(effect-acp): a dying extension handler is never reported as its t…
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { assert, describe, it } from "@effect/vitest"; | ||
| import * as Cause from "effect/Cause"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as ErrorReporter from "effect/ErrorReporter"; | ||
| import * as Exit from "effect/Exit"; | ||
| import * as Fiber from "effect/Fiber"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as Logger from "effect/Logger"; | ||
| import * as Queue from "effect/Queue"; | ||
| import * as Schema from "effect/Schema"; | ||
| import * as Stream from "effect/Stream"; | ||
| import { Rpc, RpcClient, RpcGroup, RpcServer } from "effect/rpc"; | ||
|
|
||
| import { WS_RPC_SERVER_OPTIONS } from "../ws.ts"; | ||
| import * as DefectReporter from "./DefectReporter.ts"; | ||
|
|
||
| class TestRpcs extends RpcGroup.make( | ||
| Rpc.make("subscribe", { success: Schema.Number, stream: true }), | ||
| Rpc.make("boom", { success: Schema.Void }), | ||
| ) {} | ||
| type TestRpc = RpcGroup.Rpcs<typeof TestRpcs>; | ||
|
|
||
| /** Runs `body` with every error log captured. */ | ||
| const withErrorLogs = <A, E, R>( | ||
| body: (logs: Queue.Queue<Cause.Cause<unknown>>) => Effect.Effect<A, E, R>, | ||
| ) => | ||
| Effect.gen(function* () { | ||
| const logs = yield* Queue.unbounded<Cause.Cause<unknown>>(); | ||
| const logger = Logger.make(({ cause, logLevel }) => { | ||
| if (logLevel === "Error") Queue.offerUnsafe(logs, cause); | ||
| }); | ||
| return yield* body(logs).pipe( | ||
| Effect.provide(Logger.layer([logger], { mergeWithExisting: false })), | ||
| ); | ||
| }); | ||
|
|
||
| const errorMessage = (cause: Cause.Cause<unknown>) => (Cause.squash(cause) as Error).message; | ||
|
|
||
| describe("DefectReporter", () => { | ||
| it.effect("a dying RPC handler fails alone, and its defect is logged", () => | ||
| withErrorLogs((logs) => | ||
| Effect.gen(function* () { | ||
| const subscription = yield* Queue.unbounded<number>(); | ||
| // The same client/server pairing as RpcTest.makeClient, with ws.ts's options. | ||
| let client!: Effect.Success< | ||
| ReturnType<typeof RpcClient.makeNoSerialization<TestRpc, never>> | ||
| >; | ||
| const server = yield* RpcServer.makeNoSerialization(TestRpcs, { | ||
| ...WS_RPC_SERVER_OPTIONS, | ||
| onFromServer: (response) => client.write(response), | ||
| }).pipe( | ||
| // Provided to the handlers, as ws.ts does. | ||
| Effect.provide( | ||
| TestRpcs.toLayer({ | ||
| subscribe: () => Stream.fromQueue(subscription), | ||
| boom: () => Effect.die(new Error("handler bug")), | ||
| }).pipe(Layer.provide(DefectReporter.layer)), | ||
| ), | ||
| ); | ||
| client = yield* RpcClient.makeNoSerialization(TestRpcs, { | ||
| supportsAck: true, | ||
| onFromClient: ({ message }) => server.write(0, message), | ||
| }); | ||
|
|
||
| const received = yield* Queue.unbounded<number>(); | ||
| const sibling = yield* client.client.subscribe().pipe( | ||
| Stream.runForEach((value) => Queue.offer(received, value)), | ||
| Effect.forkScoped, | ||
| ); | ||
| yield* Queue.offer(subscription, 1); | ||
| assert.equal(yield* Queue.take(received), 1); | ||
|
|
||
| const boom = yield* Effect.exit(client.client.boom()); | ||
| assert.isTrue(Exit.hasDies(boom)); | ||
| // The server reports before it answers, so the log is already written. | ||
| const logged = yield* Queue.clear(logs); | ||
| assert.deepEqual(logged.map(errorMessage), ["handler bug"]); | ||
|
|
||
| // The sibling subscription on the same client keeps delivering. | ||
| yield* Queue.offer(subscription, 2); | ||
| const next = yield* Queue.take(received).pipe( | ||
| Effect.raceFirst(Fiber.await(sibling).pipe(Effect.as("subscription ended"))), | ||
| ); | ||
| assert.equal(next, 2); | ||
| assert.equal(yield* Queue.size(logs), 0); | ||
| }), | ||
| ).pipe(Effect.scoped), | ||
| ); | ||
|
|
||
| it.effect("logs defects, not typed failures or interrupts", () => | ||
| withErrorLogs((logs) => | ||
| Effect.gen(function* () { | ||
| yield* ErrorReporter.report(Cause.fail(new Error("expected"))); | ||
| yield* ErrorReporter.report(Cause.interrupt()); | ||
| yield* ErrorReporter.report(Cause.die(new Error("bug"))); | ||
|
|
||
| // Reporters log synchronously, so the logs are already written. | ||
| assert.deepEqual((yield* Queue.clear(logs)).map(errorMessage), ["bug"]); | ||
| }).pipe(Effect.provide(DefectReporter.layer)), | ||
| ), | ||
| ); | ||
| }); |
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,24 @@ | ||
| import * as Cause from "effect/Cause"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as ErrorReporter from "effect/ErrorReporter"; | ||
|
|
||
| /** | ||
| * Logs the defects of WebSocket RPC handlers. RpcServer reports every failed | ||
| * handler exit; typed failures are expected responses, so only dies are logged. | ||
| */ | ||
| const reporter: ErrorReporter.ErrorReporter = { | ||
| [ErrorReporter.TypeId]: ErrorReporter.TypeId, | ||
| report: ({ cause, fiber }) => { | ||
| for (const reason of cause.reasons) { | ||
| if (reason._tag !== "Die" || ErrorReporter.isIgnored(reason.defect)) continue; | ||
| // Reporters are called synchronously from the failing fiber. Logging with | ||
| // its context keeps its loggers and annotations, and a fork cannot throw | ||
| // back into the server that reported. | ||
| Effect.runForkWith(fiber.context)( | ||
| Effect.logError("Unhandled defect", Cause.fromReasons([reason])), | ||
| ); | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| export const layer = ErrorReporter.layer([reporter]); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.