diff options
author | Mehrdad Afshari <mehrdada@users.noreply.github.com> | 2018-07-12 07:28:07 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-12 07:28:07 -0700 |
commit | ea44d938918ccb21885d0c40d711175ad25ecf1b (patch) | |
tree | ab551fd0942b68849fe67d384575778f8531fce6 /src/csharp | |
parent | ea75af5e972055515bfbab93b2e0d4b48a9d3268 (diff) | |
parent | 8c2eeee322e6c35025dbf3143b27ec26eeb7f197 (diff) |
Merge pull request #15998 from grpc/v1.13.x
Upmerge changes from v1.13.x to master.
Diffstat (limited to 'src/csharp')
-rw-r--r-- | src/csharp/Grpc.Core/GrpcEnvironment.cs | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/src/csharp/Grpc.Core/GrpcEnvironment.cs b/src/csharp/Grpc.Core/GrpcEnvironment.cs index 6bb2f6c3e5..a6a1d8af50 100644 --- a/src/csharp/Grpc.Core/GrpcEnvironment.cs +++ b/src/csharp/Grpc.Core/GrpcEnvironment.cs @@ -422,9 +422,32 @@ namespace Grpc.Core { if (!hooksRegistered) { + // Under normal circumstances, the user is expected to shutdown all + // the gRPC channels and servers before the application exits. The following + // hooks provide some extra handling for cases when this is not the case, + // in the effort to achieve a reasonable behavior on shutdown. #if NETSTANDARD1_5 - System.Runtime.Loader.AssemblyLoadContext.Default.Unloading += (assemblyLoadContext) => { HandleShutdown(); }; + // No action required at shutdown on .NET Core + // - In-progress P/Invoke calls (such as grpc_completion_queue_next) don't seem + // to prevent a .NET core application from terminating, so no special handling + // is needed. + // - .NET core doesn't run finalizers on shutdown, so there's no risk of getting + // a crash because grpc_*_destroy methods for native objects being invoked + // in wrong order. + // TODO(jtattermusch): Verify that the shutdown hooks are still not needed + // once we add support for new platforms using netstandard (e.g. Xamarin). #else + // On desktop .NET framework and Mono, we need to register for a shutdown + // event to explicitly shutdown the GrpcEnvironment. + // - On Desktop .NET framework, we need to do a proper shutdown to prevent a crash + // when the framework attempts to run the finalizers for SafeHandle object representing the native + // grpc objects. The finalizers calls the native grpc_*_destroy methods (e.g. grpc_server_destroy) + // in a random order, which is not supported by gRPC. + // - On Mono, the process would hang as the GrpcThreadPool threads are sleeping + // in grpc_completion_queue_next P/Invoke invocation and mono won't let the + // process shutdown until the P/Invoke calls return. We achieve that by shutting down + // the completion queue(s) which associated with the GrpcThreadPool, which will + // cause the grpc_completion_queue_next calls to return immediately. AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) => { HandleShutdown(); }; AppDomain.CurrentDomain.DomainUnload += (sender, eventArgs) => { HandleShutdown(); }; #endif |