Skip to content

Commit

Permalink
fix: clean up old requests to prevent lambda resource exhaustion (#1217)
Browse files Browse the repository at this point in the history
refs PR 1210
  • Loading branch information
aryamohanan authored Jul 5, 2024
1 parent 90c0ca6 commit a8a22f0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/serverless/src/backend_connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ function send(resourcePath, payload, finalLambdaRequest, callback) {
if (finalLambdaRequest) {
req.removeAllListeners();
req.on('error', () => {});
// At this point we already received a response from the server, but since we removed all listeners we have to
// manually clean up the request.
req.destroy();
}

handleCallback();
Expand Down
13 changes: 12 additions & 1 deletion packages/serverless/test/backend_connector_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,21 @@ describe('[UNIT] backend connector', () => {
this.timeout(config.getTestTimeout());

let onStub;
let destroyStub;

beforeEach(() => {
sinon.spy(global, 'setInterval');
sinon.spy(global, 'clearInterval');

onStub = sinon.stub();
destroyStub = sinon.stub();

sinon.stub(uninstrumentedHttp.http, 'request').returns({
on: onStub,
setTimeout: sinon.stub(),
end: sinon.stub(),
removeAllListeners: sinon.stub()
removeAllListeners: sinon.stub(),
destroy: destroyStub
});
});

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

setTimeout(onEnd, 200);
await prom;

expect(destroyStub.called).to.be.true;
});
});

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

setTimeout(onEnd, 200);
await prom;

expect(destroyStub.called).to.be.true;
});
});

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

// 1 bundle req, 2 heartbeats
expect(uninstrumentedHttp.http.request.callCount).to.eql(3);

expect(destroyStub.called).to.be.true;
});
});

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

expect(uninstrumentedHttp.http.request.callCount).to.eql(2);

expect(destroyStub.called).to.be.true;
});
});
});
Expand Down

0 comments on commit a8a22f0

Please sign in to comment.