• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2019 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 // close_fd_test tests the behavior of grpc core when the transport gets
18 // disconnected.
19 // The test creates an http2 transport over a socket pair and closes the
20 // client or server file descriptor to simulate connection breakage while
21 // an RPC call is in progress.
22 //
23 //
24 #include <grpc/impl/channel_arg_names.h>
25 #include <grpc/impl/propagation_bits.h>
26 #include <grpc/slice.h>
27 #include <grpc/status.h>
28 #include <grpc/support/time.h>
29 #include <stdint.h>
30 
31 #include "absl/log/check.h"
32 #include "absl/log/log.h"
33 #include "absl/status/statusor.h"
34 #include "absl/strings/str_format.h"
35 #include "src/core/channelz/channelz.h"
36 #include "src/core/lib/channel/channel_args.h"
37 #include "src/core/lib/iomgr/endpoint.h"
38 #include "src/core/lib/iomgr/error.h"
39 #include "src/core/lib/iomgr/exec_ctx.h"
40 #include "src/core/lib/iomgr/port.h"
41 #include "src/core/lib/surface/channel_create.h"
42 #include "src/core/lib/surface/channel_stack_type.h"
43 #include "src/core/lib/transport/transport.h"
44 #include "src/core/util/orphanable.h"
45 #include "src/core/util/ref_counted_ptr.h"
46 
47 // This test won't work except with posix sockets enabled
48 #ifdef GRPC_POSIX_SOCKET_TCP
49 
50 #include <grpc/byte_buffer.h>
51 #include <grpc/grpc.h>
52 #include <grpc/support/alloc.h>
53 #include <string.h>
54 #include <unistd.h>
55 
56 #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
57 #include "src/core/lib/iomgr/endpoint_pair.h"
58 #include "src/core/lib/surface/channel.h"
59 #include "src/core/lib/surface/completion_queue.h"
60 #include "src/core/server/server.h"
61 #include "src/core/util/crash.h"
62 #include "test/core/test_util/test_config.h"
63 
tag(intptr_t t)64 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
65 
66 typedef struct test_ctx test_ctx;
67 
68 struct test_ctx {
69   // completion queue for call notifications on the server
70   grpc_completion_queue* cq;
71   // completion queue registered to server for shutdown events
72   grpc_completion_queue* shutdown_cq;
73   // client's completion queue
74   grpc_completion_queue* client_cq;
75   // completion queue bound to call on the server
76   grpc_completion_queue* bound_cq;
77   // Server responds to client calls
78   grpc_server* server;
79   // Client calls are sent over the channel
80   grpc_channel* client;
81   // encapsulates client, server endpoints
82   grpc_endpoint_pair* ep;
83 };
84 
85 static test_ctx g_ctx;
86 
87 // chttp2 transport that is immediately available (used for testing
88 // connected_channel without a client_channel
89 
server_setup_transport(grpc_core::Transport * transport)90 static void server_setup_transport(grpc_core::Transport* transport) {
91   grpc_core::ExecCtx exec_ctx;
92   grpc_endpoint_add_to_pollset(g_ctx.ep->server, grpc_cq_pollset(g_ctx.cq));
93   grpc_core::Server* core_server = grpc_core::Server::FromC(g_ctx.server);
94   CHECK(GRPC_LOG_IF_ERROR(
95       "SetupTransport",
96       core_server->SetupTransport(transport, nullptr,
97                                   core_server->channel_args(), nullptr)));
98 }
99 
client_setup_transport(grpc_core::Transport * transport)100 static void client_setup_transport(grpc_core::Transport* transport) {
101   grpc_core::ExecCtx exec_ctx;
102   grpc_endpoint_add_to_pollset(g_ctx.ep->client,
103                                grpc_cq_pollset(g_ctx.client_cq));
104   grpc_arg authority_arg = grpc_channel_arg_string_create(
105       const_cast<char*>(GRPC_ARG_DEFAULT_AUTHORITY),
106       const_cast<char*>("test-authority"));
107   grpc_channel_args* args =
108       grpc_channel_args_copy_and_add(nullptr, &authority_arg, 1);
109   // TODO (pjaikumar): use GRPC_CLIENT_CHANNEL instead of
110   // GRPC_CLIENT_DIRECT_CHANNEL
111   g_ctx.client = (*grpc_core::ChannelCreate(
112                       "socketpair-target", grpc_core::ChannelArgs::FromC(args),
113                       GRPC_CLIENT_DIRECT_CHANNEL, transport))
114                      .release()
115                      ->c_ptr();
116   grpc_channel_args_destroy(args);
117 }
118 
init_client()119 static void init_client() {
120   grpc_core::ExecCtx exec_ctx;
121   grpc_core::Transport* transport;
122   transport = grpc_create_chttp2_transport(
123       grpc_core::ChannelArgs(),
124       grpc_core::OrphanablePtr<grpc_endpoint>(g_ctx.ep->client), true);
125   client_setup_transport(transport);
126   CHECK(g_ctx.client);
127   grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr,
128                                       nullptr);
129 }
130 
init_server()131 static void init_server() {
132   grpc_core::ExecCtx exec_ctx;
133   grpc_core::Transport* transport;
134   CHECK(!g_ctx.server);
135   g_ctx.server = grpc_server_create(nullptr, nullptr);
136   grpc_server_register_completion_queue(g_ctx.server, g_ctx.cq, nullptr);
137   grpc_server_start(g_ctx.server);
138   transport = grpc_create_chttp2_transport(
139       grpc_core::ChannelArgs(),
140       grpc_core::OrphanablePtr<grpc_endpoint>(g_ctx.ep->server), false);
141   server_setup_transport(transport);
142   grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr,
143                                       nullptr);
144 }
145 
test_init()146 static void test_init() {
147   grpc_endpoint_pair* sfd =
148       static_cast<grpc_endpoint_pair*>(gpr_malloc(sizeof(grpc_endpoint_pair)));
149   memset(&g_ctx, 0, sizeof(g_ctx));
150   g_ctx.ep = sfd;
151   g_ctx.cq = grpc_completion_queue_create_for_next(nullptr);
152   g_ctx.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
153   g_ctx.bound_cq = grpc_completion_queue_create_for_next(nullptr);
154   g_ctx.client_cq = grpc_completion_queue_create_for_next(nullptr);
155 
156   // Create endpoints
157   *sfd = grpc_iomgr_create_endpoint_pair("fixture", nullptr);
158   // Create client, server and setup transport over endpoint pair
159   init_server();
160   init_client();
161 }
162 
drain_cq(grpc_completion_queue * cq)163 static void drain_cq(grpc_completion_queue* cq) {
164   grpc_event event;
165   do {
166     event = grpc_completion_queue_next(cq, grpc_timeout_seconds_to_deadline(1),
167                                        nullptr);
168   } while (event.type != GRPC_QUEUE_SHUTDOWN);
169 }
170 
drain_and_destroy_cq(grpc_completion_queue * cq)171 static void drain_and_destroy_cq(grpc_completion_queue* cq) {
172   grpc_completion_queue_shutdown(cq);
173   drain_cq(cq);
174   grpc_completion_queue_destroy(cq);
175 }
176 
shutdown_server()177 static void shutdown_server() {
178   if (!g_ctx.server) return;
179   grpc_server_shutdown_and_notify(g_ctx.server, g_ctx.shutdown_cq, tag(1000));
180   CHECK(grpc_completion_queue_pluck(g_ctx.shutdown_cq, tag(1000),
181                                     grpc_timeout_seconds_to_deadline(1),
182                                     nullptr)
183             .type == GRPC_OP_COMPLETE);
184   grpc_server_destroy(g_ctx.server);
185   g_ctx.server = nullptr;
186 }
187 
shutdown_client()188 static void shutdown_client() {
189   if (!g_ctx.client) return;
190   grpc_channel_destroy(g_ctx.client);
191   g_ctx.client = nullptr;
192 }
193 
end_test()194 static void end_test() {
195   shutdown_server();
196   shutdown_client();
197 
198   drain_and_destroy_cq(g_ctx.cq);
199   drain_and_destroy_cq(g_ctx.client_cq);
200   drain_and_destroy_cq(g_ctx.bound_cq);
201   grpc_completion_queue_destroy(g_ctx.shutdown_cq);
202   gpr_free(g_ctx.ep);
203 }
204 
205 typedef enum fd_type { CLIENT_FD, SERVER_FD } fd_type;
206 
fd_type_str(fd_type fdtype)207 static const char* fd_type_str(fd_type fdtype) {
208   if (fdtype == CLIENT_FD) {
209     return "client";
210   } else if (fdtype == SERVER_FD) {
211     return "server";
212   } else {
213     grpc_core::Crash(absl::StrFormat("Unexpected fd_type %d", fdtype));
214   }
215 }
216 
_test_close_before_server_recv(fd_type fdtype)217 static void _test_close_before_server_recv(fd_type fdtype) {
218   grpc_core::ExecCtx exec_ctx;
219   grpc_call* call;
220   grpc_call* server_call;
221   grpc_event event;
222   grpc_slice request_payload_slice =
223       grpc_slice_from_copied_string("hello world");
224   grpc_slice response_payload_slice =
225       grpc_slice_from_copied_string("hello you");
226   grpc_byte_buffer* request_payload =
227       grpc_raw_byte_buffer_create(&request_payload_slice, 1);
228   grpc_byte_buffer* response_payload =
229       grpc_raw_byte_buffer_create(&response_payload_slice, 1);
230   LOG(INFO) << "Running test: test_close_" << fd_type_str(fdtype)
231             << "_before_server_recv";
232   test_init();
233 
234   grpc_op ops[6];
235   grpc_op* op;
236   grpc_metadata_array initial_metadata_recv;
237   grpc_metadata_array trailing_metadata_recv;
238   grpc_metadata_array request_metadata_recv;
239   grpc_byte_buffer* request_payload_recv = nullptr;
240   grpc_byte_buffer* response_payload_recv = nullptr;
241   grpc_call_details call_details;
242   grpc_status_code status = GRPC_STATUS__DO_NOT_USE;
243   grpc_call_error error;
244   grpc_slice details;
245 
246   gpr_timespec deadline = grpc_timeout_seconds_to_deadline(1);
247   call = grpc_channel_create_call(
248       g_ctx.client, nullptr, GRPC_PROPAGATE_DEFAULTS, g_ctx.client_cq,
249       grpc_slice_from_static_string("/foo"), nullptr, deadline, nullptr);
250   CHECK(call);
251 
252   grpc_metadata_array_init(&initial_metadata_recv);
253   grpc_metadata_array_init(&trailing_metadata_recv);
254   grpc_metadata_array_init(&request_metadata_recv);
255   grpc_call_details_init(&call_details);
256 
257   memset(ops, 0, sizeof(ops));
258   op = ops;
259   op->op = GRPC_OP_SEND_INITIAL_METADATA;
260   op->data.send_initial_metadata.count = 0;
261   op->flags = 0;
262   op->reserved = nullptr;
263   op++;
264   op->op = GRPC_OP_SEND_MESSAGE;
265   op->data.send_message.send_message = request_payload;
266   op->flags = 0;
267   op->reserved = nullptr;
268   op++;
269   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
270   op->flags = 0;
271   op->reserved = nullptr;
272   op++;
273   op->op = GRPC_OP_RECV_INITIAL_METADATA;
274   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
275   op->flags = 0;
276   op->reserved = nullptr;
277   op++;
278   op->op = GRPC_OP_RECV_MESSAGE;
279   op->data.recv_message.recv_message = &response_payload_recv;
280   op->flags = 0;
281   op->reserved = nullptr;
282   op++;
283   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
284   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
285   op->data.recv_status_on_client.status = &status;
286   op->data.recv_status_on_client.status_details = &details;
287   op->flags = 0;
288   op->reserved = nullptr;
289   op++;
290   error = grpc_call_start_batch(call, ops, static_cast<size_t>(op - ops),
291                                 tag(1), nullptr);
292   CHECK_EQ(error, GRPC_CALL_OK);
293 
294   error = grpc_server_request_call(g_ctx.server, &server_call, &call_details,
295                                    &request_metadata_recv, g_ctx.bound_cq,
296                                    g_ctx.cq, tag(101));
297   CHECK_EQ(error, GRPC_CALL_OK);
298   event = grpc_completion_queue_next(
299       g_ctx.cq, grpc_timeout_milliseconds_to_deadline(100), nullptr);
300   CHECK_EQ(event.success, 1);
301   CHECK(event.tag == tag(101));
302   CHECK(event.type == GRPC_OP_COMPLETE);
303 
304   memset(ops, 0, sizeof(ops));
305   op = ops;
306   op->op = GRPC_OP_SEND_INITIAL_METADATA;
307   op->data.send_initial_metadata.count = 0;
308   op->flags = 0;
309   op->reserved = nullptr;
310   op++;
311   op->op = GRPC_OP_RECV_MESSAGE;
312   op->data.recv_message.recv_message = &request_payload_recv;
313   op->flags = 0;
314   op->reserved = nullptr;
315   op++;
316 
317   grpc_endpoint_pair* sfd = g_ctx.ep;
318   int fd;
319   if (fdtype == SERVER_FD) {
320     fd = sfd->server->vtable->get_fd(sfd->server);
321   } else {
322     CHECK(fdtype == CLIENT_FD);
323     fd = sfd->client->vtable->get_fd(sfd->client);
324   }
325   // Connection is closed before the server receives the client's message.
326   close(fd);
327 
328   error = grpc_call_start_batch(server_call, ops, static_cast<size_t>(op - ops),
329                                 tag(102), nullptr);
330   CHECK_EQ(error, GRPC_CALL_OK);
331 
332   event = grpc_completion_queue_next(
333       g_ctx.bound_cq, grpc_timeout_milliseconds_to_deadline(100), nullptr);
334 
335   // Batch operation completes on the server side.
336   // event.success will be true if the op completes successfully.
337   // event.success will be false if the op completes with an error. This can
338   // happen due to a race with closing the fd resulting in pending writes
339   // failing due to stream closure.
340   //
341   CHECK(event.type == GRPC_OP_COMPLETE);
342   CHECK(event.tag == tag(102));
343 
344   event = grpc_completion_queue_next(
345       g_ctx.client_cq, grpc_timeout_milliseconds_to_deadline(100), nullptr);
346   // When the client fd is closed, the server gets EPIPE.
347   // When server fd is closed, server gets EBADF.
348   // In both cases server sends GRPC_STATUS_UNAVALABLE to the client. However,
349   // the client may not receive this grpc_status as it's socket is being closed.
350   // If the client didn't get grpc_status from the server it will time out
351   // waiting on the completion queue. So there 2 2 possibilities:
352   // 1. client times out waiting for server's response
353   // 2. client receives GRPC_STATUS_UNAVAILABLE from server
354   //
355   if (event.type == GRPC_QUEUE_TIMEOUT) {
356     CHECK_EQ(event.success, 0);
357     // status is not initialized
358     CHECK_EQ(status, GRPC_STATUS__DO_NOT_USE);
359   } else {
360     CHECK(event.type == GRPC_OP_COMPLETE);
361     CHECK_EQ(event.success, 1);
362     CHECK(event.tag == tag(1));
363     CHECK_EQ(status, GRPC_STATUS_UNAVAILABLE);
364   }
365 
366   grpc_metadata_array_destroy(&initial_metadata_recv);
367   grpc_metadata_array_destroy(&trailing_metadata_recv);
368   grpc_metadata_array_destroy(&request_metadata_recv);
369   grpc_call_details_destroy(&call_details);
370 
371   grpc_call_unref(call);
372   grpc_call_unref(server_call);
373 
374   grpc_byte_buffer_destroy(request_payload);
375   grpc_byte_buffer_destroy(response_payload);
376   grpc_byte_buffer_destroy(request_payload_recv);
377   grpc_byte_buffer_destroy(response_payload_recv);
378 
379   end_test();
380 }
381 
test_close_before_server_recv()382 static void test_close_before_server_recv() {
383   // Close client side of the connection before server receives message from
384   // client
385   _test_close_before_server_recv(CLIENT_FD);
386   // Close server side of the connection before server receives message from
387   // client
388   _test_close_before_server_recv(SERVER_FD);
389 }
390 
_test_close_before_server_send(fd_type fdtype)391 static void _test_close_before_server_send(fd_type fdtype) {
392   grpc_core::ExecCtx exec_ctx;
393   grpc_call* call;
394   grpc_call* server_call;
395   grpc_event event;
396   grpc_slice request_payload_slice =
397       grpc_slice_from_copied_string("hello world");
398   grpc_slice response_payload_slice =
399       grpc_slice_from_copied_string("hello you");
400   grpc_byte_buffer* request_payload =
401       grpc_raw_byte_buffer_create(&request_payload_slice, 1);
402   grpc_byte_buffer* response_payload =
403       grpc_raw_byte_buffer_create(&response_payload_slice, 1);
404   LOG(INFO) << "Running test: test_close_" << fd_type_str(fdtype)
405             << "_before_server_send";
406   test_init();
407 
408   grpc_op ops[6];
409   grpc_op* op;
410   grpc_metadata_array initial_metadata_recv;
411   grpc_metadata_array trailing_metadata_recv;
412   grpc_metadata_array request_metadata_recv;
413   grpc_byte_buffer* request_payload_recv = nullptr;
414   grpc_byte_buffer* response_payload_recv = nullptr;
415   grpc_call_details call_details;
416   grpc_status_code status = GRPC_STATUS__DO_NOT_USE;
417   grpc_call_error error;
418   grpc_slice details;
419   int was_cancelled = 2;
420 
421   gpr_timespec deadline = grpc_timeout_seconds_to_deadline(1);
422   call = grpc_channel_create_call(
423       g_ctx.client, nullptr, GRPC_PROPAGATE_DEFAULTS, g_ctx.client_cq,
424       grpc_slice_from_static_string("/foo"), nullptr, deadline, nullptr);
425   CHECK(call);
426 
427   grpc_metadata_array_init(&initial_metadata_recv);
428   grpc_metadata_array_init(&trailing_metadata_recv);
429   grpc_metadata_array_init(&request_metadata_recv);
430   grpc_call_details_init(&call_details);
431 
432   memset(ops, 0, sizeof(ops));
433   op = ops;
434   op->op = GRPC_OP_SEND_INITIAL_METADATA;
435   op->data.send_initial_metadata.count = 0;
436   op->flags = 0;
437   op->reserved = nullptr;
438   op++;
439   op->op = GRPC_OP_SEND_MESSAGE;
440   op->data.send_message.send_message = request_payload;
441   op->flags = 0;
442   op->reserved = nullptr;
443   op++;
444   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
445   op->flags = 0;
446   op->reserved = nullptr;
447   op++;
448   op->op = GRPC_OP_RECV_INITIAL_METADATA;
449   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
450   op->flags = 0;
451   op->reserved = nullptr;
452   op++;
453   op->op = GRPC_OP_RECV_MESSAGE;
454   op->data.recv_message.recv_message = &response_payload_recv;
455   op->flags = 0;
456   op->reserved = nullptr;
457   op++;
458   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
459   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
460   op->data.recv_status_on_client.status = &status;
461   op->data.recv_status_on_client.status_details = &details;
462   op->flags = 0;
463   op->reserved = nullptr;
464   op++;
465   error = grpc_call_start_batch(call, ops, static_cast<size_t>(op - ops),
466                                 tag(1), nullptr);
467   CHECK_EQ(error, GRPC_CALL_OK);
468 
469   error = grpc_server_request_call(g_ctx.server, &server_call, &call_details,
470                                    &request_metadata_recv, g_ctx.bound_cq,
471                                    g_ctx.cq, tag(101));
472   CHECK_EQ(error, GRPC_CALL_OK);
473   event = grpc_completion_queue_next(
474       g_ctx.cq, grpc_timeout_milliseconds_to_deadline(100), nullptr);
475   CHECK_EQ(event.success, 1);
476   CHECK(event.tag == tag(101));
477   CHECK(event.type == GRPC_OP_COMPLETE);
478 
479   memset(ops, 0, sizeof(ops));
480   op = ops;
481   op->op = GRPC_OP_SEND_INITIAL_METADATA;
482   op->data.send_initial_metadata.count = 0;
483   op->flags = 0;
484   op->reserved = nullptr;
485   op++;
486   op->op = GRPC_OP_RECV_MESSAGE;
487   op->data.recv_message.recv_message = &request_payload_recv;
488   op->flags = 0;
489   op->reserved = nullptr;
490   op++;
491   error = grpc_call_start_batch(server_call, ops, static_cast<size_t>(op - ops),
492                                 tag(102), nullptr);
493   CHECK_EQ(error, GRPC_CALL_OK);
494 
495   event = grpc_completion_queue_next(
496       g_ctx.bound_cq, grpc_timeout_milliseconds_to_deadline(100), nullptr);
497   CHECK(event.type == GRPC_OP_COMPLETE);
498   CHECK_EQ(event.success, 1);
499   CHECK(event.tag == tag(102));
500 
501   memset(ops, 0, sizeof(ops));
502   op = ops;
503   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
504   op->data.recv_close_on_server.cancelled = &was_cancelled;
505   op->flags = 0;
506   op->reserved = nullptr;
507   op++;
508   op->op = GRPC_OP_SEND_MESSAGE;
509   op->data.send_message.send_message = response_payload;
510   op->flags = 0;
511   op->reserved = nullptr;
512   op++;
513   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
514   op->data.send_status_from_server.trailing_metadata_count = 0;
515   op->data.send_status_from_server.status = GRPC_STATUS_OK;
516   grpc_slice status_details = grpc_slice_from_static_string("xyz");
517   op->data.send_status_from_server.status_details = &status_details;
518   op->flags = 0;
519   op->reserved = nullptr;
520   op++;
521 
522   grpc_endpoint_pair* sfd = g_ctx.ep;
523   int fd;
524   if (fdtype == SERVER_FD) {
525     fd = sfd->server->vtable->get_fd(sfd->server);
526   } else {
527     CHECK(fdtype == CLIENT_FD);
528     fd = sfd->client->vtable->get_fd(sfd->client);
529   }
530 
531   // Connection is closed before the server sends message and status to the
532   // client.
533   close(fd);
534   error = grpc_call_start_batch(server_call, ops, static_cast<size_t>(op - ops),
535                                 tag(103), nullptr);
536   CHECK_EQ(error, GRPC_CALL_OK);
537 
538   // Batch operation succeeds on the server side
539   event = grpc_completion_queue_next(
540       g_ctx.bound_cq, grpc_timeout_milliseconds_to_deadline(100), nullptr);
541   CHECK(event.type == GRPC_OP_COMPLETE);
542   CHECK_EQ(event.success, 1);
543   CHECK(event.tag == tag(103));
544 
545   event = grpc_completion_queue_next(
546       g_ctx.client_cq, grpc_timeout_milliseconds_to_deadline(100), nullptr);
547   // In both cases server sends GRPC_STATUS_UNAVALABLE to the client. However,
548   // the client may not receive this grpc_status as it's socket is being closed.
549   // If the client didn't get grpc_status from the server it will time out
550   // waiting on the completion queue
551   //
552   if (event.type == GRPC_OP_COMPLETE) {
553     CHECK_EQ(event.success, 1);
554     CHECK(event.tag == tag(1));
555     CHECK_EQ(status, GRPC_STATUS_UNAVAILABLE);
556   } else {
557     CHECK(event.type == GRPC_QUEUE_TIMEOUT);
558     CHECK_EQ(event.success, 0);
559     // status is not initialized
560     CHECK_EQ(status, GRPC_STATUS__DO_NOT_USE);
561   }
562   CHECK_EQ(was_cancelled, 0);
563 
564   grpc_metadata_array_destroy(&initial_metadata_recv);
565   grpc_metadata_array_destroy(&trailing_metadata_recv);
566   grpc_metadata_array_destroy(&request_metadata_recv);
567   grpc_call_details_destroy(&call_details);
568 
569   grpc_call_unref(call);
570   grpc_call_unref(server_call);
571 
572   grpc_byte_buffer_destroy(request_payload);
573   grpc_byte_buffer_destroy(response_payload);
574   grpc_byte_buffer_destroy(request_payload_recv);
575   grpc_byte_buffer_destroy(response_payload_recv);
576 
577   end_test();
578 }
579 
test_close_before_server_send()580 static void test_close_before_server_send() {
581   // Close client side of the connection before server sends message to client
582   //
583   _test_close_before_server_send(CLIENT_FD);
584   // Close server side of the connection before server sends message to client
585   //
586   _test_close_before_server_send(SERVER_FD);
587 }
588 
_test_close_before_client_send(fd_type fdtype)589 static void _test_close_before_client_send(fd_type fdtype) {
590   grpc_core::ExecCtx exec_ctx;
591   grpc_call* call;
592   grpc_event event;
593   grpc_slice request_payload_slice =
594       grpc_slice_from_copied_string("hello world");
595   grpc_slice response_payload_slice =
596       grpc_slice_from_copied_string("hello you");
597   grpc_byte_buffer* request_payload =
598       grpc_raw_byte_buffer_create(&request_payload_slice, 1);
599   grpc_byte_buffer* response_payload =
600       grpc_raw_byte_buffer_create(&response_payload_slice, 1);
601   LOG(INFO) << "Running test: test_close_" << fd_type_str(fdtype)
602             << "_before_client_send";
603   test_init();
604 
605   grpc_op ops[6];
606   grpc_op* op;
607   grpc_metadata_array initial_metadata_recv;
608   grpc_metadata_array trailing_metadata_recv;
609   grpc_metadata_array request_metadata_recv;
610   grpc_byte_buffer* request_payload_recv = nullptr;
611   grpc_byte_buffer* response_payload_recv = nullptr;
612   grpc_call_details call_details;
613   grpc_status_code status;
614   grpc_call_error error;
615   grpc_slice details;
616 
617   gpr_timespec deadline = grpc_timeout_seconds_to_deadline(1);
618   call = grpc_channel_create_call(
619       g_ctx.client, nullptr, GRPC_PROPAGATE_DEFAULTS, g_ctx.client_cq,
620       grpc_slice_from_static_string("/foo"), nullptr, deadline, nullptr);
621   CHECK(call);
622 
623   grpc_metadata_array_init(&initial_metadata_recv);
624   grpc_metadata_array_init(&trailing_metadata_recv);
625   grpc_metadata_array_init(&request_metadata_recv);
626   grpc_call_details_init(&call_details);
627 
628   memset(ops, 0, sizeof(ops));
629   op = ops;
630   op->op = GRPC_OP_SEND_INITIAL_METADATA;
631   op->data.send_initial_metadata.count = 0;
632   op->flags = 0;
633   op->reserved = nullptr;
634   op++;
635   op->op = GRPC_OP_SEND_MESSAGE;
636   op->data.send_message.send_message = request_payload;
637   op->flags = 0;
638   op->reserved = nullptr;
639   op++;
640   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
641   op->flags = 0;
642   op->reserved = nullptr;
643   op++;
644   op->op = GRPC_OP_RECV_INITIAL_METADATA;
645   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
646   op->flags = 0;
647   op->reserved = nullptr;
648   op++;
649   op->op = GRPC_OP_RECV_MESSAGE;
650   op->data.recv_message.recv_message = &response_payload_recv;
651   op->flags = 0;
652   op->reserved = nullptr;
653   op++;
654   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
655   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
656   op->data.recv_status_on_client.status = &status;
657   op->data.recv_status_on_client.status_details = &details;
658   op->flags = 0;
659   op->reserved = nullptr;
660   op++;
661 
662   grpc_endpoint_pair* sfd = g_ctx.ep;
663   int fd;
664   if (fdtype == SERVER_FD) {
665     fd = sfd->server->vtable->get_fd(sfd->server);
666   } else {
667     CHECK(fdtype == CLIENT_FD);
668     fd = sfd->client->vtable->get_fd(sfd->client);
669   }
670   // Connection is closed before the client sends a batch to the server
671   close(fd);
672 
673   error = grpc_call_start_batch(call, ops, static_cast<size_t>(op - ops),
674                                 tag(1), nullptr);
675   CHECK_EQ(error, GRPC_CALL_OK);
676 
677   // Status unavailable is returned to the client when client or server fd is
678   // closed
679   event = grpc_completion_queue_next(
680       g_ctx.client_cq, grpc_timeout_milliseconds_to_deadline(100), nullptr);
681   CHECK_EQ(event.success, 1);
682   CHECK(event.type == GRPC_OP_COMPLETE);
683   CHECK(event.tag == tag(1));
684   CHECK_EQ(status, GRPC_STATUS_UNAVAILABLE);
685 
686   // No event is received on the server
687   event = grpc_completion_queue_next(
688       g_ctx.cq, grpc_timeout_milliseconds_to_deadline(100), nullptr);
689   CHECK_EQ(event.success, 0);
690   CHECK(event.type == GRPC_QUEUE_TIMEOUT);
691 
692   grpc_slice_unref(details);
693   grpc_metadata_array_destroy(&initial_metadata_recv);
694   grpc_metadata_array_destroy(&trailing_metadata_recv);
695   grpc_metadata_array_destroy(&request_metadata_recv);
696   grpc_call_details_destroy(&call_details);
697 
698   grpc_call_unref(call);
699 
700   grpc_byte_buffer_destroy(request_payload);
701   grpc_byte_buffer_destroy(response_payload);
702   grpc_byte_buffer_destroy(request_payload_recv);
703   grpc_byte_buffer_destroy(response_payload_recv);
704 
705   end_test();
706 }
test_close_before_client_send()707 static void test_close_before_client_send() {
708   // Close client side of the connection before client sends message to server
709   //
710   _test_close_before_client_send(CLIENT_FD);
711   // Close server side of the connection before client sends message to server
712   //
713   _test_close_before_client_send(SERVER_FD);
714 }
715 
_test_close_before_call_create(fd_type fdtype)716 static void _test_close_before_call_create(fd_type fdtype) {
717   grpc_core::ExecCtx exec_ctx;
718   grpc_call* call;
719   grpc_event event;
720   test_init();
721 
722   gpr_timespec deadline = grpc_timeout_milliseconds_to_deadline(100);
723 
724   grpc_endpoint_pair* sfd = g_ctx.ep;
725   int fd;
726   if (fdtype == SERVER_FD) {
727     fd = sfd->server->vtable->get_fd(sfd->server);
728   } else {
729     CHECK(fdtype == CLIENT_FD);
730     fd = sfd->client->vtable->get_fd(sfd->client);
731   }
732   // Connection is closed before the client creates a call
733   close(fd);
734 
735   call = grpc_channel_create_call(
736       g_ctx.client, nullptr, GRPC_PROPAGATE_DEFAULTS, g_ctx.client_cq,
737       grpc_slice_from_static_string("/foo"), nullptr, deadline, nullptr);
738   CHECK(call);
739 
740   // Client and server time out waiting on their completion queues and nothing
741   // is sent or received
742   event = grpc_completion_queue_next(
743       g_ctx.client_cq, grpc_timeout_milliseconds_to_deadline(100), nullptr);
744   CHECK(event.type == GRPC_QUEUE_TIMEOUT);
745   CHECK_EQ(event.success, 0);
746 
747   event = grpc_completion_queue_next(
748       g_ctx.cq, grpc_timeout_milliseconds_to_deadline(100), nullptr);
749   CHECK(event.type == GRPC_QUEUE_TIMEOUT);
750   CHECK_EQ(event.success, 0);
751 
752   grpc_call_unref(call);
753   end_test();
754 }
755 
test_close_before_call_create()756 static void test_close_before_call_create() {
757   // Close client side of the connection before client creates a call
758   _test_close_before_call_create(CLIENT_FD);
759   // Close server side of the connection before client creates a call
760   _test_close_before_call_create(SERVER_FD);
761 }
762 
main(int argc,char ** argv)763 int main(int argc, char** argv) {
764   grpc::testing::TestEnvironment env(&argc, argv);
765   // Init grpc
766   grpc_init();
767   int iterations = 10;
768 
769   for (int i = 0; i < iterations; ++i) {
770     test_close_before_call_create();
771     test_close_before_client_send();
772     test_close_before_server_recv();
773     test_close_before_server_send();
774   }
775 
776   grpc_shutdown();
777 
778   return 0;
779 }
780 
781 #else  // GRPC_POSIX_SOCKET_TCP
782 
main(int argc,char ** argv)783 int main(int argc, char** argv) { return 1; }
784 
785 #endif  // GRPC_POSIX_SOCKET_TCP
786