test-ipc-heavy-traffic-deadlock-bug.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* Copyright libuv project contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "task.h"
  22. #include "uv.h"
  23. #include <string.h>
  24. /* See test-ipc.c */
  25. void spawn_helper(uv_pipe_t* channel,
  26. uv_process_t* process,
  27. const char* helper);
  28. #define NUM_WRITES 256
  29. #define BUFFERS_PER_WRITE 3
  30. #define BUFFER_SIZE 0x2000 /* 8 kb. */
  31. #define BUFFER_CONTENT 42
  32. #define XFER_SIZE (NUM_WRITES * BUFFERS_PER_WRITE * BUFFER_SIZE)
  33. struct write_info {
  34. uv_write_t write_req;
  35. char buffers[BUFFER_SIZE][BUFFERS_PER_WRITE];
  36. };
  37. static uv_shutdown_t shutdown_req;
  38. static size_t bytes_written;
  39. static size_t bytes_read;
  40. static void write_cb(uv_write_t* req, int status) {
  41. struct write_info* write_info =
  42. container_of(req, struct write_info, write_req);
  43. ASSERT(status == 0);
  44. bytes_written += BUFFERS_PER_WRITE * BUFFER_SIZE;
  45. free(write_info);
  46. }
  47. static void shutdown_cb(uv_shutdown_t* req, int status) {
  48. ASSERT(status == 0 || status == UV_ENOTCONN);
  49. uv_close((uv_handle_t*) req->handle, NULL);
  50. }
  51. static void do_write(uv_stream_t* handle) {
  52. struct write_info* write_info;
  53. uv_buf_t bufs[BUFFERS_PER_WRITE];
  54. size_t i;
  55. int r;
  56. write_info = malloc(sizeof *write_info);
  57. ASSERT(write_info != NULL);
  58. for (i = 0; i < BUFFERS_PER_WRITE; i++) {
  59. memset(&write_info->buffers[i], BUFFER_CONTENT, BUFFER_SIZE);
  60. bufs[i] = uv_buf_init(write_info->buffers[i], BUFFER_SIZE);
  61. }
  62. r = uv_write(
  63. &write_info->write_req, handle, bufs, BUFFERS_PER_WRITE, write_cb);
  64. ASSERT(r == 0);
  65. }
  66. static void alloc_cb(uv_handle_t* handle,
  67. size_t suggested_size,
  68. uv_buf_t* buf) {
  69. buf->base = malloc(suggested_size);
  70. buf->len = (int) suggested_size;
  71. }
  72. #ifndef _WIN32
  73. #include <sys/types.h>
  74. #include <unistd.h>
  75. #endif
  76. static void read_cb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
  77. ssize_t i;
  78. int r;
  79. ASSERT(nread >= 0);
  80. bytes_read += nread;
  81. for (i = 0; i < nread; i++)
  82. ASSERT(buf->base[i] == BUFFER_CONTENT);
  83. free(buf->base);
  84. if (bytes_read >= XFER_SIZE) {
  85. r = uv_read_stop(handle);
  86. ASSERT(r == 0);
  87. r = uv_shutdown(&shutdown_req, handle, shutdown_cb);
  88. ASSERT(r == 0);
  89. }
  90. }
  91. static void do_writes_and_reads(uv_stream_t* handle) {
  92. size_t i;
  93. int r;
  94. bytes_written = 0;
  95. bytes_read = 0;
  96. for (i = 0; i < NUM_WRITES; i++) {
  97. do_write(handle);
  98. }
  99. r = uv_read_start(handle, alloc_cb, read_cb);
  100. ASSERT(r == 0);
  101. r = uv_run(handle->loop, UV_RUN_DEFAULT);
  102. ASSERT(r == 0);
  103. ASSERT(bytes_written == XFER_SIZE);
  104. ASSERT(bytes_read == XFER_SIZE);
  105. }
  106. TEST_IMPL(ipc_heavy_traffic_deadlock_bug) {
  107. uv_pipe_t pipe;
  108. uv_process_t process;
  109. spawn_helper(&pipe, &process, "ipc_helper_heavy_traffic_deadlock_bug");
  110. do_writes_and_reads((uv_stream_t*) &pipe);
  111. MAKE_VALGRIND_HAPPY();
  112. return 0;
  113. }
  114. int ipc_helper_heavy_traffic_deadlock_bug(void) {
  115. uv_pipe_t pipe;
  116. int r;
  117. r = uv_pipe_init(uv_default_loop(), &pipe, 1);
  118. ASSERT(r == 0);
  119. r = uv_pipe_open(&pipe, 0);
  120. ASSERT(r == 0);
  121. notify_parent_process();
  122. do_writes_and_reads((uv_stream_t*) &pipe);
  123. uv_sleep(100);
  124. MAKE_VALGRIND_HAPPY();
  125. return 0;
  126. }