diff options
| author | John Estabrook <jestabro@vyos.io> | 2026-08-04 07:40:04 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-08-04 07:40:04 -0500 |
| commit | dcea6663811aa505bb0cf83f42bffa73408cb234 (patch) | |
| tree | fe0082a8bb85a6acf3cd23af3f2246ae5fc79fc9 /python | |
| parent | dd2673bf9ad36ea7ecb6998b71f318516b95e8d8 (diff) | |
| parent | 78387c7ebe51bf825db4c2468cff9f0a6c78c1ce (diff) | |
| download | vyos-1x-dcea6663811aa505bb0cf83f42bffa73408cb234.tar.gz vyos-1x-dcea6663811aa505bb0cf83f42bffa73408cb234.zip | |
Merge pull request #5369 from jestabro/configsession-finalizers
configsession: T9156: add consistent use of finalizers
Diffstat (limited to 'python')
| -rw-r--r-- | python/vyos/configsession.py | 53 |
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, |
