diff options
Diffstat (limited to 'mastodon')
-rw-r--r-- | mastodon/Mastodon.py | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 5ba33d4..1d23fad 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -2475,7 +2475,7 @@ class Mastodon: | |||
2475 | if connection.status_code != 200: | 2475 | if connection.status_code != 200: |
2476 | raise MastodonNetworkError("Could not connect to streaming server: %s" % connection.reason) | 2476 | raise MastodonNetworkError("Could not connect to streaming server: %s" % connection.reason) |
2477 | return connection | 2477 | return connection |
2478 | connection = connect_func() | 2478 | connection = None |
2479 | 2479 | ||
2480 | # Async stream handler | 2480 | # Async stream handler |
2481 | class __stream_handle(): | 2481 | class __stream_handle(): |
@@ -2506,18 +2506,19 @@ class Mastodon: | |||
2506 | 2506 | ||
2507 | # Run until closed or until error if not autoreconnecting | 2507 | # Run until closed or until error if not autoreconnecting |
2508 | while self.running: | 2508 | while self.running: |
2509 | with closing(self.connection) as r: | 2509 | if not self.connection is None: |
2510 | try: | 2510 | with closing(self.connection) as r: |
2511 | listener.handle_stream(r) | 2511 | try: |
2512 | except (AttributeError, MastodonMalformedEventError, MastodonNetworkError) as e: | 2512 | listener.handle_stream(r) |
2513 | if not (self.closed or self.reconnect_async): | 2513 | except (AttributeError, MastodonMalformedEventError, MastodonNetworkError) as e: |
2514 | raise e | 2514 | if not (self.closed or self.reconnect_async): |
2515 | else: | 2515 | raise e |
2516 | if self.closed: | 2516 | else: |
2517 | self.running = False | 2517 | if self.closed: |
2518 | self.running = False | ||
2518 | 2519 | ||
2519 | # Reconnect loop. Try immediately once, then with delays on error. | 2520 | # Reconnect loop. Try immediately once, then with delays on error. |
2520 | if self.reconnect_async and not self.closed: | 2521 | if (self.reconnect_async and not self.closed) or self.connection is None: |
2521 | self.reconnecting = True | 2522 | self.reconnecting = True |
2522 | connect_success = False | 2523 | connect_success = False |
2523 | while not connect_success: | 2524 | while not connect_success: |
@@ -2527,6 +2528,8 @@ class Mastodon: | |||
2527 | if self.connection.status_code != 200: | 2528 | if self.connection.status_code != 200: |
2528 | time.sleep(self.reconnect_async_wait_sec) | 2529 | time.sleep(self.reconnect_async_wait_sec) |
2529 | connect_success = False | 2530 | connect_success = False |
2531 | exception = MastodonNetworkError("Could not connect to server.") | ||
2532 | listener.on_abort(exception) | ||
2530 | except: | 2533 | except: |
2531 | time.sleep(self.reconnect_async_wait_sec) | 2534 | time.sleep(self.reconnect_async_wait_sec) |
2532 | connect_success = False | 2535 | connect_success = False |
@@ -2543,6 +2546,7 @@ class Mastodon: | |||
2543 | return handle | 2546 | return handle |
2544 | else: | 2547 | else: |
2545 | # Blocking, never returns (can only leave via exception) | 2548 | # Blocking, never returns (can only leave via exception) |
2549 | connection = connect_func() | ||
2546 | with closing(connection) as r: | 2550 | with closing(connection) as r: |
2547 | listener.handle_stream(r) | 2551 | listener.handle_stream(r) |
2548 | 2552 | ||