From 1c22c68e4b4469370ff78166f2f005545cdfdc50 Mon Sep 17 00:00:00 2001 From: Andrew Bulat Date: Thu, 9 Jan 2025 14:22:57 +0000 Subject: [PATCH] Fix race condition error in `http.get_rest_hosts` when `fallback_realtime_host` is set --- ably/http/http.py | 3 ++- test/unit/http_test.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/unit/http_test.py diff --git a/ably/http/http.py b/ably/http/http.py index e47ffb8f..6a5a4f71 100644 --- a/ably/http/http.py +++ b/ably/http/http.py @@ -146,7 +146,8 @@ def get_rest_hosts(self): if host is None: return hosts - if time.time() > self.__host_expires: + # unstore saved fallback host after fallbackRetryTimeout (RSC15f) + if self.__host_expires is not None and time.time() > self.__host_expires: self.__host = None self.__host_expires = None return hosts diff --git a/test/unit/http_test.py b/test/unit/http_test.py new file mode 100644 index 00000000..45f362ed --- /dev/null +++ b/test/unit/http_test.py @@ -0,0 +1,19 @@ +from ably import AblyRest + + +def test_http_get_rest_hosts_works_when_fallback_realtime_host_is_set(): + ably = AblyRest(token="foo") + ably.options.fallback_realtime_host = ably.options.get_rest_hosts()[0] + # Should not raise TypeError + hosts = ably.http.get_rest_hosts() + assert isinstance(hosts, list) + assert all(isinstance(host, str) for host in hosts) + + +def test_http_get_rest_hosts_works_when_fallback_realtime_host_is_not_set(): + ably = AblyRest(token="foo") + ably.options.fallback_realtime_host = None + # Should not raise TypeError + hosts = ably.http.get_rest_hosts() + assert isinstance(hosts, list) + assert all(isinstance(host, str) for host in hosts)