module-federation / core

Module Federation is a concept that allows developers to share code and resources across multiple JavaScript applications
https://module-federation.io/
MIT License
1.27k stars 182 forks source link

Using the `revalidate` method to trigger hot reload can cause service crashes #2670

Open shixianqin opened 6 days ago

shixianqin commented 6 days ago

Describe the bug

When I use the example React-18-server-2-server for debugging, touching a hot update will cause the service to crash and prompt port :3000 is occupied. Is my posture incorrect?

// react-18-server-2-server/app1/src/server/server-entry.ts

import { revalidate } from '@module-federation/node/utils'

export default () => async (req, res, next) => {
  await revalidate().then((shouldUpdate) => {
    console.log({ shouldUpdate })
    if (shouldUpdate) {
      // reload
    }
  })

  const renderer = (await import('./render')).default
  return renderer(req, res, next)
};

Error messages 👇:

app2 hash is different - must hot reload server
@babel/polyfill is loaded more than once on this page. This is probably not desirable/intended and may have consequences if different versions of the polyfills are applied sequentially. If you do need to load the polyfill more than once, use @babel/polyfill/noConflict instead to bypass the warning.
node:events:496
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (node:net:1872:16)
    at listenInCluster (node:net:1920:12)
    at Server.listen (node:net:2008:7)
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1899:8)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  code: 'EADDRINUSE',
  errno: -48,
  syscall: 'listen',
  address: '::',
  port: 3000
}

Reproduction

https://github.com/shixianqin/react-18-server-2-server

Used Package Manager

pnpm

System Info

System:
    OS: macOS 14.5
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 187.31 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.1 - /usr/local/bin/node
    npm: 10.2.4 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  Browsers:
    Chrome: 126.0.6478.126
    Safari: 17.5

Validations

ScriptedAlchemy commented 6 days ago

it depends on how its configured if render it attempting to use the port again then it will crash.

ScriptedAlchemy commented 5 days ago

Ill look at the example, its quite old so probbably needs to be redone. We are about to release SSR federation support in modernjs https://modernjs.dev/en/ so if you are looking at rolling your own ssr, this is going to be a much less painful solution with less pitfalls like waterfall or css flushing

shixianqin commented 3 days ago

Is there a detailed or up-to-date example of the revalidate method?