ev_epollex_linux_test.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "src/core/lib/iomgr/port.h"
  19. /* This test only relevant on linux systems where epoll() is available */
  20. #if defined(GRPC_LINUX_EPOLL_CREATE1) && defined(GRPC_LINUX_EVENTFD)
  21. #include <string.h>
  22. #include <sys/eventfd.h>
  23. #include <grpc/grpc.h>
  24. #include "src/core/lib/iomgr/ev_epollex_linux.h"
  25. #include "test/core/util/test_config.h"
  26. static void pollset_destroy(void* ps, grpc_error_handle /*error*/) {
  27. grpc_pollset_destroy(static_cast<grpc_pollset*>(ps));
  28. gpr_free(ps);
  29. }
  30. // This test is added to cover the case found in bug:
  31. // https://github.com/grpc/grpc/issues/15760
  32. static void test_pollable_owner_fd() {
  33. grpc_core::ExecCtx exec_ctx;
  34. int ev_fd1;
  35. int ev_fd2;
  36. grpc_fd* grpc_fd1;
  37. grpc_fd* grpc_fd2;
  38. grpc_pollset* ps;
  39. gpr_mu* mu;
  40. // == Create two grpc_fds ==
  41. // All we need is two file descriptors. Doesn't matter what type. We use
  42. // eventfd type here for the purpose of this test
  43. ev_fd1 = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
  44. ev_fd2 = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
  45. if (ev_fd1 < 0 || ev_fd2 < 0) {
  46. gpr_log(GPR_ERROR, "Error in creating event fds for the test");
  47. return;
  48. }
  49. grpc_fd1 = grpc_fd_create(ev_fd1, "epollex-test-fd1", false);
  50. grpc_fd2 = grpc_fd_create(ev_fd2, "epollex-test-fd2", false);
  51. grpc_core::ExecCtx::Get()->Flush();
  52. // == Create a pollset ==
  53. ps = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
  54. grpc_pollset_init(ps, &mu);
  55. grpc_core::ExecCtx::Get()->Flush();
  56. // == Add fd1 to pollset ==
  57. grpc_pollset_add_fd(ps, grpc_fd1);
  58. grpc_core::ExecCtx::Get()->Flush();
  59. // == Destroy fd1 ==
  60. grpc_fd_orphan(grpc_fd1, nullptr, nullptr, "test fd1 orphan");
  61. grpc_core::ExecCtx::Get()->Flush();
  62. // = Add fd2 to pollset ==
  63. //
  64. // Before https://github.com/grpc/grpc/issues/15760, the following line caused
  65. // unexpected behavior (The previous grpc_pollset_add_fd(ps, grpc_fd1) created
  66. // an underlying structure in epollex that held a reference to grpc_fd1 which
  67. // was being accessed here even after grpc_fd_orphan(grpc_fd1) was called
  68. grpc_pollset_add_fd(ps, grpc_fd2);
  69. grpc_core::ExecCtx::Get()->Flush();
  70. // == Destroy fd2 ==
  71. grpc_fd_orphan(grpc_fd2, nullptr, nullptr, "test fd2 orphan");
  72. grpc_core::ExecCtx::Get()->Flush();
  73. // == Destroy pollset
  74. grpc_closure ps_destroy_closure;
  75. GRPC_CLOSURE_INIT(&ps_destroy_closure, pollset_destroy, ps,
  76. grpc_schedule_on_exec_ctx);
  77. grpc_pollset_shutdown(ps, &ps_destroy_closure);
  78. grpc_core::ExecCtx::Get()->Flush();
  79. }
  80. int main(int argc, char** argv) {
  81. const char* poll_strategy = nullptr;
  82. grpc::testing::TestEnvironment env(argc, argv);
  83. grpc_init();
  84. {
  85. grpc_core::ExecCtx exec_ctx;
  86. poll_strategy = grpc_get_poll_strategy_name();
  87. if (poll_strategy != nullptr && strcmp(poll_strategy, "epollex") == 0) {
  88. test_pollable_owner_fd();
  89. } else {
  90. gpr_log(GPR_INFO,
  91. "Skipping the test. The test is only relevant for 'epollex' "
  92. "strategy. and the current strategy is: '%s'",
  93. poll_strategy);
  94. }
  95. }
  96. grpc_shutdown();
  97. return 0;
  98. }
  99. #else /* defined(GRPC_LINUX_EPOLL_CREATE1) && defined(GRPC_LINUX_EVENTFD) */
  100. int main(int /*argc*/, char** /*argv*/) { return 0; }
  101. #endif