grpc-java icon indicating copy to clipboard operation
grpc-java copied to clipboard

Netty appears to stop accepting new connections after 2h when TLS is enabled

Open invidious9000 opened this issue 9 years ago • 8 comments

Please answer these questions before submitting your issue.

What version of gRPC are you using?

1.7.0

What JVM are you using (java -version)?

Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

What did you do?

Start a server with any service definition and TLS enabled and leave it idle for 2h

What did you expect to see?

Client calls succeeding

What did you see instead?

Client calls timing out

Just to clarify, this is independent of keep-alive issues with StreamObservers - totally fresh clients (new jvms) time out on simple unary calls that worked 2h earlier, only remedy is to restart the server.

invidious9000 avatar Nov 14 '17 07:11 invidious9000

This is weird. Is there any error that shows up in the server log?

Are you clients directly connecting to the server (not through a proxy)?

ejona86 avatar Nov 14 '17 23:11 ejona86

Nothing at INFO but I'll re-run with DEBUG threshold and update in a few hours; clients are directly connecting to server which resides on same machine, no-proxy.

invidious9000 avatar Nov 14 '17 23:11 invidious9000

@invidious9000, INFO is fine; I don't expect DEBUG to show much more. I just wanted to make sure there wasn't an OutOfMemoryError or similar that would explain the breakage.

ejona86 avatar Nov 15 '17 01:11 ejona86

I think I may have figured out some of what's going on, we're using guice-persist and C3P0 and I'm getting these debug lines after approx. 2h:

2017-11-15 04:55:47,402 [grpc-default-executor-30] DEBUG c.m.v.r.BasicResourcePool - acquire test -- pool is already maxed out. [managed: 20; max: 20]
2017-11-15 04:55:47,403 [grpc-default-executor-30] DEBUG c.m.v.r.BasicResourcePool - awaitAvailable(): com.mchange.v2.c3p0.impl.NewPooledConnection@24807163

I went through some logs and found a trend - the executor number (grpc-default-executor-%d) increments until we hit our pool limit with C3P0, I'm not sure if this is expected behavior but there seems to be a direct correlation to the number of executor resources being created and the pool exhausting.

I think the reason this is breaking our client calls is because we have an interceptor that checks client certificate CRLDPs and logs caller principal to DB, with the pool exhausted this is silently failing and timing out.

What is the lifecycle of a shared executor resource? Is it normal to create so many?

I'm still digging into what's not getting cleaned up but we do no manual manipulation of UnitOfWork with guice-persist (simple @Transactional methods in DAOs) and we use the code elsewhere without issues. I will isolate the bits that interact with DB see if I can identify where all of these executor services are coming from, it could be C3P0 is leaking connections somehow or we're failing to release a resource somewhere.

invidious9000 avatar Nov 15 '17 12:11 invidious9000

What is the lifecycle of a shared executor resource? Is it normal to create so many?

It is a static Executors.newCachedThreadPool() (which is unbonudend) that is initialized on first use. We use reference counting for it. After the reference count has been zero for one second, we shut it down. If we need it again after that point, we'll create a new one as if it's the first use.

The executor itself will shutdown threads that have been inactive for 1 minute. So it is possible to have a grpc-default-executor-30 but with only a single thread in the executor (because the other ones have been shut down, even before grpc-default-executor-30 was created).

ejona86 avatar Nov 15 '17 16:11 ejona86

I'm not sure if this adds much but out of curiosity I set up a job to periodically make an RPC call every 15s against one of the services that was failing earlier, it's been close to 5h now it's still operating correctly and only on grpc-default-executor-2 (no incrementing). My hope was if it was solely a DB/pool issue it'd force consumption of the connection pool faster and make things break sooner but it seems to be covering-up or otherwise fixing something with a side-effect.

At this point I'm not sure if it's actually a grpc issue or a result of some sort of interaction between libraries, I will try to figure out how/why c3p0 is scheduling test queries on grpc executor and what the consequences are when that executor is being cleaned up.

invidious9000 avatar Nov 15 '17 20:11 invidious9000

Oh, important note: since this is on server-side the server will hold a reference to the executor so it won't be shut down. So the main thing to worry about is Threads being rotated due to inactivity (say, combined with a ThreadLocal).

ejona86 avatar Nov 15 '17 21:11 ejona86

I think it's safe to close this issue, the problem appears to be totally independent of grpc - it ended up being the result of an idiosyncrasy of guice-persist. We had a getResultList() usage that was missing a @Transactional annotation, guice-persist created UoWs for us but leaked connections that were stuck in idle in transaction state which subsequently exhausted the pool and silently broke interceptor. In non-grpc project we used PersistFilter which covered up some of these bad behaviors by always closing the EntityManager after a request, solution for this problem was to add @Transactional to all methods that use EM provider.

Sorry for the goose chase and thank you for the help.

invidious9000 avatar Nov 16 '17 10:11 invidious9000