summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/vyos/configsession.py53
1 files changed, 29 insertions, 24 deletions
diff --git a/python/vyos/configsession.py b/python/vyos/configsession.py
index f2abd3a5b..609ad79b8 100644
--- a/python/vyos/configsession.py
+++ b/python/vyos/configsession.py
@@ -209,30 +209,10 @@ class ConfigSession(object):
self, self.finalize_vyconf, self._vyconf_session
)
- def __del__(self):
- if self.shared:
- return
- if not vyconf_backend():
- try:
- output = (
- subprocess.check_output(
- [CLI_SHELL_API, 'teardownSession'], env=self.__session_env
- )
- .decode()
- .strip()
- )
- if output:
- print(
- 'cli-shell-api teardownSession output for session {0}: {1}'.format(
- self.__session_id, output
- ),
- file=sys.stderr,
- )
- except Exception as e:
- print(
- 'Could not tear down session {0}: {1}'.format(self.__session_id, e),
- file=sys.stderr,
- )
+ if not self.shared and not self._vyconf_session:
+ self._finalizer = weakref.finalize(
+ self, self.finalize_legacy, self.__session_env.copy()
+ )
@classmethod
def finalize_vyconf(cls, session: VyconfSession):
@@ -242,6 +222,31 @@ class ConfigSession(object):
session.exit_config_mode()
session.teardown()
+ @classmethod
+ def finalize_legacy(cls, session_env):
+ try:
+ output = (
+ subprocess.check_output(
+ [CLI_SHELL_API, 'teardownSession'], env=session_env
+ )
+ .decode()
+ .strip()
+ )
+ if output:
+ print(
+ 'cli-shell-api teardownSession output for session {0}: {1}'.format(
+ session_env['SESSION_PID'], output
+ ),
+ file=sys.stderr,
+ )
+ except Exception as e:
+ print(
+ 'Could not tear down session {0}: {1}'.format(
+ session_env['SESSION_PID'], e
+ ),
+ file=sys.stderr,
+ )
+
def __run_command(self, cmd_list):
p = subprocess.Popen(
cmd_list,