Skip to content

Commit f3cb29d

Browse files
authored
Make client read and write under mutex lock (#433)
1 parent 4f87ac6 commit f3cb29d

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

lib/ruby_lsp/ruby_lsp_rails/runner_client.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class EmptyMessageError < StandardError; end
4141

4242
sig { void }
4343
def initialize
44+
@mutex = T.let(Mutex.new, Mutex)
4445
# Spring needs a Process session ID. It uses this ID to "attach" itself to the parent process, so that when the
4546
# parent ends, the spring process ends as well. If this is not set, Spring will throw an error while trying to
4647
# set its own session ID
@@ -176,7 +177,9 @@ def send_message(request, params = nil)
176177
message[:params] = params if params
177178
json = message.to_json
178179

179-
@stdin.write("Content-Length: #{json.length}\r\n\r\n", json)
180+
@mutex.synchronize do
181+
@stdin.write("Content-Length: #{json.length}\r\n\r\n", json)
182+
end
180183
rescue Errno::EPIPE
181184
# The server connection died
182185
end
@@ -187,13 +190,16 @@ def send_notification(request, params = nil) = send_message(request, params)
187190

188191
sig { overridable.returns(T.nilable(T::Hash[Symbol, T.untyped])) }
189192
def read_response
190-
headers = @stdout.gets("\r\n\r\n")
191-
raise IncompleteMessageError unless headers
193+
raw_response = @mutex.synchronize do
194+
headers = @stdout.gets("\r\n\r\n")
195+
raise IncompleteMessageError unless headers
196+
197+
content_length = headers[/Content-Length: (\d+)/i, 1].to_i
198+
raise EmptyMessageError if content_length.zero?
192199

193-
content_length = headers[/Content-Length: (\d+)/i, 1].to_i
194-
raise EmptyMessageError if content_length.zero?
200+
@stdout.read(content_length)
201+
end
195202

196-
raw_response = @stdout.read(content_length)
197203
response = JSON.parse(T.must(raw_response), symbolize_names: true)
198204

199205
if response[:error]

0 commit comments

Comments
 (0)