A debugging story tracing why a PostgreSQL query with `ORDER BY random() LIMIT 10` hung while `LIMIT 9` worked fine. The root cause was a bug in undici's WebSocket implementation: when encoding a 16-bit extended payload length (triggered at exactly 126 bytes), it created a DataView over a Buffer's backing ArrayBuffer without accounting for byteOffset. Because Node's Buffer.allocUnsafe() may allocate small buffers as sub-arrays of a global pool with a non-zero byteOffset, the payload length was written to the wrong memory location — corrupting data and causing the connection to hang. The fix was to pass the correct byteOffset and byteLength to the DataView constructor, and was released in undici 5.22.1.

10m read timeFrom neon.com
Post cover image
Table of contents
WiresharkingWebSocket payload lengthsHacking on undiciBuffers, ArrayBuffers and subarraysWait, what?A happy endingPerformance: a footnote