From 9892751ff617181d2cbd0e810c61b5d3d690e813 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Wed, 29 Apr 2015 15:58:28 +0100 Subject: [PATCH] prevent leak of urlStream on failed downloads --- app/coffee/UrlFetcher.coffee | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/coffee/UrlFetcher.coffee b/app/coffee/UrlFetcher.coffee index 5082a15..e14285d 100644 --- a/app/coffee/UrlFetcher.coffee +++ b/app/coffee/UrlFetcher.coffee @@ -30,6 +30,16 @@ module.exports = UrlFetcher = if res.statusCode >= 200 and res.statusCode < 300 urlStream.pipe(fileStream) else + logger.error statusCode: res.statusCode, url:url, filePath: filePath, "unexpected status code downloading url to cache" + # https://nodejs.org/api/http.html#http_class_http_clientrequest + # If you add a 'response' event handler, then you must consume + # the data from the response object, either by calling + # response.read() whenever there is a 'readable' event, or by + # adding a 'data' handler, or by calling the .resume() + # method. Until the data is consumed, the 'end' event will not + # fire. Also, until the data is read it will consume memory + # that can eventually lead to a 'process out of memory' error. + urlStream.on 'data', () -> # discard the data callbackOnce(new Error("URL returned non-success status code: #{res.statusCode} #{url}")) urlStream.on "error", (error) ->