Skip to content

Commit

Permalink
fix: ensure ending a session triggers various listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl committed Sep 21, 2024
1 parent f4223d6 commit e624c07
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
7 changes: 7 additions & 0 deletions plugin/core/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,9 @@ def __getattr__(self, name: str) -> Any:
return attr
raise AttributeError(name)

def __del__(self) -> None:
print('[Session] __del__', self.config.name)

# TODO: Create an assurance that the API doesn't change here as it can be used by plugins.
def get_workspace_folders(self) -> list[WorkspaceFolder]:
return self._workspace_folders
Expand Down Expand Up @@ -2245,6 +2248,7 @@ def m___progress(self, params: ProgressParams) -> None:
# --- shutdown dance -----------------------------------------------------------------------------------------------

def end_async(self) -> None:
print('[Session] end_async', self.exiting)
# TODO: Ensure this function is called only from the async thread
if self.exiting:
return
Expand All @@ -2266,12 +2270,14 @@ def end_async(self) -> None:
watcher.destroy()
self._dynamic_file_watchers = {}
self.state = ClientStates.STOPPING
print('[Session] Sending shutdown')
self.send_request_async(Request.shutdown(), self._handle_shutdown_result, self._handle_shutdown_result)

def _handle_shutdown_result(self, _: Any) -> None:
self.exit()

def on_transport_close(self, exit_code: int, exception: Exception | None) -> None:
print('[Session] on_transport-close')
self.exiting = True
self.state = ClientStates.STOPPING
self.transport = None
Expand Down Expand Up @@ -2356,6 +2362,7 @@ def send_error_response(self, request_id: Any, error: Error) -> None:
def exit(self) -> None:
self.send_notification(Notification.exit())
try:
print('[Session] Closing transport')
self.transport.close() # type: ignore
except AttributeError:
pass
Expand Down
1 change: 1 addition & 0 deletions plugin/core/transports.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ def _end(self, exception: Exception | None) -> None:

def invoke() -> None:
callback_object = self._callback_object()
print('[ProcessTransport] end Transport', callback_object)
if callback_object:
callback_object.on_transport_close(exit_code, exception)

Expand Down
5 changes: 4 additions & 1 deletion plugin/core/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class WindowManager(Manager, WindowConfigChangeListener):
def __init__(self, window: sublime.Window, workspace: ProjectFolders, config_manager: WindowConfigManager) -> None:
self._window = window
self._config_manager = config_manager
self._sessions: WeakSet[Session] = WeakSet()
self._sessions: set[Session] = set()
self._workspace = workspace
self._pending_listeners: deque[AbstractViewListener] = deque()
self._listeners: WeakSet[AbstractViewListener] = WeakSet()
Expand Down Expand Up @@ -341,13 +341,15 @@ def handle_message_request(self, session: Session, params: Any, request_id: Any)
MessageRequestHandler(view, session, request_id, params, session.config.name).show()

def restart_sessions_async(self, config_name: str | None = None) -> None:
print('[WindowManager] restart_sessions_async')
self._end_sessions_async(config_name)
listeners = list(self._listeners)
self._listeners.clear()
for listener in listeners:
self.register_listener_async(listener)

def _end_sessions_async(self, config_name: str | None = None) -> None:
print('[WindowManager] end_session', config_name)
sessions = list(self._sessions)
for session in sessions:
if config_name is None or config_name == session.config.name:
Expand Down Expand Up @@ -384,6 +386,7 @@ def should_ignore_diagnostics(self, uri: DocumentUri, configuration: ClientConfi
return None

def on_post_exit_async(self, session: Session, exit_code: int, exception: Exception | None) -> None:
print('[WindowManager] on_post_exit_async', session.config.name)
self._sessions.discard(session)
for listener in self._listeners:
listener.on_session_shutdown_async(session)
Expand Down
2 changes: 2 additions & 0 deletions plugin/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def on_session_initialized_async(self, session: Session) -> None:

def on_session_shutdown_async(self, session: Session) -> None:
removed_session = self._session_views.pop(session.config.name, None)
print('[DocumentListener] on_session_shutdown_async', removed_session)
if removed_session:
removed_session.on_before_remove()
if not self._session_views:
Expand Down Expand Up @@ -1006,6 +1007,7 @@ def run_sync() -> None:
sublime.set_timeout(run_sync)

def _clear_session_views_async(self) -> None:
print('[DocumentListener] _clear_session_views_async')
session_views = self._session_views

def clear_async() -> None:
Expand Down

0 comments on commit e624c07

Please sign in to comment.