Skip to content

Commit

Permalink
Merge pull request #195 from unihd-cag/194-cleanup-cache-on-close-fai…
Browse files Browse the repository at this point in the history
…lure

Don't fail on workspace close when channel close fails
  • Loading branch information
nielsbuwen authored Oct 6, 2022
2 parents aab5056 + 095f7c4 commit 747bf8c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion skillbridge/client/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def max_transmission_length(self, value: int) -> None:
def __del__(self) -> None:
try:
self.close()
except BrokenPipeError:
except: # noqa
pass

@staticmethod
Expand Down
13 changes: 11 additions & 2 deletions skillbridge/client/workspace.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
from functools import partial
from inspect import signature
from logging import getLogger
from textwrap import dedent
from typing import Any, Callable, Dict, Iterable, NoReturn, Optional, Union, cast

Expand All @@ -17,6 +18,9 @@
_open_workspaces: Dict[WorkspaceId, 'Workspace'] = {}


logger = getLogger(__file__)


class _NoWorkspace:
id = object()
is_current = False
Expand Down Expand Up @@ -253,8 +257,13 @@ def open(cls, workspace_id: WorkspaceId = None, direct: bool = False) -> 'Worksp
_open_workspaces[workspace_id] = Workspace(channel, workspace_id)
return _open_workspaces[workspace_id]

def close(self) -> None:
self._channel.close()
def close(self, log_exception: bool = True) -> None:
try:
self._channel.close()
except: # noqa
if log_exception:
logger.exception("Failed to close workspace")

_open_workspaces.pop(self.id, None)

if current_workspace.id == self.id:
Expand Down
29 changes: 29 additions & 0 deletions tests/test_workspace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from typing import Any

from skillbridge import Workspace
from skillbridge.client.channel import Channel
from skillbridge.client.translator import DefaultTranslator
from skillbridge.client.workspace import _open_workspaces


class DummyChannel(Channel):
def send(self, data: str) -> str:
pass

def flush(self) -> None:
pass

def try_repair(self) -> Any:
pass

def close(self):
raise RuntimeError("no, i won't close")


def test_a_crash_while_closing_still_clears_the_cache():
dummy_channel = DummyChannel(1)
ws = Workspace(channel=dummy_channel, id_=123, translator=DefaultTranslator())
_open_workspaces[123] = ws

ws.close()
assert 123 not in _open_workspaces

0 comments on commit 747bf8c

Please sign in to comment.