vbrin27
vbrin27

Reputation: 991

ERR_EMPTY_RESPONSE error when the server takes more than 2min to respond

I have written a web service using node & express that runs automation on request and responds with the finalized report.

I have used jquery ajax (tried fetch API as well) in the client side to start the automation in the server and on completion, reports are compressed as zip and sent to the client.

Issue:

When the server responds within 2 min, the zip is downloaded successfully but when the server takes more than 2 min to respond, the http request fails with "ERR_EMPTY_RESPONSE" though there is no issue in the server side.
I learnt that by default there is no timeout for the HTTP requests but I am not sure why am I facing this issue.

I would like to know the reason behind this behavior.

Snippet of ajax request made in the client side:

                var promise = $.ajax({
                    url: 'http://localhost:8001/test',
                    method:'POST',
                    xhrFields: {
                        responseType: 'blob'
                    },
                    success: function(res){
                        downloadFromBlob(res);
                    },
                    error: function(xhr, statusText, err){
                        alert(statusText);
                    },
                    data: options,
                });

Snippet from webservice (I have just tried to reproduce the issue here. The automation takes nearly 15 min to complete):

  router.get('/test/', runAutomation);
  async function runAutomation(req, res, next){
        res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        res.header('Access-Control-Allow-Origin', '*');
        res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
        const timeout = 120000;
        setTimeout(()=> {
            const path = 'reports.zip';
            res.download(path);
        }, timeout);
    },  

Upvotes: 0

Views: 1645

Answers (2)

vbrin27
vbrin27

Reputation: 991

In express, if we check /bin/www file content, by default it sets the timeout to 120000 ms as below

 server.setTimeout(120000);

We need to update it. Ref: https://nodejs.org/api/http.html#http_server_settimeout_msecs_callback

Upvotes: 0

josh.trow
josh.trow

Reputation: 4901

This is caused not by the backend, but your browser - I have consistently seen the 2 minute timeout across all browsers at some point or another. You may want your backend to return a 202 Accepted immediately, and ping it for updates rather than trying to wait for it to complete in one shot.

Upvotes: 1

Related Questions