Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.
This repository was archived by the owner on May 30, 2024. It is now read-only.

The timeout option for the client won't work because there's no listener for the event #282

@homer0

Description

@homer0

Describe the bug
The timeout in the requests is being ignored because there's no listener for the timeout event.
The problem is here. The code is listening for error, but when a request times out, it emits a timeout event, that it's being ignored, so until the server fails to respond, the request will continue.

I copied the httpRequest into an isolated scenario and tried adding the following things and it seems to work:

+  let finished = false;
   const req = http.request(allOptions, resp => {
     let body = '';
     resp.on('data', chunk => {
       body += chunk;
     });
     resp.on('end', () => {
+      if (finished) {
+        return;
+      }
       callback(null, resp, body);
     });
   });
   req.on('error', err => {
     callback(err);
   });
+  req.on('timeout', () => {
+    finished = true;
+    callback(new Error('timeout'));
+    req.end();
+  });

Yeah, it could probably use a better exception :P

To reproduce
Set the timeout to a very low value and throttle your network connection.

Expected behavior
The timeout event will be handled, the connection terminated, and the promise rejected.

SDK version
Latest

Language version, developer tools
Node 16 and 18

OS/platform
MacOS 12

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