commit 97a8df9d0fcb40b242bc44f429b0b634b7ae47ae Author: Damian Johnson atagar@torproject.org Date: Mon Apr 11 09:33:55 2016 -0700
Add reconnect() controller method
While working on Nyx realized we need our connect() method to reconnect *and* authenticate. Without that multi-threaded access can do the following...
CONNECT GETINFO stuff AUTHENTICATE
That intermediate GETINFO causes tor to disconnect, failing our attempt to authenticate. We can't do this in our connect() method (since that's already part of our BaseController) so adding a new reconnect() method that does both. --- docs/change_log.rst | 1 + stem/control.py | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/docs/change_log.rst b/docs/change_log.rst index 4317307..1c6f491 100644 --- a/docs/change_log.rst +++ b/docs/change_log.rst @@ -49,6 +49,7 @@ The following are only available within Stem's `git repository * :func:`~stem.connection.connect` and :func:`~stem.control.Controller.from_port` now connect to both port 9051 (relay's default) and 9151 (Tor Browser's default) (:trac:`16075`) * :class:`~stem.exit_policy.ExitPolicy` support for *accept6/reject6* and **4/6* wildcards (:trac:`16053`) * Added `support for NETWORK_LIVENESS events <api/response.html#stem.response.events.NetworkLivenessEvent>`_ (:spec:`44aac63`) + * Added :func:`~stem.control.Controller.reconnect` to the :class:`~stem.control.Controller` * Added :func:`~stem.control.Controller.is_set` to the :class:`~stem.control.Controller` * Added :func:`~stem.control.Controller.is_user_traffic_allowed` to the :class:`~stem.control.Controller` * Added the replica attribute to the :class:`~stem.response.events.HSDescEvent` (:spec:`4989e73`) diff --git a/stem/control.py b/stem/control.py index 1437d2d..3243d1c 100644 --- a/stem/control.py +++ b/stem/control.py @@ -71,6 +71,7 @@ If you're fine with allowing your script to raise exceptions then this can be mo | +- from_socket_file - Provides a Controller based on a socket file connection. | |- authenticate - authenticates this controller with tor + |- reconnect - reconnects and authenticates to socket | |- get_info - issues a GETINFO query for a parameter |- get_version - provides our tor version @@ -1017,10 +1018,6 @@ class Controller(BaseController):
self.add_event_listener(_confchanged_listener, EventType.CONF_CHANGED)
- def connect(self): - super(Controller, self).connect() - self.clear_cache() - def close(self): # making a best-effort attempt to quit before detaching the socket if self.is_alive(): @@ -1042,6 +1039,22 @@ class Controller(BaseController): import stem.connection stem.connection.authenticate(self, *args, **kwargs)
+ def reconnect(self, *args, **kwargs): + """ + Reconnects and authenticates to our control socket. + + .. versionadded:: 1.5.0 + + :raises: + * :class:`stem.SocketError` if unable to re-establish socket + * :class:`stem.connection.AuthenticationFailure` if unable to authenticate + """ + + with self._msg_lock: + self.connect() + self.clear_cache() + self.authenticate(*args, **kwargs) + @with_default() def get_info(self, params, default = UNDEFINED, get_bytes = False): """