Skip to content

https proxy reattaches listeners without removing them #62904

Description

@ChALkeR

This seems minor, but it can trigger MaxListenersExceededWarning on proxy connections

Image

https://github.com/nodejs/node/blob/HEAD/lib/https.js#L224

This can be observed with this change on the proxy side to trigger:

--- a/test/common/proxy-server.js
+++ b/test/common/proxy-server.js
@@ -82,7 +82,11 @@ function createProxyServer(options = {}) {
 
     const normalizedHostname = hostname.startsWith('[') && hostname.endsWith(']') ?
       hostname.slice(1, -1) : hostname;
-    const proxyReq = net.connect(port, normalizedHostname, () => {
+    const proxyReq = net.connect(port, normalizedHostname, async () => {
+      const first = 'HTTP/1.1 200 Connection Established'
+      const slow = 5
+      for (let i = 0; i < slow; i++) {
+        res.write(first[i])
+        await new Promise(a => setTimeout(a, 1))
+      }
       res.write(
-        'HTTP/1.1 200 Connection Established\r\n' +
+        first.slice(slow) + '\r\n' +
         'Proxy-agent: Node.js-Proxy\r\n' +

Results in

(node:68634) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 readable listeners added to [Socket]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit
    at genericNodeError (node:internal/errors:998:15)
    at wrappedFn (node:internal/errors:543:14)
    at _addListener (node:events:590:17)
    at Socket.addListener (node:events:608:10)
    at Readable.on (node:internal/streams/readable:1136:35)
    at Socket.read (node:https:224:12)
    at Socket.emit (node:events:521:24)
    at emitReadable_ (node:internal/streams/readable:837:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:89:21)

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

    httpsIssues and PRs related to the https subsystem.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions