Skip to content

Commit a8a22f0

Browse files
authored
fix: clean up old requests to prevent lambda resource exhaustion (#1217)
refs PR 1210
1 parent 90c0ca6 commit a8a22f0

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

packages/serverless/src/backend_connector.js

+3
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ function send(resourcePath, payload, finalLambdaRequest, callback) {
308308
if (finalLambdaRequest) {
309309
req.removeAllListeners();
310310
req.on('error', () => {});
311+
// At this point we already received a response from the server, but since we removed all listeners we have to
312+
// manually clean up the request.
313+
req.destroy();
311314
}
312315

313316
handleCallback();

packages/serverless/test/backend_connector_test.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,21 @@ describe('[UNIT] backend connector', () => {
3232
this.timeout(config.getTestTimeout());
3333

3434
let onStub;
35+
let destroyStub;
3536

3637
beforeEach(() => {
3738
sinon.spy(global, 'setInterval');
3839
sinon.spy(global, 'clearInterval');
3940

4041
onStub = sinon.stub();
42+
destroyStub = sinon.stub();
4143

4244
sinon.stub(uninstrumentedHttp.http, 'request').returns({
4345
on: onStub,
4446
setTimeout: sinon.stub(),
4547
end: sinon.stub(),
46-
removeAllListeners: sinon.stub()
48+
removeAllListeners: sinon.stub(),
49+
destroy: destroyStub
4750
});
4851
});
4952

@@ -97,6 +100,8 @@ describe('[UNIT] backend connector', () => {
97100

98101
setTimeout(onEnd, 200);
99102
await prom;
103+
104+
expect(destroyStub.called).to.be.true;
100105
});
101106
});
102107

@@ -122,6 +127,8 @@ describe('[UNIT] backend connector', () => {
122127

123128
setTimeout(onEnd, 200);
124129
await prom;
130+
131+
expect(destroyStub.called).to.be.true;
125132
});
126133
});
127134

@@ -163,6 +170,8 @@ describe('[UNIT] backend connector', () => {
163170

164171
// 1 bundle req, 2 heartbeats
165172
expect(uninstrumentedHttp.http.request.callCount).to.eql(3);
173+
174+
expect(destroyStub.called).to.be.true;
166175
});
167176
});
168177

@@ -224,6 +233,8 @@ describe('[UNIT] backend connector', () => {
224233
await delay(250);
225234

226235
expect(uninstrumentedHttp.http.request.callCount).to.eql(2);
236+
237+
expect(destroyStub.called).to.be.true;
227238
});
228239
});
229240
});

0 commit comments

Comments
 (0)