@@ -158,30 +158,33 @@ def connect_to_tor(launch_tor, circuit_build_timeout, control_port=None,
158158 if tor_overrides :
159159 tor_options .update (tor_overrides )
160160
161+ tor_config = txtorcon .TorConfig ()
162+
163+ # Update Tor config options from dictionary
164+ for key , value in tor_options .items ():
165+ setattr (tor_config , key , value )
166+
161167 if launch_tor :
162168 log .info ("Spawning a new Tor instance." )
163169 # TODO: Pass in data_dir directory so consensus can be cached
164- tor = yield txtorcon .launch (reactor )
170+ # Launch tor with config, in order to don't get CONF_CHANGED when
171+ # updating options that can't be changed while tor is running.
172+ tor = yield txtorcon .launch (reactor , _tor_config = tor_config )
165173 else :
166174 log .info ("Trying to connect to a running Tor instance." )
167175 if control_port :
168176 endpoint = endpoints .TCP4ClientEndpoint (reactor , "localhost" , control_port )
169177 else :
170178 endpoint = None
171179 tor = yield txtorcon .connect (reactor , endpoint )
180+ # TODO: check whether CONF_CHANGED will happen here or not because
181+ # we get the state later
182+ tor .config = tor_config
183+ tor .config .save ()
172184
173- # Get Tor state first to avoid a race conditions where CONF_CHANGED
174- # messages are received while Txtorcon is reading the consensus.
175- tor_state = yield tor .create_state ()
176-
177- # Get current TorConfig object
178- tor_config = yield tor .get_config ()
179185 wait_for_consensus = options_need_new_consensus (tor_config , tor_options )
180186
181- # Update Tor config options from dictionary
182- for key , value in tor_options .items ():
183- setattr (tor_config , key , value )
184- yield tor_config .save () # Send updated options to Tor
187+ tor_state = yield tor .create_state ()
185188
186189 if wait_for_consensus :
187190 yield wait_for_newconsensus (tor_state )
0 commit comments