Make gil handling in completion queue more robust

It turns out that the code generation for "with gil" is a bit more
complicated than the logic for re-obtaining the gil at the end of
"with nogil." This is because PyGILState_Ensure seems to, during
interpreter finalization, think it needs to call a new thread
(resulting in a call to cpython new_threadstate) which then segfaults.

Because "with nogil" knows that, prior to executing, it already had
the gil, it doesn't need to set up as much state, and thus the segfault
does not occur.

To avoid this, we just only use "with nogil" within the infinite loop,
and then end the "nogil" block before we check signals. This avoids
needing any "with gil" call at all.

I was able to reliably reproduce the segfault within a few minutes
before the patch by running a binary in a loop (with py3) while
maxing out my machines cpu usage. After the patch, I have not
been able to reproduce the segfault after two hours.

Note that this race can only occur when the user does not properly
clean up all their channels, and is relying on garbage collection to
do so (which isn't guaranteed). However, we want to avoid a segfault
on failure to close because this isn't a good user error and makes it
hard to debug.
1 file changed
tree: 53519f8870c377470749b5518569025938a159f1
  1. .github/
  2. .vscode/
  3. bazel/
  4. cmake/
  5. doc/
  6. etc/
  7. examples/
  8. include/
  9. src/
  10. summerofcode/
  11. templates/
  12. test/
  13. third_party/
  14. tools/
  15. .bazelrc
  16. .clang-format
  17. .clang-tidy
  18. .clang_complete
  19. .editorconfig
  20. .gitignore
  21. .gitmodules
  22. .istanbul.yml
  23. .pylintrc
  24. .pylintrc-examples
  25. .pylintrc-tests
  26. .rspec
  27. .travis.yml
  28. .yardopts
  29. AUTHORS
  30. BUILD
  31. build.yaml
  32. build_config.rb
  33. BUILDING.md
  34. CMakeLists.txt
  35. CODE-OF-CONDUCT.md
  36. composer.json
  37. CONCEPTS.md
  38. config.m4
  39. config.w32
  40. CONTRIBUTING.md
  41. Gemfile
  42. gRPC-C++.podspec
  43. gRPC-Core.podspec
  44. gRPC-ProtoRPC.podspec
  45. gRPC-RxLibrary.podspec
  46. grpc.bzl
  47. grpc.def
  48. grpc.gemspec
  49. grpc.gyp
  50. gRPC.podspec
  51. LICENSE
  52. Makefile
  53. MANIFEST.md
  54. NOTICE.txt
  55. OWNERS
  56. package.xml
  57. PYTHON-MANIFEST.in
  58. Rakefile
  59. README.md
  60. requirements.bazel.txt
  61. requirements.txt
  62. setup.cfg
  63. setup.py
  64. TROUBLESHOOTING.md
  65. WORKSPACE
README.md

gRPC - An RPC library and framework

gRPC is a modern, open source, high-performance remote procedure call (RPC) framework that can run anywhere. gRPC enables client and server applications to communicate transparently, and simplifies the building of connected systems.

Join the chat at https://gitter.im/grpc/grpc

To start using gRPC

To maximize usability, gRPC supports the standard method for adding dependencies to a user‘s chosen language (if there is one). In most languages, the gRPC runtime comes as a package available in a user’s language package manager.

For instructions on how to use the language-specific gRPC runtime for a project, please refer to these documents

  • C++: follow the instructions under the src/cpp directory
  • C#: NuGet package Grpc
  • Dart: pub package grpc
  • Go: go get google.golang.org/grpc
  • Java: Use JARs from Maven Central Repository
  • Node: npm install grpc
  • Objective-C: Add gRPC-ProtoRPC dependency to podspec
  • PHP: pecl install grpc
  • Python: pip install grpcio
  • Ruby: gem install grpc
  • WebJS: follow the grpc-web instructions

Per-language quickstart guides and tutorials can be found in the documentation section on the grpc.io website. Code examples are available in the examples directory.

Precompiled bleeding-edge package builds of gRPC master branch's HEAD are uploaded daily to packages.grpc.io.

To start developing gRPC

Contributions are welcome!

Please read How to contribute which will guide you through the entire workflow of how to build the source code, how to run the tests, and how to contribute changes to the gRPC codebase. The “How to contribute” document also contains info on how the contribution process works and contains best practices for creating contributions.

Troubleshooting

Sometimes things go wrong. Please check out the Troubleshooting guide if you are experiencing issues with gRPC.

Performance

See the Performance dashboard for performance numbers of the latest released version.

Concepts

See gRPC Concepts

About This Repository

This repository contains source code for gRPC libraries implemented in multiple languages written on top of a shared C core library src/core.

Libraries in different languages may be in various states of development. We are seeking contributions for all of these libraries:

LanguageSource
Shared C [core library]src/core
C++src/cpp
Rubysrc/ruby
Pythonsrc/python
PHPsrc/php
C#src/csharp
Objective-Csrc/objective-c
LanguageSource repo
Javagrpc-java
Gogrpc-go
NodeJSgrpc-node
WebJSgrpc-web
Dartgrpc-dart