Skip to content

Commit 046c965

Browse files
committed
Fix GRPC server blocking interrupt of RSpec
The `GRPC::RpcServer` was run with `run_till_terminated_or_interrupted` listening on the `QUIT`, `INT` and `TERM` signals. This however replaced the original signal traps from RSpec, preventing its interruption after the GRPC server was started. To fix this, the signal traps are moved into the entrypoint, so they are not registered during RSpec and RSpec continues to handle the signals and shutting down the server.
1 parent 5631b39 commit 046c965

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

bin/grpc_server

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,24 @@
33

44
require_relative '../config/environment'
55

6-
Sagittarius::Grpc::Launcher.new.start_blocking
6+
launcher = Sagittarius::Grpc::Launcher.new
7+
8+
stop_signals = %w[QUIT INT TERM]
9+
stop_read, stop_write = IO.pipe
10+
11+
stop_signals.each do |signal|
12+
Signal.trap(signal) do
13+
stop_write.puts(signal)
14+
end
15+
end
16+
17+
launcher.start
18+
19+
while (readable_io = IO.select([stop_read]))
20+
signal = readable_io.first[0].gets.strip
21+
22+
if stop_signals.include?(signal)
23+
launcher.stop
24+
break
25+
end
26+
end

lib/sagittarius/grpc/launcher.rb

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Launcher
77

88
HOST = '0.0.0.0:50051'
99

10-
def load
10+
def create_server
1111
@server = GRPC::RpcServer.new(interceptors: [
1212
Sagittarius::Middleware::Grpc::Context.new,
1313
Sagittarius::Middleware::Grpc::Logger.new,
@@ -26,26 +26,21 @@ def load
2626
end
2727

2828
def run_server!
29-
load if @server.nil?
29+
create_server if @server.nil?
3030
logger.info('Running server')
31-
@server.run_till_terminated_or_interrupted(%w[QUIT INT TERM])
31+
@server.run_till_terminated_or_interrupted([])
3232
end
3333

3434
def run_stream_listener!
3535
GrpcStreamHandler.listen!
3636
end
3737

3838
def start
39-
load
39+
create_server
4040
@stream_thread = Thread.new { run_stream_listener! }
4141
@server_thread = Thread.new { run_server! }
4242
end
4343

44-
def start_blocking
45-
start
46-
@server_thread.join # Wait for the server thread to finish because the program would end otherwise
47-
end
48-
4944
def stop
5045
@server.stop
5146
@server_thread.join

0 commit comments

Comments
 (0)