FG
๐Ÿ’ป Software๐Ÿ“ก Networking

Default uWebSocket Engine in 2.0.1 causing 100% CPU spin on server

Fresh3 days ago
Mar 14, 20260 views
Confidence Score55%
55%

Problem

I want to: [x] report a bug [ ] request a feature Current behaviour Version 2.0.1 exhibits a problem for me when using the default engine: uWebSockets, pushing the Node.js server process into 100% CPU Steps to reproduce (if the current behaviour is a bug) I get the problem consistently if I have an application running in the browser with a web-socket connection in place. I then reload the application in the browser (which forces the old socket to be removed and a new one put in place). At this point the server goes into a spinning loop, eating 100% of the CPU. Replacing the engine with "ws" fixes the issue. Setup - OS: Ubuntu 16.04 - browser: Chrome 57 - socket.io version: 2.0.1 Other information (e.g. stacktraces, related issues, suggestions how to fix) Here's what node debug reports when I pause during 100% CPU utilisation: ubuntu@ip-172-30-1-57:~/qewd$ node debug localhost:7000 connecting to localhost:7000 ... ok debug> pause break in node_modules/uws/uws.js:8 6 const DEFAULT_PAYLOAD_LIMIT = 16777216; 7 > 8 function noop() {} 9 10 function abortConnection(socket, code, name) { debug> It appears to be stuck in a loop calling this function

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Replace uWebSocket Engine with ws to Prevent CPU Spin

Medium Risk

The uWebSocket engine in version 2.0.1 has a known issue where it enters a spinning loop when a WebSocket connection is abruptly closed and a new one is established. This behavior leads to 100% CPU utilization due to the engine continuously attempting to manage the socket lifecycle without properly handling the closure and re-establishment of connections.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Identify the WebSocket Engine Configuration

    Locate the part of your application where the WebSocket engine is defined, typically in the server setup code.

    javascript
    const io = require('socket.io')(server, { engine: { name: 'uws' } });
  2. 2

    Change the WebSocket Engine to ws

    Modify the WebSocket engine configuration to use 'ws' instead of 'uws'. This change will prevent the CPU spinning issue by utilizing a more stable WebSocket implementation.

    javascript
    const io = require('socket.io')(server, { engine: { name: 'ws' } });
  3. 3

    Test the Application

    Run your application and perform the same steps that previously caused the CPU spin. Reload the application in the browser and observe the server's CPU usage.

    none
    Open the application in Chrome and reload the page.
  4. 4

    Monitor CPU Usage

    Use system monitoring tools to check the CPU usage of the Node.js process. Ensure that it remains stable and does not spike to 100%.

    bash
    top or htop
  5. 5

    Review Application Logs

    Check the application logs for any errors or warnings related to WebSocket connections. Confirm that there are no unexpected behaviors during the connection lifecycle.

    bash
    tail -f /path/to/your/logfile.log

Validation

Confirm that the CPU usage remains below 20% during normal operation and that there are no errors in the logs related to WebSocket connections after performing the above steps.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

socket.iowebsocketrealtime