@@ -41,6 +41,7 @@ class EmptyMessageError < StandardError; end
41
41
42
42
sig { void }
43
43
def initialize
44
+ @mutex = T . let ( Mutex . new , Mutex )
44
45
# Spring needs a Process session ID. It uses this ID to "attach" itself to the parent process, so that when the
45
46
# parent ends, the spring process ends as well. If this is not set, Spring will throw an error while trying to
46
47
# set its own session ID
@@ -176,7 +177,9 @@ def send_message(request, params = nil)
176
177
message [ :params ] = params if params
177
178
json = message . to_json
178
179
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
180
183
rescue Errno ::EPIPE
181
184
# The server connection died
182
185
end
@@ -187,13 +190,16 @@ def send_notification(request, params = nil) = send_message(request, params)
187
190
188
191
sig { overridable . returns ( T . nilable ( T ::Hash [ Symbol , T . untyped ] ) ) }
189
192
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?
192
199
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
195
202
196
- raw_response = @stdout . read ( content_length )
197
203
response = JSON . parse ( T . must ( raw_response ) , symbolize_names : true )
198
204
199
205
if response [ :error ]
0 commit comments