# Buggy code
def robust_connect_subscriber(
    conn, dest_addr, dest_port, pub_uri, receive_cb, resolved_topic_name
):
    """
    Keeps trying to create connection for subscriber.  Then passes off to receive_loop once connected.
    """
    # kwc: this logic is not very elegant.  I am waiting to rewrite
    # the I/O loop with async i/o to clean this up.

    # timeout is really generous. for now just choosing one that is large but not infinite
    interval = 0.5
    while conn.socket is None and not conn.done and not rospy.is_shutdown():
        try:
            conn.connect(dest_addr, dest_port, pub_uri, timeout=60.0)
        except rospy.exceptions.TransportInitError as e:
            rospyerr(
                "unable to create subscriber transport: %s.  Will try again in %ss",
                e,
                interval,
            )
            interval = interval * 2
            time.sleep(interval)

            # check to see if publisher state has changed
            conn.done = not check_if_still_publisher(resolved_topic_name, pub_uri)

    if not conn.done:
        conn.receive_loop(receive_cb)


# Traceback (AttributeError)
[rospy.internal][WARNING] 2014-11-11 20:39:42,119: Unknown error initiating TCP/IP socket to pv1106:39758 (http://pv1106:34792/): Traceback (most recent call last):
File "/data/users/rlinsalata/dev/desk/bugs/10848_mem-leak/rosbridge/catkin_ws/src/ros_comm/clients/rospy/src/rospy/impl/tcpros_base.py", line 557, in connect
self.read_header()
File "/data/users/rlinsalata/dev/desk/bugs/10848_mem-leak/rosbridge/catkin_ws/src/ros_comm/clients/rospy/src/rospy/impl/tcpros_base.py", line 618, in read_header
self._validate_header(read_ros_handshake_header(sock, self.read_buff, self.protocol.buff_size))
AttributeError: 'NoneType' object has no attribute 'buff_size'

[rospy.internal][INFO] 2014-11-11 20:39:42,119: topic[/robot/limb/left/endpoint_state] removing connection to http://pv1106:34792/
[rospy.internal][ERROR] 2014-11-11 20:39:42,119: unable to create subscriber transport: 'NoneType' object has no attribute 'buff_size'.  Will try again in 64.0s

# Fixed code
def robust_connect_subscriber(
    conn, dest_addr, dest_port, pub_uri, receive_cb, resolved_topic_name
):
    """
    Keeps trying to create connection for subscriber.  Then passes off to receive_loop once connected.
    """
    # kwc: this logic is not very elegant.  I am waiting to rewrite
    # the I/O loop with async i/o to clean this up.

    # timeout is really generous. for now just choosing one that is large but not infinite
    interval = 0.5
    while conn.socket is None and not conn.done and not rospy.is_shutdown():
        try:
            conn.connect(dest_addr, dest_port, pub_uri, timeout=60.0)
        except rospy.exceptions.TransportInitError as e:
            # if the connection was closed intentionally
            # because of an unknown error, stop trying
            if conn.protocol is None:
                conn.done = True
                break
            rospyerr(
                "unable to create subscriber transport: %s.  Will try again in %ss",
                e,
                interval,
            )
            interval = interval * 2
            time.sleep(interval)

            # check to see if publisher state has changed
            conn.done = not check_if_still_publisher(resolved_topic_name, pub_uri)

    if not conn.done:
        conn.receive_loop(receive_cb)