Skip to content
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

Incorrect data is returned when using multiple clients #201

Open
BrendanC23 opened this issue Jul 2, 2024 · 0 comments
Open

Incorrect data is returned when using multiple clients #201

BrendanC23 opened this issue Jul 2, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@BrendanC23
Copy link

There is a race condition that causes incorrect data to be returned if there are two different clients. I have two different TRPC clients. The first is a regular client.

function App() {
    const [queryClient] = useState(() => new QueryClient());
    const trpcClient = trpc.createClient({
        links: [loggerLink(), ipcLink()],
        transformer: superjson,
    });

    return (
        <trpc.Provider client={trpcClient} queryClient={queryClient}>
            <QueryClientProvider client={queryClient}>
            </QueryClientProvider>
        </trpc.Provider>
    );
}

The second is a proxy client.

export const trpc = createTRPCReact<AppRouter>();

export const trpcProxyClient = createTRPCProxyClient<AppRouter>({
    links: [loggerLink(), ipcLink()],
    transformer: superjson,
});

I can use the regular client in components, doing things like const result = trpc.path.getData.useQuery(). The proxy client is used in TanStack Router route loaders, as it can be awaited.

export const Route = createRootRouteWithContext<RouterContext>()({
    loader: async ({ context }) => {
        const data = await context.trpcProxyClient.path.getData.query();
        return data;
    },
    component: RootComponent,
});

This causes an issue because the two TRPC clients cause there to be two instances of onMessage.

this.#electronTRPC.onMessage((response: TRPCResponseMessage) => {
this.#handleResponse(response);
});

Each electron-trpc client will have a separate request Operation id.

https://github.com/trpc/trpc/blob/48de686d22ccbefe1cf97eaa7f205dabe7302d2a/packages/client/src/internals/TRPCUntypedClient.ts#L79-L88

Thus, it is possible for the wrong data to be returned in some circumstances. Consider the following:

  1. trpcClient (client 1) is created. It's requestId = 0. An onMessage handler is added.
  2. trpcProxyClient (client 2) is created. It's requestId = 0. An onMessage handler is added.
  3. trpcClient fetches data using requestId = 1.
  4. trpcProxyClient fetches data using requestId = 1.
  5. trpcClient's request completes. Both trpcClient and trpcProxyClient have their onMessage callback trigger with id = 1.
  6. trpcClient fullfils the request with the correct data.
  7. trpcProxyClient's onMessage was called with id = 1, and so it returns incorrect data back to the caller.
  8. trpcProxyClient's request finishes, but #handleResponse has already been run on incorrect data. The correct response is discarded.
@jsonnull jsonnull added the bug Something isn't working label Dec 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants