moleculerjs / moleculer-web

:earth_africa: Official API Gateway service for Moleculer framework
http://moleculer.services/docs/moleculer-web.html
MIT License
292 stars 119 forks source link

Possible memory leak on alias with custom function on error #298

Closed giovanni-bertoncelli closed 2 years ago

giovanni-bertoncelli commented 2 years ago

Description

I have noticed a strange behaviour when declaring some alias with a custom function that triggers an error on the response. It seems that when the response is not closed correctly, for example on an error, the context remains pending and it is not closed correctly. To simulate this behaviour it should be enough to call res.destroy().

Expected behaviour

I was expecting the context gets correctly disposed when the res somehow is closed or destroyed. On hundreds of calls the memory gets filled up and the memory leak is more evident. It seems that in this case the event finish is not enough to listen to in order to dispose the context:

https://github.com/moleculerjs/moleculer-web/blob/65b7addddc3dc34856d2a3bf6fe202664b2e033c/src/index.js#L418

Reproduction example

https://codesandbox.io/s/res-destroy-memory-leak-igq1i?file=/public/index.html

Workaround

Handling the res.on('close') error and emitting res.emit('finish') once, should fix this behaviour.

giovanni-bertoncelli commented 2 years ago

@icebob can you please release this?