| 710 | self._close() |
| 711 | |
| 712 | def _close(self): # noqa: C901 |
| 713 | if self._closed: |
| 714 | return |
| 715 | self._closed = True |
| 716 | self._coordinator_endpoint = None |
| 717 | |
| 718 | self._unregister_default() |
| 719 | |
| 720 | if self._heartbeat_sending_thread: |
| 721 | try: |
| 722 | self._heartbeat_sending_thread.join( |
| 723 | timeout=self._heartbeat_interval_seconds |
| 724 | ) |
| 725 | except RuntimeError: # ignore the "cannot join current thread" error |
| 726 | pass |
| 727 | self._heartbeat_sending_thread = None |
| 728 | |
| 729 | self._disconnected = True |
| 730 | |
| 731 | # close all interactive instances |
| 732 | for instance in self._interactive_instance_dict.values(): |
| 733 | try: |
| 734 | instance.close() |
| 735 | except Exception: |
| 736 | pass |
| 737 | self._interactive_instance_dict.clear() |
| 738 | |
| 739 | # close all learning instances |
| 740 | for instance in self._learning_instance_dict.values(): |
| 741 | try: |
| 742 | instance.close() |
| 743 | except Exception: |
| 744 | pass |
| 745 | self._learning_instance_dict.clear() |
| 746 | |
| 747 | if self._grpc_client: |
| 748 | try: |
| 749 | self._grpc_client.close() |
| 750 | except Exception: |
| 751 | pass |
| 752 | self._grpc_client = None |
| 753 | _session_dict.pop(self._session_id, None) |
| 754 | |
| 755 | # clean up |
| 756 | if self._config.coordinator.endpoint is None: |
| 757 | try: |
| 758 | if self._launcher: |
| 759 | self._launcher.stop() |
| 760 | except Exception: |
| 761 | pass |
| 762 | self._pod_name_list = [] |
| 763 | |
| 764 | def _close_interactive_instance(self, instance): |
| 765 | """Close an interactive instance.""" |