Socket hang up error

Team

This was originally posted to postman support. They told me to post it in the community.

I am getting socket hang up error while executing the following using newman(4.4.0) after executing few iterations. First few iterations are running fine. After that getting this error. There are no coding issues. The test suite is working fine when executed locally (desktop enterprise version 7.26) for 1-2 iterations.
is there a way to make sendRequest call synchronously or equivalent one to make a synchronous call?

  1. Making a get request returns a response. The response returns items array. One of the attribute in the items array is url. items array’s length is around 100-300. For example items[0].url…items[99].url.
  2. Iterating over the response array till the length of the array
  3. The test (test tab) checks if the url(s) returned is valid or not by using the following code snippet
  4. It is executing fine when executed locally or using postman desktop OR from command line using newman when the array size is small say 10.

Error

**5093. Error socket hang up **
iteration: at request
12 inside “”

Code snippet

//for loop to go through each array item from the response
pm.sendRequest(url, function (err, res) {
if (res.code !==200)
{
console.log("This url is not reachable: " +url);
console.log("Error code = "+res.code);
}
else
{
success = true;
}
});
//end of for loop

I need to verify each url from the response. The tests are failing due to socket hang up error. is there a way to make sendRequest call synchronously?

Appreciate your suggestions/input.

Hi @rjayaram,

Do update your post with the latest information, so that anyone looking at it can provide you with help.

Cheers

Hi Amit

Thanks. I am pasting the latest code here. As you suggested, I have moved the timeout(in milliseconds) parameter to first position. However the values of url and idx are not recognized in the function. It was working before moving the parameter to first position.

Here is the code snippet of calling

//for loop to parse the response and get urls from the result items

console.log("url before calling the function= "+url); //This is printed correctly
 setTimeout(performRequest,11000,url,idx); // Call the function with a delay of 11 seconds
 idx++;

*//end of for loop*
*//end of test*

//Function defined in the same test tab
function performRequest(url,idx) {

   pm.sendRequest(url,function (err, res) {
       console.log('Finsihed: ' + idx); //idx and url values are undefined
       console.log('url = ' + url);
   });
}

Version which worked before, however semantically it seems incorrect
setTimeout(performRequest(url,idx), 11000);

Hey @rjayaram

Try this, it should work as intended:

setTimeout(performRequest.bind(null, url, idx),5000); 

setTimeout(performRequest, 11000, URL, idx); is a newer syntax and looks like not currently available via the NodeJS modules in the Postman Sandbox.

Hi Amit

I tried your above suggestion. Still seeing socket hang up error. In fact I played around by increasing the timeout. No use. Pl let me know if there is any other solution for this issue.

Thanks, Raje

Hi Amit

I wrote a python script to achieve the same. There are no socket errors. I guess it is specific to newman and postman.

Regards, Raje

Hey, @rjayaram glad to know you were able to develop an alternative solution. :raised_hands:

Error socket hang up error received in client pretty much always indicates the server closed the connection for various reason (not being able to process the request in time, or running into some error while processing the request, etc).

I’m just wondering if the library you are using isn’t sending requests asynchronously as pm.sendRequest() does?

I would love to find out why your Python script works for you while newman fails! Which library/methods you are using to send requests in Python? An example script would help us improve solutions at our end :slight_smile:

Thank you! :+1:

Hi Amit

Thanks for looking into it. I am using Python requests module (to handle RESTful service calls). But it is not in Postman test suite. As we have all API tests written in Postman, I do not want one specific feature tests written in different framework. This is kind of workaround. Is there a way in Postman to call sendRequest OR another command to send requests synchronously in Postman?

Thanks, Raje

Hi @rjayaram,

Thanks for your reply. Looks like Python’s Requests module sends requests synchronously and the reason why it works fine. (I’m not a Python expert, but that’s what I got after a quick online search)

I will create a support ticket internally and reach out to you to discuss this further and understand why the setTimeout() method didn’t work for you. :+1:

To anyone following this thread, we will post a solution post troubleshooting.

Hey Raje,

I think I understand why things were failing for you.

You mentioned using a for loop, which I missed.
Inside the for loop you are calling setTimeout() to initiate the performRequest() function.

Since setTimeout would return back immediately, all your requests would again be called pretty much simultaneously.

You should have something like the following to call your requests, instead of an explicit loop construct.

// I've put in some dummy data here
// in your case `requestList` would be the list of URLs you read from the response
let requestList = [
   "http://localhost?id=1",
   "http://localhost?id=2",
   "http://localhost?id=3",
   "http://localhost?id=4",
   "http://localhost?id=5",
   "http://localhost?id=6",
   "http://localhost?id=7",
   "http://localhost?id=8",
   "http://localhost?id=9",
   "http://localhost?id=10",
];

// We are setting a global index variable to keep track of requests being sent
let idx = 0;

// This function will execute the request to the URLs in requestList
function performRequest() {
    
    // URL to send the request to
    let url = requestList[idx];
    console.log("Calling:", url);

    // Send the actual request
    pm.sendRequest(url, (err, res) => {
        console.log("Finished:", requestList[idx]);
        
        // Check if requests are remaining and call the next one after 2 seconds delay.
        if( ++idx < requestList.length) {
            setTimeout(performRequest, 2000);
        } 
    });
}

// Start sending requests
performRequest();

Unfortunately, async-await and promise aren’t yet supported within Postman sandbox, else we could have a cleaner solution. Till then, the above solution should work!

Just came to know about a hack to make the async/await work in Postman sandbox, thanks to @kevin.swiber :raised_hands:
Linking to their solution: Executing sync requests programmatically from a pre-request or test scripts

Here is cleaner solutions using async/await:

// Set this to some high value
// current value 300000 = 5 minutes, which should be good in most cases!
const _dummy = setInterval(() => {}, 300000);

function performRequest(url) {
   return new Promise( (resolve) => { pm.sendRequest(url, (err, res) => {

            // We are not checking for errors
            // If you want you can read the err object to do do some error handling

            setTimeout((res) => { return resolve(res); }, 2000); // Wait for 2 seconds before returning the result.

            // if no delay is required remove the above line and uncomment the following
            // return resolve(res);
        });
   });
}

// I haven't defined requestList in this example, it should be set as described in the previous post
(async function () {
    for(let idx = 0; idx < requestList.length; idx++) {
        console.log("Calling:", requestList[idx]);

        let response = await performRequest(requestList[idx]);

        console.log("response:", response);
    }
    
    clearInterval(_dummy);
}) ();

Hi Amit

Thanks for looking into this. I will try and update you in few days.

Regards, Raje