# Buggy code
def should_join_circuit(self, create_payload, previous_node_address):
    """
    Check whether we should join a circuit. Returns a future that fires with a boolean.
    """
    if self.settings.max_joined_circuits <= len(self.relay_from_to) + len(
        self.exit_sockets
    ):
        self.logger.warning(
            "too many relays (%d)", (len(self.relay_from_to) + len(self.exit_sockets))
        )
        return succeed(False)

    # Check whether we have a random open slot, if so, allocate this to this request.
    circuit_id = create_payload.circuit_id
    for index, slot in enumerate(self.random_slots):
        if not slot:
            self.random_slots[index] = circuit_id
            return succeed(True)

    # No random slots but this user might be allocated a competing slot.
    # Next, we request the token balance of the circuit initiator.
    self.logger.info("Requesting balance of circuit initiator!")
    balance_future = Future()
    self.request_cache.add(BalanceRequestCache(self, circuit_id, balance_future))

    # Temporarily add these values, otherwise we are unable to communicate with the previous hop.
    self.directions[circuit_id] = EXIT_NODE
    shared_secret, _, _ = self.crypto.generate_diffie_shared_secret(create_payload.key)
    self.relay_session_keys[circuit_id] = self.crypto.generate_session_keys(
        shared_secret
    )

    self.send_cell(
        Peer(create_payload.node_public_key, previous_node_address),
        BalanceRequestPayload(circuit_id, create_payload.identifier),
    )

    self.directions.pop(circuit_id, None)
    self.relay_session_keys.pop(circuit_id, None)

    return balance_future


# Traceback (RuntimeError)
[PID:27493] 2020-12-11 21:15:27,035 - ERROR <taskmanager:135> TriblerTunnelCommunity.done_cb(): Task resulted in error: This number is already in use '1503337235'
Traceback (most recent call last):
File "/home/vader/my_SRC/TRIBLER/tribler_ichorid/src/pyipv8/ipv8/taskmanager.py", line 131, in done_cb
future.result()
File "/home/vader/my_SRC/TRIBLER/tribler_ichorid/src/pyipv8/ipv8/messaging/anonymization/community.py", line 693, in on_create
result = await self.should_join_circuit(payload, source_address)
File "/home/vader/my_SRC/TRIBLER/tribler_ichorid/src/tribler-core/tribler_core/modules/tunnel/community/triblertunnel_community.py", line 219, in should_join_circuit
self.request_cache.add(BalanceRequestCache(self, circuit_id, balance_future))
File "/home/vader/my_SRC/TRIBLER/tribler_ichorid/src/tribler-core/tribler_core/modules/tunnel/community/caches.py", line 9, in __init__
super(BalanceRequestCache, self).__init__(community.request_cache, "balance-request", circuit_id)
File "/home/vader/my_SRC/TRIBLER/tribler_ichorid/src/pyipv8/ipv8/requestcache.py", line 20, in __init__
raise RuntimeError("This number is already in use '%s'" % number)
RuntimeError: This number is already in use '1503337235'

# Fixed code
def should_join_circuit(self, create_payload, previous_node_address):
    """
    Check whether we should join a circuit. Returns a future that fires with a boolean.
    """
    if self.settings.max_joined_circuits <= len(self.relay_from_to) + len(
        self.exit_sockets
    ):
        self.logger.warning(
            "too many relays (%d)", (len(self.relay_from_to) + len(self.exit_sockets))
        )
        return succeed(False)

    circuit_id = create_payload.circuit_id
    if self.request_cache.has("balance-request", circuit_id):
        self.logger.warning(
            "balance request already in progress for circuit %d", circuit_id
        )
        return succeed(False)

    # Check whether we have a random open slot, if so, allocate this to this request.
    for index, slot in enumerate(self.random_slots):
        if not slot:
            self.random_slots[index] = circuit_id
            return succeed(True)

    # No random slots but this user might be allocated a competing slot.
    # Next, we request the token balance of the circuit initiator.
    self.logger.info("Requesting balance of circuit initiator!")
    balance_future = Future()
    self.request_cache.add(BalanceRequestCache(self, circuit_id, balance_future))

    # Temporarily add these values, otherwise we are unable to communicate with the previous hop.
    self.directions[circuit_id] = EXIT_NODE
    shared_secret, _, _ = self.crypto.generate_diffie_shared_secret(create_payload.key)
    self.relay_session_keys[circuit_id] = self.crypto.generate_session_keys(
        shared_secret
    )

    self.send_cell(
        Peer(create_payload.node_public_key, previous_node_address),
        BalanceRequestPayload(circuit_id, create_payload.identifier),
    )

    self.directions.pop(circuit_id, None)
    self.relay_session_keys.pop(circuit_id, None)

    return balance_future