Connection DoesNotExistsError:connection was closed in the middle of operation

Humankind 0 Reputation points
2025-02-26T12:19:59.53+00:00

Hi, Users are complaining "Connection DoesNotExistsError:connection was closed in the middle of operation" error while using a streamlit UI based web app deployed on Azure.

Thanks for help!

Azure Database for PostgreSQL
{count} votes

1 answer

Sort by: Most helpful
  1. Adithya Prasad K 10 Reputation points Microsoft External Staff
    2025-02-26T14:22:01.4266667+00:00

    Hi

    This error typically indicates that the connection to the database (likely Azure SQL Database or another data source) was unexpectedly terminated during an operation. This can happen due to various reasons, including:
    1.Network connectivity issues
    2.Timeout settings
    3.Resource limitations on the database
    4.Idle connections being closed by the server

    -Could you please Check Database Connection Settings: Ensure that your database connection string is correctly configured. Verify that the connection timeout settings are appropriate for your application
    -Implement Connection Resiliency: Use connection pooling and implement retry logic in your application to handle transient errors. This can help automatically reconnect when a connection is lost.
    -Increase Timeout Settings: If your operations take a long time, consider increasing the command timeout settings in your database connection configuration
    -Monitor Database Performance:Use Azure Monitor to check the performance metrics of your database. Look for high CPU usage, memory pressure, or other indicators that might suggest resource limitations
    -Review Application Logs: Check the logs of your Streamlit application for any additional error messages or context that might help diagnose the issue
    -Test Network Connectivity: Ensure that there are no network issues affecting connectivity to the database. You can use tools like ping or traceroute to diagnose network problems.
    -Consider Using Azure Functions or Azure App Service: If your application is under heavy load, consider using Azure Functions or Azure App Service to scale your application and manage connections more effectively.

    I would request you to refer the below mentioned links for more information 
    https://docs.python.org/3/library/asyncio.html
    https://docs.streamlit.io/
    I hope this information helps. Please do let us know if you have any further queries.  

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.