I think self.connection should be initialized here. self.connection.add_timeout call (line 43) can occur before on_connect