46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/debug_socket.rb', line 46
def self.start(path, &block)
pid = Process.pid
raise "debug socket thread already running for this process" if @thread && @pid == pid
@pid = pid
old_mask = File.umask(0o0177)
@path = path.to_s
server = UNIXServer.new(@path)
@thread = Thread.new do
errors = 0
loop do
socket = server.accept
input = socket.read
logger&.warn("debug-socket-command=#{input.inspect}")
perform_audit(input, &block) if block
socket.puts(Commands.isolated_eval(input))
errors = 0
rescue StandardError => e
errors += 1
logger&.error { "debug-socket-error=#{e.inspect} errors=#{errors} path=#{@path} backtrace=#{e.backtrace.inspect}" }
raise e if errors > 20
sleep(1)
ensure
socket&.close
end
rescue Exception => e logger&.error { "debug-socket-error=#{e.inspect} DebugSocket is broken now path=#{@path} backtrace=#{e.backtrace.inspect}" }
end
logger&.warn { "Debug socket listening on #{@path}" }
@thread
ensure
File.umask(old_mask) if old_mask
end
|