Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http.request can throw immediately but still connect a socket, cause unhandled error #26143

Closed
zbjornson opened this issue Feb 16, 2019 · 3 comments

Comments

@zbjornson
Copy link
Contributor

  • Version: v10.15.0, v12.0.0-nightly201902158e68dc53b3
  • Platform: Win10 x64, Linux x64 (4.15.0-1027-gcp Ubuntu 16)
  • Subsystem: http

A call to http.request can throw an error immediately but still connect a socket. When that socket is closed by the other party (e.g. due to timeout), I don't think there's a way to handle the socket hangup error without digging into the http.Agent. Instead, it becomes an unhandled exception.

An edge case, but still seems like a bug. Not sure if there are other ways to cause exceptions besides an invalid timeout type.

const http = require("http");
const {fork} = require("child_process");

if (!process.argv.includes("--server")) {
  const server = fork(__filename, ["--server"]);

  server.on("message", () => { // server is listening
    try {
      http.get({
        port: 3333,
        timeout: null // this causes an error to be thrown immediately
      });
    } catch (ex) {
      // TypeError [ERR_INVALID_ARG_TYPE]: The "msecs" argument must be of type number.
      console.log("caught:", ex);

      // There's a socket here, which will timeout and create an uncaught exception
      console.log("after:", http.globalAgent.sockets);
    }
  });

} else {
  const server = http.createServer();
  server.listen(3333, () => {
    process.send("listening");
  });
  server.timeout = 1000;
}
output
caught: TypeError [ERR_INVALID_ARG_TYPE]: The "msecs" argument must be of type number. Received type object
    at validateNumber (internal/validators.js:130:11)
    at validateTimerDuration (internal/timers.js:146:3)
    at Socket.setTimeout (net.js:418:11)
    at setRequestSocket (_http_agent.js:361:10)
    at handleSocketCreation_Inner (_http_agent.js:349:7)
    at oncreate (_http_agent.js:224:5)
    at Agent.createSocket (_http_agent.js:229:5)
    at Agent.addRequest (_http_agent.js:185:10)
    at new ClientRequest (_http_client.js:249:16)
    at request (http.js:42:10)
after: { 'localhost:3333:':
   [ Socket {
       connecting: true,
       _hadError: false,
       _handle: [TCP],
       _parent: null,
       _host: 'localhost',
       _readableState: [ReadableState],
       readable: false,
       _events: [Object],
       _eventsCount: 5,
       _maxListeners: undefined,
       _writableState: [WritableState],
       writable: true,
       allowHalfOpen: false,
       _sockname: null,
       _pendingData: null,
       _pendingEncoding: '',
       server: null,
       _server: null,
       timeout: null,
       [Symbol(asyncId)]: 8,
       [Symbol(lastWriteQueueSize)]: 0,
       [Symbol(timeout)]: null,
       [Symbol(kBytesRead)]: 0,
       [Symbol(kBytesWritten)]: 0 } ] }
events.js:167
      throw er; // Unhandled 'error' event
      ^

Error: socket hang up
    at createHangUpError (_http_client.js:323:15)
    at Socket.socketOnEnd (_http_client.js:426:23)
    at Socket.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1094:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
    at Socket.socketOnEnd (_http_client.js:426:9)
    at Socket.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1094:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
cjihrig added a commit to cjihrig/node that referenced this issue Feb 26, 2019
Validate the timeout option in the ClientRequest() constructor
to prevent asynchronously thrown validation errors.

PR-URL: nodejs#26214
Fixes: nodejs#26143
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Wyatt Preul <wpreul@gmail.com>
Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
@kkmoslehpour
Copy link

What was the cause of this fix, I am getting this error on http request.

error: 'TypeError [ERR_INVALID_ARG_TYPE]: The "msecs" argument must be of type number. Received type string ('1000')'.

@himanshupareek
Copy link

What was the cause of this fix, I am getting this error on http request.

error: 'TypeError [ERR_INVALID_ARG_TYPE]: The "msecs" argument must be of type number. Received type string ('1000')'.

Yes, I am also getting the same issue

@zbjornson
Copy link
Contributor Author

@kkmoslehpour @himanshupareek the error message says what the problem is: use msecs: 1000 instead of msecs: "1000". The PR attached to this issue fixes how sockets are connected after this kind of setup error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants