• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env ruby
2
3# Copyright 2016 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
17require_relative './end2end_common'
18
19# Service that sleeps for a long time upon receiving an 'echo request'
20# Also, this calls it's callback upon receiving an RPC as a method
21# of synchronization/waiting for the child to start.
22class SleepingEchoServerImpl < Echo::EchoServer::Service
23  def initialize(received_rpc_callback)
24    @received_rpc_callback = received_rpc_callback
25  end
26
27  def echo(echo_req, _)
28    @received_rpc_callback.call
29    # sleep forever to get the client stuck waiting
30    sleep
31    Echo::EchoReply.new(response: echo_req.request)
32  end
33end
34
35def main
36  STDERR.puts 'start server'
37
38  client_started = false
39  client_started_mu = Mutex.new
40  client_started_cv = ConditionVariable.new
41  received_rpc_callback = proc do
42    client_started_mu.synchronize do
43      client_started = true
44      client_started_cv.signal
45    end
46  end
47
48  service_impl = SleepingEchoServerImpl.new(received_rpc_callback)
49  # RPCs against the server will all be hanging, so kill thread
50  # pool workers immediately rather than after waiting for a second.
51  rpc_server_args = { poll_period: 0, pool_keep_alive: 0 }
52  server_runner = ServerRunner.new(service_impl, rpc_server_args: rpc_server_args)
53  server_port = server_runner.run
54  STDERR.puts 'start client'
55  _, client_pid = start_client('killed_client_thread_client.rb',
56                               server_port)
57
58  client_started_mu.synchronize do
59    client_started_cv.wait(client_started_mu) until client_started
60  end
61
62  # SIGTERM the child process now that it's
63  # in the middle of an RPC (happening on a non-main thread)
64  Process.kill('SIGTERM', client_pid)
65  STDERR.puts 'sent shutdown'
66
67  begin
68    Timeout.timeout(10) do
69      Process.wait(client_pid)
70    end
71  rescue Timeout::Error
72    STDERR.puts "timeout wait for client pid #{client_pid}"
73    Process.kill('SIGKILL', client_pid)
74    Process.wait(client_pid)
75    STDERR.puts 'killed client child'
76    raise 'Timed out waiting for client process. ' \
77      'It likely hangs when killed while in the middle of an rpc'
78  end
79
80  client_exit_code = $CHILD_STATUS
81  if client_exit_code.termsig != 15 # SIGTERM
82    fail 'expected client exit from SIGTERM ' \
83      "but got child status: #{client_exit_code}"
84  end
85
86  server_runner.stop
87end
88
89main
90