Skip to content

Readable event triggers but socket.readableLength returns zero #40136

Description

@davidkjackson54

Version

16.4.2

Platform

Windows 10

Subsystem

No response

What steps will reproduce the bug?

I am reading specific lengths of data off the readable queue. I check the readableLength to determine how much data I can read.
However, there are occasions when the readable event triggers but the readableLength is returned as zero.

client._socket.on("readable", () => {
        console.log("Readable event triggered");
        console.log(
          `${new Date().toLocaleString()}, Readable Stream has ${client._socket.readableLength} bytes of data available`
        );
        if (client._socket.readableLength === 0) {
          // for some reason the readable event can trigger but there be nothing in the readable queue
          console.log("readable queue is empty");
          return;
        }

How often does it reproduce? Is there a required condition?

This typically happens when TCP has decided to chunk the return of the data. I read what is available then wait for the next readable event to fire, then I find that the readableLength is zero - despite the readable event having triggered.

What is the expected behavior?

readableLength should contain the number of bytes available in the stream after the readable event has triggered.

What do you see instead?

Readable event triggered
9/17/2021, 7:47:19 AM, Readable Stream has 0 bytes of data available
readable queue is empty

Additional information

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to Node.js documentation.good first issueIssues that are suitable for first-time contributors.netIssues and PRs related to the net subsystem.streamIssues and PRs related to Node.js streams.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions