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:
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 emittingres.emit('finish')
once, should fix this behaviour.