Possible memory leak
Hi all,
I am using NGINX as a reverse proxy for TCP streams. The server listens to 6680 TCP ports (not a typo) and pushes them on to upstream server(s) on port 5000.
In the filter phase there is a njs script involved that injects the original destination port to the payload of the packet so that my application knows it.
My NGINX configuration:
user nginx;
worker_processes auto;
worker_rlimit_nofile 65535;
error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;
load_module modules/ngx_stream_js_module.so;
events {
worker_connections 65535;
}
stream {
js_include inject_filter.js;
server {
include listen_tcp.conf;
js_filter injectPort;
proxy_pass backends ;
}
upstream backends {
server 0.0.0.0:5000;
}
}
inject_filter.js script
function injectPort(s){
s.on('upload', function(data,flags){
s.send(s.variables.server_port + data, flags);
return;
});
}
The TCP streams never close (by design), the memory consumption keeps increasing as the data of the streams keep coming. As soon as the streams are closed the memory is freeed.
Hi @mantzouranisc,
Thank you for reporting the issue.
Yes, this is a known limitation, njs context is not destroyed until an http request or a stream session object exists. njs garbage collection is not implemented yet (and not always needed, for short http requests for example).
BTW, nginx itself rarely frees anything until a http request exists (it uses memory pool attached for current request, the pool is destroyed as a whole at the end).
@xeioex thanks for your quick reply. Are you planning on supporting this kind of functionality in the future? Any suggestions on how to work around it?
Thanks in advance
@mantzouranisc
Any suggestions on how to work around it?
It depends on your application obviously. For example, you can manually count the number of callback invocations, and forcefully stop the session by throwing exception.
function injectPort(s) {
var n = 0;
s.on('upload', function(data,flags) {
if (++n > LIMIT) {
throw Error("limit reached");
};
s.send(s.variables.server_port + data, flags);
});
}
@xeioex my application has TCP connections that never actually close. They constantly push data and I need to concatenate the port number with the stream's data. I guess that one solution would be to periodically close the TCP connection between nginx and the upstream servers, so that allocated memory is freed. What would the recommended way to do that be? Would it be possible to nullify the stream data that are accumulated over time?
@mantzouranisc
I guess that one solution would be to periodically close the TCP connection between nginx and the upstream servers, so that allocated memory is freed. What would the recommended way to do that be?
Yes. I would recommend to close the tcp connection manually, after a number of processed messages. See https://github.com/nginx/njs/issues/99#issuecomment-464669647
@xeioex would setting the last flag terminate the TCP session? I there any related documentation?
thanks
For such simple cases we can introduce manual garbage collection. It wouldn't be hard to add a function that resets js context.
@mantzouranisc
try the following as a workaround
var LIMIT = 10000;
function injectPort(s) {
var n = 0;
s.on('upload', function(data,flags) {
if (++n > LIMIT) {
throw Error("limit reached");
};
s.send(s.variables.server_port + data, flags);
});
}
in logs you will see:
2019/02/18 19:48:15 [error] 10479#10479: *22 js exception: Error: limit reached
at anonymous (test.js:121)
at main (native) while proxying and sending to upstream, client: 127.0.0.1, server: 127.0.0.1:8095, upstream: "127.0.0.1:8079", bytes from/to client:65/0, bytes from/to upstream:0/0```
Currently this is the only way to forcefully close the session.
@xeioex I will test it out and keep you updated.
thanks for your support
@xeioex works as expected:
2019/02/18 21:15:10 [error] 2554#2554: *1037263 js exception: Error: limit reached
at anonymous (:8)
at main (native)
while proxying and sending to upstream, client: xxx.xxx.xxx.xxx, server: 0.0.0.0:9712, upstream: "0.0.0.0:5000", bytes from/to client:558304/0, bytes from/to upstream:0/670623
2019/02/18 21:15:11 [info] 2555#2555: *1037749 js: new connection from xxx.xxx.xxx.xxx
That means that the connection between nginx and the upstream server is destroyed, but since the connection from the client to nginx is still open it immediately reconnects to the upstream. Correct?
@mantzouranisc
That means that the connection between nginx and the upstream server is destroyed, but since the connection from the client to nginx is still open it immediately reconnects to the upstream. Correct?
No. The connection to the client is also closed.
at anonymous (test.js:121)
at main (native)
while proxying and sending to upstream, client: 127.0.0.1, server: 127.0.0.1:8095, upstream: "127.0.0.1:8079", bytes from/to client:65/0, bytes from/to upstream:0/0
2019/02/18 19:48:15 [debug] 10479#10479: *22 finalize stream proxy: 200
2019/02/18 19:48:15 [debug] 10479#10479: *22 free rr peer 1 0
2019/02/18 19:48:15 [debug] 10479#10479: *22 close stream proxy upstream connection: 32
2019/02/18 19:48:15 [debug] 10479#10479: *22 reusable connection: 0
2019/02/18 19:48:15 [debug] 10479#10479: *22 finalize stream session: 200 <<<
2019/02/18 19:48:15 [debug] 10479#10479: *22 stream log handler
2019/02/18 19:48:15 [debug] 10479#10479: *22 close stream connection: 31