diff options
Diffstat (limited to 'mastodon')
-rw-r--r-- | mastodon/Mastodon.py | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 0922a6f..64450b6 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -3610,7 +3610,8 @@ class Mastodon: | |||
3610 | 3610 | ||
3611 | def close(self): | 3611 | def close(self): |
3612 | self.closed = True | 3612 | self.closed = True |
3613 | self.connection.close() | 3613 | if not self.connection is None: |
3614 | self.connection.close() | ||
3614 | 3615 | ||
3615 | def is_alive(self): | 3616 | def is_alive(self): |
3616 | return self._thread.is_alive() | 3617 | return self._thread.is_alive() |
@@ -3621,6 +3622,14 @@ class Mastodon: | |||
3621 | else: | 3622 | else: |
3622 | return True | 3623 | return True |
3623 | 3624 | ||
3625 | def _sleep_attentive(self): | ||
3626 | if self._thread != threading.current_thread(): | ||
3627 | raise RuntimeError ("Illegal call from outside the stream_handle thread") | ||
3628 | time_remaining = self.reconnect_async_wait_sec | ||
3629 | while time_remaining>0 and not self.closed: | ||
3630 | time.sleep(0.5) | ||
3631 | time_remaining -= 0.5 | ||
3632 | |||
3624 | def _threadproc(self): | 3633 | def _threadproc(self): |
3625 | self._thread = threading.current_thread() | 3634 | self._thread = threading.current_thread() |
3626 | 3635 | ||
@@ -3642,16 +3651,26 @@ class Mastodon: | |||
3642 | self.reconnecting = True | 3651 | self.reconnecting = True |
3643 | connect_success = False | 3652 | connect_success = False |
3644 | while not connect_success: | 3653 | while not connect_success: |
3645 | connect_success = True | 3654 | if self.closed: |
3655 | # Someone from outside stopped the streaming | ||
3656 | self.running = False | ||
3657 | break | ||
3646 | try: | 3658 | try: |
3647 | self.connection = self.connect_func() | 3659 | the_connection = self.connect_func() |
3648 | if self.connection.status_code != 200: | 3660 | if the_connection.status_code != 200: |
3649 | time.sleep(self.reconnect_async_wait_sec) | 3661 | exception = MastodonNetworkError(f"Could not connect to server. " |
3650 | connect_success = False | 3662 | f"HTTP status: {the_connection.status_code}") |
3651 | exception = MastodonNetworkError("Could not connect to server.") | ||
3652 | listener.on_abort(exception) | 3663 | listener.on_abort(exception) |
3664 | self._sleep_attentive() | ||
3665 | if self.closed: | ||
3666 | # Here we have maybe a rare race condition. Exactly on connect, someone | ||
3667 | # stopped the streaming before. We close the previous established connection: | ||
3668 | the_connection.close() | ||
3669 | else: | ||
3670 | self.connection = the_connection | ||
3671 | connect_success = True | ||
3653 | except: | 3672 | except: |
3654 | time.sleep(self.reconnect_async_wait_sec) | 3673 | self._sleep_attentive() |
3655 | connect_success = False | 3674 | connect_success = False |
3656 | self.reconnecting = False | 3675 | self.reconnecting = False |
3657 | else: | 3676 | else: |