1

I have this problem with node.js, I'm using the node --trace-sync-io when starting my application and when I make api call on my server.js file using the request module and request-promise module, it still warns that I'm using Sync API and points to the start of my request.promise call.

var request = require("request");
var rp = require("request-promise");

var options = {
    uri: url,
    json: true
};

rp(options).then(function (data){
    //then I do something with data here
}.catch(function(err){
    //catch errors here
})

So when I start node with command node --trace-sync-io server.js I get WARNING: Detected use of sync API and stack trace

(node:17212) WARNING: Detected use of sync API
at rng (mypath\node_modules\uuid\lib\rng.js:7:10)
at v4 (mypath\node_modules\uuid\v4.js:13:52)
at Multipart (mypath\node_modules\request\lib\multipart.js:11:19)
at Request (mypath\node_modules\request\request.js:127:21)
at request (mypath\node_modules\request\index.js:54:10)
at mypath\server.js:333:8
at emitOne (events.js:96:13)
at emit (events.js:188:7)
at Query.handleReadyForQuery (mypath\node_modules\pg\lib\query.js:126:8)

which points to the line where I start the call rp(options.then(function(data)))

Quartal
  • 410
  • 3
  • 14
  • It points to [this function](https://github.com/kelektiv/node-uuid/blob/3b218806aa82e76c7aaa6c9ad62e4d5abe2de4e3/lib/rng.js#L6-L8), which calls the sync version of `crypto.randomBytes`. You can't fix that without patching `request` and `uuid`, but I don't see what the actual problem is. – robertklep May 07 '17 at 13:14
  • @robertklep Why does request module call something like that? The problem I think is that I read that using sync api with node.js slows down the whole site, because the server can't process anything else when using sync api. But I don't know what kind of impact this particular thing has? – Quartal May 07 '17 at 13:21
  • `request` uses it for multipart requests (the UUID is used as separator), apparenty even when you're not posting any multipart data. But it's no big deal, my laptop can generate about 300K UUIDv4's a second, so each one takes about 3.3 microseconds. That's not going to be noticable. – robertklep May 07 '17 at 13:24
  • @robertklep Ah okay it's fine then, thank you for the help! – Quartal May 07 '17 at 13:28
  • @robertklep - You should post an answer. – jfriend00 May 07 '17 at 19:10

1 Answers1

1

The stack trace points to the following function inside the uuid package (which is used by request internally to generate a multipart separator):

var rb = require('crypto').randomBytes;

function rng() {
  return rb(16);
};

crypto.randomBytes() is called synchronously here, which is why you get the warning.

However, this doesn't necessarily mean that this code will be causing big issues by blocking the event loop. Sure, it's synchronous, so blocking, but to put things in perspective, my laptop can generate about 300K UUID's per second (the function above is called for each of those).

That means that the event loop will get blocked about 3.3 microseconds for each call, which, in the case of request, is once per request (I think). That's negligible.

robertklep
  • 198,204
  • 35
  • 394
  • 381