code: def build_timeout_circuit(tor_state, reactor, path, timeout, using_guards=False):
      
      timed_circuit = []
      d = tor_state.build_circuit(routers=path, using_guards=using_guards)
  
      def get_circuit(c):
          timed_circuit.append(c)
          return c
  
      def trap_cancel(f):
          f.trap(defer.CancelledError)
          if timed_circuit:
              d2 = timed_circuit[0].close()
          else:
              d2 = defer.succeed(None)
          d2.addCallback(lambda _: Failure(CircuitBuildTimedOutError("circuit build timed out")))
          return d2
  
      d.addCallback(get_circuit)
      d.addCallback(lambda circ: circ.when_built())
      d.addErrback(trap_cancel)
  
      reactor.callLater(timeout, d.cancel)
      return d
docs: Build a new circuit within a timeout.
  
      CircuitBuildTimedOutError will be raised unless we receive a
      circuit build result (success or failure) within the `timeout`
      duration.
  
      :returns: a Deferred which fires when the circuit build succeeds (or
          fails to build).