ws icon indicating copy to clipboard operation
ws copied to clipboard

Empty unmasked frames are written as a separate zero-length write, which fails with EFAULT on Electron on some arm64 kernels

Open OrcaWin opened this issue 1 month ago • 2 comments

Is there an existing issue for this?

  • [x] I've searched for any related issues and avoided creating a duplicate issue.

Description

Sender.sendFrame() writes an unmasked frame as two socket writes, the header and then the payload, even when the payload is empty:

sendFrame(list, cb) {
  if (list.length === 2) {
    this._socket.cork();
    this._socket.write(list[0]);
    this._socket.write(list[1], cb);
    this._socket.uncork();
  } else {
    this._socket.write(list[0], cb);
  }
}

On Electron (43.7.9 and 44.7.0) on arm64 Linux kernels with a 39-bit address space, such as a Raspberry Pi 4 on the 64-bit Raspberry Pi OS kernel, net.Socket#write() of a zero-length Buffer fails with EFAULT and destroys the socket. That's a runtime bug, and I've reported it at electron/electron#54742. Until it's fixed, though, every empty server-side ping, pong, message or close() without a code, including the automatic pong to an empty client ping, kills the connection with 1006. With a heartbeat, connections drop on every interval.

Would you accept skipping the payload write when the payload is empty?

if (list.length === 2 && list[1].length !== 0) {

The header already encodes length 0, so the bytes on the wire are the same, and it saves a write per empty frame. We ship this as a patch downstream (stablyai/orca#26200), with a loopback test that covers an empty ping plus the auto-pong, the server's auto-pong to an empty client ping, an empty pong, an empty text frame (callback fires exactly once), and close() with no code ending in 1005. I'm happy to open a PR with a test if this is welcome.

In my tests, empty frames sent by the client (masked) were not affected.

Reproduction steps

Kernel needed: arm64 Linux with CONFIG_ARM64_VA_BITS=39. I reproduced it in a Debian 13 arm64 VM with Debian's 6.12.111 kernel rebuilt with a 39-bit VA. It was first seen on a Raspberry Pi 4 (6.18.39+rpt-rpi-v8). The stock 52-bit kernel in the same VM does not reproduce it. Check with:

zcat /proc/config.gz | grep VA_BITS   # CONFIG_ARM64_VA_BITS=39
tail -1 /proc/self/maps               # [stack] ends at 0x8000000000 on a 39-bit kernel

repro.js (npm i ws):

const { WebSocketServer, WebSocket } = require('ws')

const wss = new WebSocketServer({ port: 0, host: '127.0.0.1' }, () => {
  const ws = new WebSocket(`ws://127.0.0.1:${wss.address().port}`)
  // The client ping is masked (one write); the server's automatic pong is
  // unmasked and empty, so it goes out as header + zero-length write.
  ws.on('open', () => ws.ping())
  ws.on('pong', () => { console.log('pong received'); ws.close() })
  ws.on('close', (code) => { console.log('close', code); wss.close() })
})
ELECTRON_RUN_AS_NODE=1 /path/to/electron repro.js

Expected, and what standalone Node 24.21.0 prints on the same machine:

pong received
close 1005

Electron 44.7.0 and 43.7.9:

close 1006

The server-side socket emits write EFAULT while writing the empty pong payload.

With ws 8.22.0, one fresh connection per case:

Action Electron 44.7.0 Node 24.21.0
server ping() closed (write EFAULT) open
server pong() closed (write EFAULT) open
server send('') closed (write EFAULT) open
client ping() (server auto-pong) closed (write EFAULT) open
server close() with no code client sees 1006 (write EFAULT) client sees 1005
client pong() open open
client send('') open open
client send(Buffer.alloc(0)) open open

The same thing without ws:

const net = require('net')
const srv = net.createServer((s) => s.resume()).listen(0, '127.0.0.1', () => {
  const s = net.connect(srv.address().port, '127.0.0.1', () => {
    s.on('error', (e) => console.log('ERROR', e.code)) // ERROR EFAULT under Electron
    s.cork(); s.write(Buffer.from([0x8a, 0x00])); s.write(Buffer.alloc(0)); s.uncork()
  })
})

ws version

8.22.0

Node.js Version

v24.21.0 (embedded in Electron 44.7.0 and 43.7.9). Standalone Node v24.21.0 is not affected.

System

Debian 13.7 arm64 VM (QEMU/HVF on Apple Silicon), kernel 6.12.111 with CONFIG_ARM64_VA_BITS=39, 4K pages. Originally reported on a Raspberry Pi 4 Model B with Raspberry Pi OS 64-bit, kernel 6.18.39+rpt-rpi-v8 (39-bit VA).

Expected result

Empty ping, pong, message and close frames are sent, and the connection stays open.

Actual result

The server socket is destroyed with write EFAULT and the peer sees close code 1006. With a 15 s server heartbeat, every connection drops every 15 s. Clients that send an empty ping are dropped within milliseconds.

Attachments

Electron issue: electron/electron#54742. Downstream reports: stablyai/orca#19876, stablyai/orca#20673, stablyai/orca#23398. Downstream patch: stablyai/orca#26200.

OrcaWin avatar Oct 08 '26 07:10 OrcaWin

Would you accept skipping the payload write when the payload is empty?

Yes, I think no test is needed as it seems an obvious optimization. Is writev() used when the issue occurs or is it not supported?

lpinca avatar Oct 08 '26 19:10 lpinca

Is writev() used when the issue occurs or is it not supported?

It is used as written in https://github.com/electron/electron/issues/54742.

lpinca avatar Oct 08 '26 19:10 lpinca