pushy icon indicating copy to clipboard operation
pushy copied to clipboard

Seems that memory leak in pushy

Open janzar opened this issue 6 years ago • 8 comments

Hello,

couple of weeks one of our servers stopped working with Full GC with version 0.14.0. We upgraded to 0.14.1, but yesterday all of them failed with Full GC. We have upper bound of 10 k push notifications in flight, but histogram showed that there are more than 1m SimpleApnsPushNotification:

8: 1224994 58799712 com.eatthepath.pushy.apns.util.SimpleApnsPushNotification 9: 1225011 39200352 io.netty.util.concurrent.DefaultPromise 10: 1224994 39199808 com.eatthepath.pushy.apns.ApnsClient$$Lambda$1268/526413906 11: 1224863 39195616 java.util.concurrent.CancellationException 12: 1224994 29399856 com.eatthepath.pushy.apns.util.concurrent.PushNotificationFuture

Internally we do not store elsewhere SimpleApnsPushNotification. Because of some problems we were unable to get full heap dump, but today have looked on heap when everything worked and only place where PushNotificationFuture are stored was map unattachedResponsePromisesByStreamId in ApnsClientHandler. When problems started to happen we seen ioexception, streamexception, http2channelclosedexception exceptions.

May be for some reason pushy considers channel active, but there is something wrong in internals so onStreamAdded is not called after responsePromise is put in unattachedResponsePromisesByStreamId map. Perhaps in writePushNotification in place of putting promise in map, stream could be created, and promise put to it directly, because somewhere inside this.encoder().writeHeaders stream is created anyway. or ChannelFuture returned from this.encoder().writeHeaders should be checked for immediate failure result and then promise responsePromise from map. Because for example at the beginning of StreamBufferingEncoder:: writeHeaders there is such code:

if (closed) { return promise.setFailure(new Http2ChannelClosedException()); }

not sure when it could be closed, but if yes, guess that onStreamAdded will not be called. These are only thoughts, because I'm not an expert in netty/pushy, but we definitely have problems with memory.

janzar avatar Oct 28 '20 17:10 janzar

We log exceptions once in some period. Have searched logs for Http2ChannelClosedException and have found some with stack traces. seems that in this case unattachedResponsePromisesByStreamId will not be cleared. and pingTimeoutFuture.cancel added in 0.14.2, will not be called too:

io.netty.handler.codec.http2.StreamBufferingEncoder$Http2ChannelClosedException: Connection closed at io.netty.handler.codec.http2.StreamBufferingEncoder.close(StreamBufferingEncoder.java:228) at io.netty.handler.codec.http2.Http2ConnectionHandler$BaseDecoder.channelInactive(Http2ConnectionHandler.java:204) at io.netty.handler.codec.http2.Http2ConnectionHandler.channelInactive(Http2ConnectionHandler.java:417) at com.eatthepath.pushy.apns.ApnsClientHandler.channelInactive(ApnsClientHandler.java:499) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:262) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:248) at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:241) at io.netty.channel.ChannelInboundHandlerAdapter.channelInactive(ChannelInboundHandlerAdapter.java:81) at io.netty.handler.timeout.IdleStateHandler.channelInactive(IdleStateHandler.java:277) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:262) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:248) at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:241) at io.netty.handler.codec.ByteToMessageDecoder.channelInputClosed(ByteToMessageDecoder.java:386) at io.netty.handler.codec.ByteToMessageDecoder.channelInactive(ByteToMessageDecoder.java:351) at io.netty.handler.ssl.SslHandler.channelInactive(SslHandler.java:1084) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:262) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:248) at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:241) at io.netty.handler.proxy.ProxyHandler.channelInactive(ProxyHandler.java:231) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:262) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:248) at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:241) at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelInactive(CombinedChannelDuplexHandler.java:418) at io.netty.channel.CombinedChannelDuplexHandler.channelInactive(CombinedChannelDuplexHandler.java:223) at io.netty.handler.proxy.HttpProxyHandler$HttpClientCodecWrapper.channelInactive(HttpProxyHandler.java:262) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:262) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:248) at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:241) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelInactive(DefaultChannelPipeline.java:1405) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:262) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:248) at io.netty.channel.DefaultChannelPipeline.fireChannelInactive(DefaultChannelPipeline.java:901) at io.netty.channel.AbstractChannel$AbstractUnsafe$8.run(AbstractChannel.java:818) at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164) at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500) at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.lang.Thread.run(Thread.java:745)

janzar avatar Oct 28 '20 18:10 janzar

My last comment is wrong, because this is just stack trace were exception is created, but not thrown. But question about failure in this.encoder().writeHeaders before stream is created looks like valid.

janzar avatar Oct 29 '20 09:10 janzar

Thanks for the report.

You wrote:

We have upper bound of 10 k push notifications in flight

Can you say more about how you're enforcing that limit? Also, could you please try this with 0.14.2, which fixed a bug that could keep connections in memory longer than desired?

jchambers avatar Oct 31 '20 17:10 jchambers

Hello,

we use semaphore to enforce this limit. Yes, two days ago I have updated part of servers to 0.14.2 and part leaved with 0.14.1. Yesterday we have seen problems with Apple pushes again - lot of timeouts. Seems that on servers with 0.14.2 there where no problems with memory, but on servers with 0.14.1 it stated to grow, so we need to restart them. But I'm not yet 100% sure, that bug is fixed. Because 0.14.2 handled pushes for different iOS application than 0.14.1. And in past we have seen different behavior for different apps. Next week we will update all servers to 0.14.2. If we will have timeout problems after that and all servers will be fine, I will let you know.

Thanks, Zigurds

janzar avatar Oct 31 '20 18:10 janzar

forgot to add: there where no decrease in successfully delivered pushes with 0.14.2. but with 0.14.1 count of successful pushes decreases by 10 times.

janzar avatar Oct 31 '20 19:10 janzar

@janzar I wanted to follow up on this; have you updated to 0.14.2 across the board, and has that solved the problem?

jchambers avatar Nov 14 '20 15:11 jchambers

Yes, we updated to 0.14.2. When there were problems with Apple this week, servers continued to work without memory problems. Seems that issue is resolved. Thank you!

janzar avatar Nov 14 '20 18:11 janzar

Excellent! Glad to hear it! I'll go ahead and close this issue, then. Thanks for the report and thanks for following up.

jchambers avatar Nov 14 '20 18:11 jchambers