| 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 |
|
| 17 |
require_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. |
| 22 |
class 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 |
| 33 |
end |
| 34 |
|
| 35 |
def main |
| 36 |
STDERR.puts 'start server' |
| 37 |
|
| 38 |
received_rpc = false |
| 39 |
received_rpc_mu = Mutex.new |
| 40 |
received_rpc_cv = ConditionVariable.new |
| 41 |
received_rpc_callback = proc do |
| 42 |
received_rpc_mu.synchronize do |
| 43 |
received_rpc = true |
| 44 |
received_rpc_cv.signal |
| 45 |
end |
| 46 |
end |
| 47 |
|
| 48 |
service_impl = SleepingEchoServerImpl.new(received_rpc_callback) |
| 49 |
# RPCs against the server will all be freezing, 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_controller = ClientController.new( |
| 56 |
'killed_client_thread_client.rb', server_port) |
| 57 |
|
| 58 |
received_rpc_mu.synchronize do |
| 59 |
received_rpc_cv.wait(received_rpc_mu) until received_rpc |
| 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_controller.client_pid) |
| 65 |
STDERR.puts 'sent shutdown' |
| 66 |
|
| 67 |
begin |
| 68 |
Timeout.timeout(120) do |
| 69 |
Process.wait(client_controller.client_pid) |
| 70 |
end |
| 71 |
rescue Timeout::Error |
| 72 |
STDERR.puts "timeout wait for client pid #{client_controller.client_pid}" |
| 73 |
Process.kill('SIGKILL', client_controller.client_pid) |
| 74 |
Process.wait(client_controller.client_pid) |
| 75 |
STDERR.puts 'killed client child' |
| 76 |
raise 'Timed out waiting for client process. ' \ |
| 77 |
'It likely freezes 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 |
| 87 |
end |
| 88 |
|
| 89 |
main |