Skip to content

Commit e83b716

Browse files
committed
Fix occasionally occurring race condition causing an exception
1 parent 032ea35 commit e83b716

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

python_packages/jupyter_lsp/jupyter_lsp/session.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class LanguageServerSessionBase(
5555
thread = Instance(
5656
Thread, help="worker thread for running an event loop", allow_none=True
5757
)
58+
main_loop = Instance(
59+
IOLoop, help="the event loop of the main thread", allow_none=True)
5860
writer = Instance(LspStreamWriter, help="the JSON-RPC writer", allow_none=True)
5961
reader = Instance(LspStreamReader, help="the JSON-RPC reader", allow_none=True)
6062
from_lsp = Instance(
@@ -107,6 +109,7 @@ def start(self):
107109
108110
will return as soon as the session is ready for communication
109111
"""
112+
self.main_loop = IOLoop.current()
110113
self.started.clear()
111114
self.thread = Thread(target=anyio.run, kwargs={"func": self.run})
112115
self.thread.start()
@@ -121,6 +124,7 @@ def stop(self):
121124
# wait for the session to get cleaned up
122125
if self.thread and self.thread.is_alive():
123126
self.thread.join()
127+
self.main_loop = None
124128

125129
async def run(self):
126130
"""run this session in a cancel scope and clean everything up on cancellation
@@ -189,7 +193,7 @@ def now(self):
189193
return datetime.now(timezone.utc)
190194

191195
async def start_process(self, argv: List[str]):
192-
"""start the language server subprocess giben in argv"""
196+
"""start the language server subprocess given in argv"""
193197
self.process = await anyio.open_process(
194198
argv,
195199
stdin=subprocess.PIPE,
@@ -283,7 +287,8 @@ async def _broadcast_from_lsp(self):
283287
"""
284288
async for message in self.from_lsp:
285289
self.last_server_message_at = self.now()
286-
await self.parent.on_server_message(message, self)
290+
# handle message in the main thread's event loop
291+
self.main_loop.add_callback(self.parent.on_server_message, message, self)
287292
self.from_lsp.task_done()
288293

289294

0 commit comments

Comments
 (0)