Skip to content

Premature close error on socket under Node.js 12 #477

Description

@vweevers

This only happens with readable-stream@4 and Node.js 12. Later versions of Node, or earlier versions of readable-stream, work fine.

'use strict'

const net = require('net')
const { pipeline, PassThrough } = require('readable-stream')

const server = net.createServer(function (sock) {
  pipeline(sock, new PassThrough(), sock, function () { })
})

server.listen(3000, function () {
  const sock = net.connect(3000)

  // Change to false to avoid error
  if (true) {
    pipeline(sock, new PassThrough(), sock, (err) => {
      // NodeError: Premature close
      if (err) throw err
    })
  } else {
    sock.on('connect', function () {
      console.log('client connected')

      pipeline(sock, new PassThrough(), sock, (err) => {
        if (err) throw err
      })
    })
  }
})

Activity

  1. vweevers commented on Jul 2, 2022

    @vweevers
    ContributorAuthor

    Happens because sock.readable is initially false (before connect has been emitted) and this branch is hit:

    } else if (
    !writable &&
    (!willEmitClose || isWritable(stream)) &&
    (readableFinished || isReadable(stream) === false)
    ) {
    process.nextTick(onclose)

    Which evaluates to:

    if (
      !false &&
      (!false || true) &&
      (false || false === false)
    ) {
      process.nextTick(onclose)
    }
  2. mcollina commented on Jul 2, 2022

    @mcollina
    SponsorMember

    I'm not sure what can we do about this.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions