Fix #7282 by replacing the shutdown_hook with the atexit module.
Merge pull request #7295 from nebffa/issue_7282

Fix #7282