@@ -128,20 +128,23 @@ def stop(self):
128
128
self .main_loop = None
129
129
130
130
async def run (self ):
131
- """run this session in a cancel scope and clean everything up on cancellation
132
-
133
- the event `self.started` will be set when everything is set up and the session
134
- will be ready for communication
135
- """
131
+ """run this session in a task group and clean everything up on cancellation"""
136
132
self .thread_loop = IOLoop .current ()
137
- async with CancelScope () as scope :
138
- self .cancelscope = scope
139
- await self .initialize ()
140
- self .started .set ()
141
- await self .listen ()
142
- await self .cleanup ()
143
- self .cancelscope = None
144
- self .thread_loop = None
133
+
134
+ try :
135
+ async with anyio .create_task_group () as tg :
136
+ self .cancelscope = tg .cancel_scope
137
+ await self .initialize ()
138
+ self .started .set ()
139
+ tg .start_soon (self ._read_lsp )
140
+ tg .start_soon (self ._write_lsp )
141
+ tg .start_soon (self ._broadcast_from_lsp )
142
+ except Exception as e : # pragma: no cover
143
+ self .log .exception ("Execption while listening {}" , e )
144
+ finally :
145
+ await self .cleanup ()
146
+ self .cancelscope = None
147
+ self .thread_loop = None
145
148
146
149
async def initialize (self ):
147
150
"""initialize a language server session"""
@@ -154,16 +157,6 @@ async def initialize(self):
154
157
155
158
self .status = SessionStatus .STARTED
156
159
157
- async def listen (self ):
158
- """start the actual read/write tasks"""
159
- try :
160
- async with anyio .create_task_group () as tg :
161
- await tg .spawn (self ._read_lsp )
162
- await tg .spawn (self ._write_lsp )
163
- await tg .spawn (self ._broadcast_from_lsp )
164
- except Exception as e : # pragma: no cover
165
- self .log .exception ("Execption while listening {}" , e )
166
-
167
160
async def cleanup (self ):
168
161
"""clean up all of the state of the session"""
169
162
self .status = SessionStatus .STOPPING
0 commit comments