mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
fix: tcp client memory leaks #10286
This commit is contained in:
@@ -108,6 +108,13 @@ export class ClientTCP extends ClientProxy {
|
||||
this.isConnected = false;
|
||||
this.socket = null;
|
||||
this.connection = undefined;
|
||||
if (this.routingMap.size > 0) {
|
||||
const err = new Error('connection to server closed');
|
||||
for (const callback of this.routingMap.values()) {
|
||||
callback({ err });
|
||||
}
|
||||
this.routingMap.clear();
|
||||
}
|
||||
}
|
||||
|
||||
protected publish(
|
||||
|
||||
@@ -154,9 +154,15 @@ describe('ClientTCP', () => {
|
||||
});
|
||||
});
|
||||
describe('close', () => {
|
||||
let routingMap;
|
||||
let callback;
|
||||
|
||||
beforeEach(() => {
|
||||
(client as any).socket = socket;
|
||||
routingMap = new Map<string, Function>();
|
||||
callback = sinon.spy();
|
||||
routingMap.set('some id', callback)(client as any).socket = socket;
|
||||
(client as any).isConnected = true;
|
||||
(client as any).routingMap = routingMap;
|
||||
client.close();
|
||||
});
|
||||
it('should end() socket', () => {
|
||||
@@ -168,6 +174,16 @@ describe('ClientTCP', () => {
|
||||
it('should set "socket" to null', () => {
|
||||
expect((client as any).socket).to.be.null;
|
||||
});
|
||||
it('should clear out the routing map', () => {
|
||||
expect((client as any).routingMap.size).to.be.eq(0);
|
||||
});
|
||||
it('should call callbacks', () => {
|
||||
expect(
|
||||
callback.calledWith({
|
||||
err: new Error('connection to server closed'),
|
||||
}),
|
||||
).to.be.true;
|
||||
});
|
||||
});
|
||||
describe('bindEvents', () => {
|
||||
it('should bind error event handler', () => {
|
||||
|
||||
Reference in New Issue
Block a user