facebook / react

The library for web and native user interfaces.
https://react.dev
MIT License
225.37k stars 45.96k forks source link

Bug: promises passed to server actions block on promise parameters #28510

Open AaronFriel opened 4 months ago

AaronFriel commented 4 months ago

React version: 18.2.0

Steps To Reproduce

  1. Create a server action that takes a promise parameter p:

    "use server";
    
    export async function serverAction(p: Promise<void>) {
     console.log(p);
     const t1 = new Date();
    
     await p;
    
     const t2 = new Date();
    
     console.log("elapsed", t2.valueOf() - t1.valueOf());
     return {
       t1,
       t2,
     };
    }
  2. Pass new Promise((r) => setTimeout(r, 10_000)) to that server action.
  3. Expect that the action begins running immediately, then blocks on the promise, and reports an elapsed time of approximately 10 seconds.

Link to code example:

https://codesandbox.io/p/devbox/next-14-0-3-forked-7q86s7?file=%2Fapp%2Factions.ts%3A4%2C18&workspaceId=958ee911-ec8f-4ca3-9ce1-668005ecaeb8

The current behavior

Serialization of promises is synchronous and blocking.

The expected behavior

That a promise could be passed to a server action and resolved asynchronously, e.g., as a cancellation token.

github-actions[bot] commented 1 month ago

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

AaronFriel commented 1 month ago

Would really love someone to take a look at this! It's pretty surprising given the serialization on the return path.