This topic provides the answer to a frequently asked question (FAQ) about SDKs.

What do I do if the ErrCode:SDK.ServerUnreachable error occurs when I use SDKs?

Problem

The following error may occur when you use SDKs:

ErrCode:ServerUnreachable
ErrMsg:SocketTimeoutException has occurred on a socket read or accept.
Solution
  1. Specify the ClientToken parameter in the request to prevent the client from repeatedly sending the request.

    The following section provides the Python sample code:

    #!/usr/bin/env python
    # coding=utf-8
    import time
    import uuid
    
    from aliyunsdkcore.client import AcsClient
    from aliyunsdkcore.acs_exception.exceptions import ClientException, ServerException
    from aliyunsdkros.request.v20190910.CreateStackRequest import CreateStackRequest
    
    ACCESS_KEY_ID = ''
    ACCESS_KEY_SECRET = ''
    REGION_ID = 'cn-beijing'
    
    
    def generate_client_token(prefix, add_uuid, *suffix_list):
        if prefix:
            t = [prefix]
        else:
            t = []
        if add_uuid:
            t.append(str(uuid.uuid1())[:-13])
        t.extend(suffix_list)
        r = '_'.join(t)
        if len(r) > 64:
            r = r[:64]
        return r
    
    
    def retry_with_client_token(retry_interval=10, retry_time_out=300):
        def wrapper(fun):
            def retry_func(*args, **kwargs):
                retry_timeout = retry_time_out
                elapsed_time = 0
                # This parameter is used to ensure the idempotence of the request. You can use the client to generate the value, but you must make sure that it is unique among different requests. 
                kwargs['ClientToken'] = generate_client_token(None, True)
                while True:
                    try:
                        retry_timeout -= elapsed_time
                        return fun(*args, **kwargs)
                    except Exception as e:
                        if isinstance(e, ClientException):
                            if e.error_code != 'SDK.HttpError':
                                raise
                        elif isinstance(e, ServerException):
                            if e.error_code not in ('LastTokenProcessing', 'ServiceUnavailable'):
                                raise
                        else:
                            raise
    
                        time.sleep(retry_interval)
                        elapsed_time += retry_interval
                        if elapsed_time >= retry_timeout:
                            raise
    
            return retry_func
    
        return wrapper
    
    
    class RosClient(object):
        def __init__(self):
            self.client = AcsClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET, REGION_ID)
    
        def _send_request(self, request, params, **kwargs):
            request.set_accept_format('json')
            request.set_connect_timeout(10000)
            request.set_read_timeout(0.01)
            params.update(kwargs)
            for name, value in params.items():
                set_func = getattr(request, 'set_{}'.format(name))
                set_func(value)
            return self.client.do_action_with_exception(request)
    
        @retry_with_client_token(retry_interval=10, retry_time_out=300)
        def create_stack(self, params, **kwargs):
            request = CreateStackRequest()
            return self._send_request(request, params, **kwargs)
    
    
    if __name__ == '__main__':
        ros_client = RosClient()
        result = ros_client.create_stack(dict(
            StackName='test',
            TemplateBody="{\"ROSTemplateFormatVersion\": \"2015-09-01\"}"
        ))
        print('stack_id:', result)                  
  2. If the problem persists, submit a ticket.