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

`grpc.Client.close()` should have a callback when all calls complete

Open SimenB opened this issue 4 years ago • 8 comments

Is your feature request related to a problem? Please describe.

I'd like to call close on all grpc clients when the app is shutting down, and wait for the in-flight requests to complete.

Describe the solution you'd like

As per #1211:

The close method prevents new calls from being started and frees up resources once all active calls have finished

I'd like to get a callback "once all active calls have finished" so I know it's safe to shutdown the server

(ideally it'd return a Promise, but since the API is using callbacks, this should as well for consistency)

Describe alternatives you've considered

Right now we call close then immediately process.exit(0). It generally works fine, but there might be inflight requests we then ignore.

Additional context

SimenB avatar Apr 07 '20 11:04 SimenB

I'm curious, what's your use case for waiting to shut down the server until the Client's close callback has been called? I ask because shutdown itself actually has similar semantics: Server#tryShutdown stops the server from accepting new requests, and calls its callback when there are no more active requests.

murgatroid99 avatar Apr 07 '20 21:04 murgatroid99

My server in this case is an express.js server serving user facing http traffic, not a grpc server. And this express app speaks grpc to the backend. When the app receives a sigterm it refuses new requests, but waits until all inbound requests are completed (similar to the tryShutdown function). But we have some grpc requests that haven't completed yet even though all client requests have been handled, and I'd like to wait for those to complete before completing the graceful shutdown. Most of the time it's not issue as those internal requests usually takes a few ms from start to finish, but we've seen a couple of instances where it created som log noise.

SimenB avatar Apr 07 '20 21:04 SimenB

I have a similar use case in which I want to know when all grpc connections have been fully closed out after calling close. In my case, I'm spawning a proxy server and connecting grpc through that. I want to shut down the proxy server only after all existing connections have been fully closed. If I don't do this grpc itself prints an error for every connection that is still established at the time when I shutdown the proxy.

Failed to connect to proxy 127.0.0.1:9065 with error read ECONNRESET

Ideally, I'd await client.close() before shutting down my proxy.

mrfelton avatar Apr 23 '20 16:04 mrfelton

I am OK with adding this, but it will require some plumbing. Currently the client doesn't track calls that it started, so it doesn't know when they end.

murgatroid99 avatar Apr 23 '20 21:04 murgatroid99

Would be useful for some tests that do assertions on the connection state after closing client connections.

CMCDragonkai avatar Nov 23 '21 06:11 CMCDragonkai

I have a client interceptor that needs to complete its task before I close the client. I need a way to ensure that everything is completed by the time I decide to close the client.

If there isn't a call to await on, the interceptor runs afterwards, and I get ordering bugs.

Is there something to poll on to know whether the client connections are all done?

CMCDragonkai avatar Dec 09 '21 04:12 CMCDragonkai

If this callback did exist, it would track whether all requests have finished at the network level. It would not account for interceptor code that may be running after that.

murgatroid99 avatar Dec 09 '21 15:12 murgatroid99

I created my own interceptor that does track asynchronous interceptor code for now. I call it FlowCountInterceptor and it uses a sort of reference count to eventually emit an event when the count of flows reaches 0. Each flow represents all asynchronous ops running from the inbound interceptors. Except for onReceiveMessage because I found that only works on unary and client streaming calls, but not duplex nor server streaming calls.

CMCDragonkai avatar Dec 13 '21 00:12 CMCDragonkai