Compare commits

...

1 Commits

Author SHA1 Message Date
Rahul Padigela 4214282faf bf: close/end readable/response streams on errors
This fixes the leakage of sockets in CLOSE_WAIT state by closing the streams
and destroying the sockets when the client has abruptly closed the connection.

Upstream requests to Azure/AWS need to be aborted in
AzureClient/AWSClient implementation. Currently azure-storage module doesn't
have a clear way of aborting a request.
2018-01-02 15:39:48 -08:00
1 changed files with 69 additions and 40 deletions

View File

@ -1,7 +1,12 @@
const url = require('url'); const url = require('url');
const { eachSeries } = require('async');
const ipCheck = require('../ipCheck'); const ipCheck = require('../ipCheck');
const errors = require('../errors'); const errors = require('../errors');
const responseErr = new Error();
responseErr.code = 'ResponseError';
responseErr.message = 'response closed by client request before all data sent';
/** /**
* setCommonResponseHeaders - Set HTTP response headers * setCommonResponseHeaders - Set HTTP response headers
* @param {object} headers - key and value of new headers to add * @param {object} headers - key and value of new headers to add
@ -242,54 +247,78 @@ function okContentHeadersResponse(overrideParams, resHeaders,
return response; return response;
} }
function retrieveData(locations, dataRetrievalFn, function retrieveData(locations, retrieveDataFn, response, log) {
response, logger, errorHandlerFn) { let responseDestroyed = false;
if (locations.length === 0) { const _destroyResponse = () => {
return response.end(); // destroys the socket if available
} response.destroy();
if (errorHandlerFn === undefined) { responseDestroyed = true;
// eslint-disable-next-line };
errorHandlerFn = () => { response.connection.destroy(); }; response.once('close', () => {
} log.debug('received close event before response end');
const current = locations.shift(); _destroyResponse();
if (current.azureStreamingOptions) { });
// pipe data directly from source to response
response.on('error', err => { eachSeries(locations,
logger.error('error piping data from source'); (current, next) => retrieveDataFn(current, log, (err, readable) => {
errorHandlerFn(err); let cbCalled = false;
}); const _next = err => {
return dataRetrievalFn(current, response, logger, err => { // Avoid multiple callbacks since it's possible that response's
// close event and the readable's end event are emitted at
// the same time.
if (!cbCalled) {
cbCalled = true;
next(err);
}
};
if (err) { if (err) {
logger.error('failed to get object from source', { log.error('failed to get object', {
error: err,
method: 'retrieveData',
backend: 'Azure',
});
return errorHandlerFn(err);
}
return undefined;
});
}
return dataRetrievalFn(current, response, logger,
(err, readable) => {
if (err) {
logger.error('failed to get object', {
error: err, error: err,
method: 'retrieveData', method: 'retrieveData',
}); });
return errorHandlerFn(err); _destroyResponse();
return _next(err);
} }
readable.on('error', err => { if (responseDestroyed) {
logger.error('error piping data from source'); log.debug('response destroyed before readable could stream');
errorHandlerFn(err); readable.emit('close');
return _next(responseErr);
}
if (current.azureStreamingOptions) {
// azure-sdk handles the streams
return _next();
}
// client closed the connection abruptly
response.once('close', () => {
log.debug('received close event before readable end');
if (!responseDestroyed) {
_destroyResponse();
}
readable.emit('close');
return _next(responseErr);
}); });
// readable stream successfully consumed
readable.on('end', () => { readable.on('end', () => {
process.nextTick(retrieveData, log.debug('readable stream end reached');
locations, dataRetrievalFn, response, logger); return _next();
}); });
readable.pipe(response, { end: false }); // errors on server side with readable stream
return undefined; readable.on('error', err => {
}); log.error('error piping data from source');
return _next(err);
});
return readable.pipe(response, { end: false });
}), err => {
if (err) {
log.debug('abort response due to client error', {
error: err.code, errMsg: err.message });
}
// call end for all cases (error/success) per node.js docs
// recommendation
response.end();
}
);
} }
function _responseBody(responseBackend, errCode, payload, response, log, function _responseBody(responseBackend, errCode, payload, response, log,