Websocket ConnectionClosedError

I was checking the stability of websocket connection and had kept the data retrieval and processing on for 85 symbols. At 13:21:03 today(29-Sep '23) I got stopped out of the connection with following message.
File “/usr/lib/python3.11/asyncio/streams.py”, line 727, in readexactly
raise exceptions.IncompleteReadError(incomplete, n)
asyncio.exceptions.IncompleteReadError: 0 bytes read on a total of 2 expected bytes

The above exception was the direct cause of the following exception:
.—

websockets.exceptions.ConnectionClosedError: no close frame received or sent
Why this error and is there a workaround? I was running script on Google Cloud

2 Likes

Same problem faced today

Please share the code snippet used to establish the connection and the message sent over the socket.

async def fetch_market_data(scrips):

# Create default SSL context
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE

# Configure OAuth2 access token for authorization
configuration = upstox_client.Configuration()

api_version = '2.0'

configuration.access_token = token

# Get market data feed authorization
response = get_market_data_feed_authorize(
    api_version, configuration)

# Connect to the WebSocket with SSL context
async with websockets.connect(response.data.authorized_redirect_uri, ssl=ssl_context) as websocket:
    print('Connection established')

    await asyncio.sleep(1)  # Wait for 1 second

    # Data to be sent over the WebSocket
    data = {
        "guid": "someguid",
        "method": "sub",
        "data": {
            "mode": "full",
            "instrumentKeys": scrips
        }
    }

    # Convert data to binary and send over WebSocket
    binary_data = json.dumps(data).encode('utf-8')
    await websocket.send(binary_data)

    # Continuously receive and decode data from WebSocket
    while True:
        message = await websocket.recv()
        decoded_data = decode_protobuf(message)

        # Convert the decoded data to a dictionary
        data_dict = MessageToDict(decoded_data)
        process_ticks_Upstox.delay(data_dict)

I have kept the code snippet close to the one provided in examples. process_ticks_Upstox.py is a celery job which uses Redis as broker. token is access_token assigned in the main module. scrips is a comma separated list of scrips for which need the streaming data. I had established connection at 9 am today and it was running okay till 13:21 when faced the error.

i also faces same kind of problem
is there any way to get disconnection callback then can able to reconnect to websocket
websockets always should have listners like OnConnect OnCloseConnection OnReconnection like will be there but here we just using raw socket if listners are available what is the method names used here
please resolve this thing

No issues today. Executed perfectly from 09:00 to 15:30. Did not make any changes in the program. Please let me know how can we catch the error and reconnect in case it reappears.

@prsh

Our team is actively working on this matter. We will release an update as soon as we have one.

Thank you for your cooperation!

any update here ?
as a hack , currently im reconnectly when it gets closed

async def main():
    while True:
        try:
            await fetch_market_data(configuration)
        except KeyboardInterrupt:
            pass
        finally:
            if 'websocket' in locals():
                await websockets.close()

We implemented the CDN configuration over the last week, and we are currently monitoring it. We will make any necessary adjustments based on our observations.

Kindly inform us if there have been any improvements in the WebSocket connection.

Today also i faced the same issue recently before also i faced this issue

@santhosh_kumar

Thank you for bringing this to our attention.

Could you please provide your UCC so that we can investigate the cause of the disconnection?

Thanks!

My id is 2GBUTU …
its happrning like 1 or 2 days in week 1 timer per day

@santhosh_kumar

We’re currently looking into this matter. We’ll update you as soon as we have more information. Please bear with us, as it might take some time.

Thank you for your patience and understanding.

Same happened with me today

websockets.exceptions.ConnectionClosedError: no close frame received or sent

Yesterday the code was working fine.

@Mandar_Salvi

The disconnect experienced was due to scheduled technical maintenance on our end and was anticipated. Please be assured that this is not a recurring issue.

Thank you!