Skip to content

Node 10: close event is still emitted on normal HTTP response #25294

Description

@borisovg

According to #21063 and #21809 in Node v10 HTTP response should not emit 'close' if 'end' was emitted, but it still does:

'use strict';

const http = require('http');

const server = http.createServer(function (req, res) {
    res.end('OK');
});

console.log(process.versions);

server.listen(8080, function () {
    const req = http.get('http://localhost:8080/', function (res) {
        res.on('data', function (chunk) {
            console.log(`DATA: ${chunk}`);
        });

        res.on('close', function () {
            console.log('CLOSE');
        });

        res.on('end', function () {
            console.log('END');
            server.close();
        });
    });

    req.end();
});

output:

{ http_parser: '2.8.0',
  node: '10.15.0',
  v8: '6.8.275.32-node.45',
  uv: '1.23.2',
  zlib: '1.2.11',
  ares: '1.15.0',
  modules: '64',
  nghttp2: '1.34.0',
  napi: '3',
  openssl: '1.1.0j',
  icu: '62.1',
  unicode: '11.0',
  cldr: '33.1',
  tz: '2018e' }
DATA: OK
END
CLOSE

Activity

  1. mscdex commented on Dec 31, 2018

    @mscdex
    Contributor

    res in this case is an IncomingMessage (a Readable stream), which is documented as having a 'close' event. Also FWIW in #21063 the res there is a ServerResponse (a Writable stream), which is a different issue.

  2. borisovg commented on Dec 31, 2018

    @borisovg
    Author

    @mscdex you are correct

    This is the output in Node v8:

    { http_parser: '2.8.1',
      node: '8.11.4',
      v8: '6.2.414.54',
      uv: '1.20.2',
      zlib: '1.2.11',
      ares: '1.14.0',
      modules: '57',
      nghttp2: '1.32.0',
      napi: '3',
      openssl: '1.0.2o',
      icu: '60.1',
      unicode: '10.0',
      cldr: '32.0',
      tz: '2017c' }
    DATA: OK
    END
    

    Change in behaviour is the same.

  3. borisovg commented on Dec 31, 2018

    @borisovg
    Author

    OK, read the docs some more and changed my code to avoid this issue entirely.

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