Skip to content

createBroker never get called and websocket is always failing, ETIMEDOUT happened instead of ECONNREFUSED #3918

@kambing86

Description

@kambing86

Describe the bug

websocket is failing all the time and the dts hot reloading is not working as expected

below is the typesGenerate.log when running FEDERATION_DEBUG=true pnpm start in module-federation-examples/react-manifest-example

[2025-07-16T11:11:27.094] [INFO] forkDevWorker - ChildProcess(71921), message: {"type":"mf_call","id":"76e34ae3-410e-4ebf-8dcf-d1eb2098b34e","args":[{"name":"host","remote":{"context":"/Users/pckm0640/Projects/module-federation-examples/react-manifest-example/host","moduleFederationConfig":{"name":"host","remotes":{"remote1":"remote1@http://localhost:3001/mf-manifest.json","remote2":"remote2@http://localhost:3002/mf-manifest.json"},"shared":["react","react-dom"],"implementation":"/Users/pckm0640/Projects/module-federation-examples/react-manifest-example/node_modules/.pnpm/@[email protected]/node_modules/@module-federation/runtime-tools/dist/index.cjs.js","runtimePlugins":["/Users/pckm0640/Projects/module-federation-examples/react-manifest-example/node_modules/.pnpm/@[email protected][email protected]/node_modules/@module-federation/dts-plugin/dist/dynamic-remote-type-hints-plugin.js"]},"hostRemoteTypesFolder":"@mf-types","compileInChildProcess":true,"typesFolder":".dev-server"},"host":{"context":"/Users/pckm0640/Projects/module-federation-examples/react-manifest-example/host","moduleFederationConfig":{"name":"host","remotes":{"remote1":"remote1@http://localhost:3001/mf-manifest.json","remote2":"remote2@http://localhost:3002/mf-manifest.json"},"shared":["react","react-dom"],"implementation":"/Users/pckm0640/Projects/module-federation-examples/react-manifest-example/node_modules/.pnpm/@[email protected]/node_modules/@module-federation/runtime-tools/dist/index.cjs.js","runtimePlugins":["/Users/pckm0640/Projects/module-federation-examples/react-manifest-example/node_modules/.pnpm/@[email protected][email protected]/node_modules/@module-federation/dts-plugin/dist/dynamic-remote-type-hints-plugin.js"]},"typesFolder":"@mf-types","abortOnError":false,"consumeAPITypes":true},"extraOptions":{},"disableLiveReload":false,"disableHotTypesReload":false}]} 
[2025-07-16T11:11:27.116] [INFO] forkDevWorker - host module generateTypes done, localServerAddress:  http://10.188.104.57:62341
[2025-07-16T11:11:27.118] [INFO] Module Federation Dev Server - Publisher:host Trying to connect to ws://10.188.104.57:16322...
[2025-07-16T11:11:28.777] [INFO] forkDevWorker - ChildProcess(71921), message: {"type":"mf_call","id":"76e34ae3-410e-4ebf-8dcf-d1eb2098b34e","args":[null,"update"]} 
[2025-07-16T11:11:28.784] [INFO] consumeTypes - updateTypes options:, {
  "updateMode": "POSITIVE",
  "remoteName": "host"
}
[2025-07-16T11:11:28.780] [INFO] forkDevWorker - remoteModule host receive devWorker update, start typesManager.updateTypes 
[2025-07-16T11:12:42.139] [FATAL] Module Federation Dev Server - websocket error: Error: connect ETIMEDOUT 10.188.104.57:16322
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16)
[2025-07-16T11:12:42.142] [INFO] Module Federation Dev Server - start reconnecting to server after 1s.
[2025-07-16T11:12:42.141] [WARN] Module Federation Dev Server - Connection closed with code 1006.
[2025-07-16T11:12:43.145] [INFO] Module Federation Dev Server - Publisher:host Trying to connect to ws://10.188.104.57:16322...
[2025-07-16T11:13:58.154] [FATAL] Module Federation Dev Server - websocket error: Error: connect ETIMEDOUT 10.188.104.57:16322
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16)

it should not block the code to createBroker in https://github.com/module-federation/core/blob/cce6a94/packages/dts-plugin/src/server/DevServer.ts#L414-L455 when there is ETIMEDOUT error for websocket

private _tryCreateBackgroundBroker(err: any): void {
    if (
      !(
        err?.code === 'ECONNREFUSED' && // <-- why only allow `ECONNREFUSED`, in my case here it is timeout `ETIMEDOUT`
        err.port === Broker.DEFAULT_WEB_SOCKET_PORT
      )
    ) {
      fileLog(`websocket error: ${err.stack}`, MF_SERVER_IDENTIFIER, 'fatal');
      return; // <-- why return here?
    }
    fileLog(
      `Failed to connect to ws://${this._ip}:${Broker.DEFAULT_WEB_SOCKET_PORT}...`,
      MF_SERVER_IDENTIFIER,
      'fatal',
    );
    this._isReconnecting = true;
    setTimeout(
      () => {
        this._isReconnecting = false;
        if (this._reconnect === false) {
          return;
        }
        fileLog(
          'Creating new background broker...',
          MF_SERVER_IDENTIFIER,
          'warn',
        );
        const broker = createBroker();

        broker.on('message', (message) => {
          if (message === 'ready') {
            fileLog('background broker started.', MF_SERVER_IDENTIFIER, 'info');
            this._reconnectTimes = 1;
            if (process.send) {
              process.send('ready');
            }
          }
        });
      },
      Math.ceil(100 * Math.random()),
    );
  }

NOTE: I did commented out the return; at https://github.com/module-federation/core/blob/cd59ae5/packages/dts-plugin/src/server/DevServer.ts#L422 and try in my local, and everything will work just fine

Reproduction

https://github.com/module-federation/module-federation-examples/tree/master/react-manifest-example

Used Package Manager

pnpm

System Info

System:
    OS: macOS 15.5
    CPU: (8) arm64 Apple M1
    Memory: 366.84 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.20.4 - ~/.nvm/versions/node/v18.20.4/bin/node
    npm: 10.7.0 - ~/.nvm/versions/node/v18.20.4/bin/npm
    pnpm: 9.4.0 - ~/.nvm/versions/node/v24.2.0/bin/pnpm
    bun: 1.1.30 - ~/Library/pnpm/bun
  Browsers:
    Chrome: 138.0.7204.101
    Edge: 138.0.3351.83
    Safari: 18.5

Validations

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