Hey Carl,

Thank you for your quick response! I've tried to extract the issue in a
test case here
<https://github.com/fkorotkov/snippets/blob/master/proto-streams/src/test/java/com/fkorotkov/example/StreamShutdownTest.java#L51-L52>.
Note that if I change to an in-process server and `InProcessChannelBuilder`
everything works as expected and the test waits 20 seconds.

I suspect it's related to
ManagedChannelImpl#SUBCHANNEL_SHUTDOWN_DELAY_SECONDS
<https://github.com/grpc/grpc-java/blob/master/core/src/main/java/io/grpc/internal/ManagedChannelImpl.java#L87>
constants
because it's the only 5 seconds constant and I see this magic number in
logs.

But in my case I'm basically running my services in Kubernetes and
Kubernetes sends SIGTERM before killing containers so I have a shutdown
hook like this:

Runtime.getRuntime().addShutdownHook(object : Thread() {
  override fun run() {
    println("Got shutdown hook!")
    val start =  System.currentTimeMillis()
    grpcServer?.shutdown()
    grpcServer?.awaitTermination()
    val duration = Duration.ofMillis(System.currentTimeMillis() - start)
    println("Shutdown GRPC service in ${duration.seconds} seconds.")
  }
})

And I call start() separately when the container starts.

On Thu, Oct 19, 2017 at 10:28 AM, 'Carl Mastrangelo' via grpc.io <
[email protected]> wrote:

> Can you show your shutdown code?  awaitTermination() should actually wait
> for shutdown to complete.  In fact, its expected that you call it, even
> when you don't call shutdown():
>
>
> Server s = ....
>
> s.start();
> s.awaitTermination();
>
>
> That blocks the current thread (usually main) while the server serves.
>
>
> On Wednesday, October 18, 2017 at 8:41:47 AM UTC-7, Fedor Korotkov wrote:
>>
>> Hey there,
>>
>> I've asked this question on StackOverflow
>> <https://stackoverflow.com/questions/46716024/graceful-shutdown-of-java-grpc-server-with-streams>,
>> but haven't got a response so decided to duplicate it here. :-)
>>
>> I'm trying to add graceful shutdown for my GRPC service that has some
>> streaming APIs. Basically I want to wait for all GRPC calls to complete
>> before shutting down my application. Streaming calls can take up to several
>> minutes(big file uploads) and it seems current GRPC Java implementation is
>> not respecting such use case.
>>
>> My service is implemented in Java and uses GRPC 1.7.0. So I simply call
>> grpcServer.shutdown() which according to the docs should: "Initiates an
>> orderly shutdown in which preexisting calls continue but new calls are
>> rejected." After it I immediately call grpcServer.awaitTermination() to
>> block until my services in TERMINATED state.
>>
>> But I see that GRPC actually waits at most 5 seconds before sending
>> service in a TERMINATED state even though there are active streams.
>>
>> So is seems grpcServer.shutdown() is not doing what the docs says it does
>> and I wonder what should I do to support my use case. I think GRPC should
>> support it(seems like a pretty common use case). If not I will need to
>> track active streams manually which is doable not looks more like a hack.
>>
>> Best,
>> Fedor
>>
> --
> You received this message because you are subscribed to the Google Groups "
> grpc.io" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/grpc-io.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/grpc-io/f1225a84-ede0-40fb-867b-828a0ba78483%40googlegroups.com
> <https://groups.google.com/d/msgid/grpc-io/f1225a84-ede0-40fb-867b-828a0ba78483%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/grpc-io/CANCeRsXwZf%3DpdX0zMR0VCiytMR0R5-dV9gDNfhwPH71hMpNbPQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to