Skip to content

HTTP/2 — how to listen for connection close? #16666

@yoshuawuyts

Description

@yoshuawuyts
  • Version: v8.8.1 / v9.0.0
  • Platform: Darwin
  • Subsystem: HTTP/2

Heya! I'm trying to detect when a connection closes on an HTTP/2 connection in compat mode. We're trying to keep an SSE connection going, as long as the server and client are connected. In prior versions Node 8.x we could listen for it by attaching a listener to req.on('error') and req.connection.on('disconnect'):

    res.writeHead(200, {
      'Content-Type': 'text/event-stream',
      'Cache-Control': 'no-cache'
    })
    res.write('retry: 10000\n')

    var interval = setInterval(function () {
      res.write(`id:${id++}\ndata:{ "type:": "heartbeat" }\n\n`)
    }, 4000)

    // Attach an error handler, but no need to actually handle the error.
    // This is a bug in Node core according to mcollina which will be fixed
    // in a future Node release. Let's keep this in place as long as v8.x.x of
    // Node isn't in LTS yet.
    res.on('error', disconnect)
    req.on('error', disconnect)

    req.connection.addListener('close', disconnect, false)

    function disconnect () {
      clearInterval(interval)
      if (connected) {
        emitter.removeListener('scripts:bundle', reloadScript)
        emitter.removeListener('style:bundle', reloadStyle)
        connected = false
        state.sse -= 1
        if (!quiet) render()
      }
    }

source (choojs/bankai:http.js)


Since node v8.8.x (including node v9.0.x), this code no longer works. We're having trouble listening reliably for disconnects, which sometimes causes writes to happen after the connection has occurred:

A critical error occured, forcing Bankai to abort:

Error [ERR_HTTP2_STREAM_CLOSED]: The stream is already closed
    at Http2ServerResponse.write (internal/http2/compat.js:562:19)
    at Timeout._onTimeout (/Users/anon/src/choojs/bankai/http.js:192:11)
    at ontimeout (timers.js:471:11)
    at tryOnTimeout (timers.js:306:5)
    at Timer.listOnTimeout (timers.js:266:5)

If you think this might be a bug in Bankai, please consider helping
improve Bankai's stability by submitting an error to:

  https://github.com/choojs/bankai/issues/new

Please include the steps to reproduce this error, the stack trace
printed above, your version of Node, and your version of npm. Thanks!
— Team Choo

So I'm curious how to reliably listen for connection disconnects in the latest version of HTTP/2. You can try and reproduce this by cloning the latest bankai master, running npm install and npm start. Open up a browser window, and hit f5 a bunch — at some point a write after end should occur.

Hopefully this is enough info; let me know if I can help in any way. Thanks!

cc/ @jasnell @mcollina

Ref

Metadata

Metadata

Assignees

No one assigned

    Labels

    http2Issues or PRs related to the http2 subsystem.questionIssues that look for answers.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions