I believe this leak is caused by your client not closing the socket. NanoHttpd uses a timeout of 5s on read and only stops the serving thread when the socket has been closed on client side :
Line 190 :
} catch (Exception e) {
// When the socket is closed by the client, we throw our own SocketException
// to break the "keep alive" loop above.
if (!(e instanceof SocketException && "NanoHttpd Shutdown".equals(e.getMessage()))) {
e.printStackTrace();
}
}
Line 863 :
if (read == -1) {
// socket was been closed
safeClose(inputStream);
safeClose(outputStream);
throw new SocketException("NanoHttpd Shutdown");
}
I would suggest checking if your client correctly closes the connection and maybe write your own AsyncRunner
implementation instead of the DefaultAsyncRunner
provided by NanoHttpd, in order to limit the number of threads. You could also implement, on top of that, a mechanism to stop threads serving for more than x sec (30s would be a good start imo).