Skip to content

Server function module export not resolved for serverFn (probably some race condition) #4486

Open
@pbuszka

Description

@pbuszka

Which project does this relate to?

Start

Describe the bug

I experience a failure deep in tanstack code after starting dev mode server and running a specific page for the first time.

I did manage to narrow down the problem to this place: https://github.com/TanStack/router/blob/d672b229aefc0eed4a463485e29ef880f803622a/packages/start-server-core/src/server-functions-handler.ts#L66

but more importantly adding just before a 1ms delay

  const delay = (duration) =>
    new Promise(resolve => setTimeout(resolve, duration));
  
  await delay(1)

solves the problem which suggests some race condition or similar type of issue.

Unfortunatly, I did not manage to create a simple reproduction as it is time dependent and even the same source code placement (see observations below)

The error stack tarce:

Error: Server function module export not resolved for serverFn ID: src_server_api_routers_document-serverfn_ts--getYDocDiffs_createServerFn_handler
    at jsonPayloadAsString (/Users/pbuszka/projects/verosetta/webapp/node_modules/@tanstack/start-server-core/src/server-functions-handler.ts:79:5)
    at async eval (/Users/pbuszka/projects/verosetta/webapp/node_modules/@tanstack/start-server-core/src/createStartHandler.ts:143:20)
    at async startRequestResolver (/Users/pbuszka/projects/verosetta/webapp/node_modules/@tanstack/start-server-core/src/createStartHandler.ts:127:24)
    at async file:///Users/pbuszka/projects/verosetta/webapp/node_modules/@tanstack/start-plugin-core/dist/esm/dev-server-plugin/plugin.js:45:30

Your Example Website or App

did not succeeded as it seems to be issue depending on time

Steps to Reproduce the Bug or Issue

The problem is not reproducible in a separate repo but what I found out in my own dev environment

  1. start dev server: npm run dev
  2. go to the offending page, there are 4 calls to a speciifc serverFn on this page
  3. one of the 4 calls (in most cases the 1st) to serverFn fails with error as in descirption, subsequent calls to the same serverFn work as expected
  4. browser refresh of the page WITHOUT restart of the dev server doesn't trigger the problem again
  5. restarting dev server triggers

Another path that triggers the problem:

  1. start dev server
  2. start with a different page - it works fine. This page uses a different serverFn from the same source file (so the problem is not linked to first usage of a file with serverfns)
  3. navigate to the offending page
  4. the same problem is triggered on the same server fn as above

Expected behavior

serverfn call not failing

Screenshots or Videos

No response

Platform

  • OS: [ macOS 15.5, M1 chip]
  • Browser: [Chrome]
  • Version: [1.121.28]

Additional context

The problem never appeared before 1.121. I found it when exploring migrating to 1.121

Observations:

  • reducing the body of this server function to simple return did not solve the problem
  • adding await with 1ms delay in handleServerAction "solves" the problem (see above)
  • moving the problematic server function to a separate source file did "solve" the issue.
  • moving the serverfn source code within the same source file did not help
  • other functions from the same file execute fine on the same page
  • the offending page makes 12 serverfn calls from 3 different source files, including 4 calls to the problematic fn
  • it is linked with the first call to the specific serverfn
  • it is NOT linked to the first call of any serverfn in the same source file

My gustimate:

  • there is some dynamic import process that in some circumstances is not finished before running the code in handleServerAction
  • it probably depends somehow on the size of the source file where server fn is located
  • maybe it's somehow related with multiple calls when severfn is first used
  • it is not related to the actual body of the server function
  • it happens only once per dev server lifetime

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions