Hello Python Community,
I am currently dealing with one persistent problem in the Python backend of my website, and I am hoping someone can help me understand what might be causing it. The core issue is that the website initially handles requests normally, but after the application has been running continuously for an extended period, API requests gradually become slower and eventually some requests stop receiving a response within the expected time. The website itself may still be reachable, but specific API endpoints become extremely slow or appear to hang until the application process is restarted. Restarting the Python application immediately restores normal response times, but the same behaviour eventually returns after the application has been running again for several hours. I am trying to determine what could cause a Python web application to gradually lose responsiveness without producing an obvious fatal exception or immediately terminating the process.
The application handles normal website requests as well as several API endpoints that retrieve and update information from a database. Under normal conditions, these endpoints respond quickly and consistently, and I do not see any unusual resource consumption when the application starts. However, as uptime increases, I can see the response time of certain requests gradually increasing even though the overall traffic to the website has not changed significantly. Eventually, requests to the affected endpoints can remain pending for a long time before the client receives a response. The rest of the server remains accessible during this period, so it does not appear to be a complete server outage. Restarting only the Python application is enough to return the API to normal operation, which makes me suspect that something inside the application process or one of the resources it manages is accumulating over time.
I have already started monitoring the Python process and have been checking memory usage, CPU usage, and the number of active requests while the problem develops. Memory consumption does increase gradually, although I have not yet established whether the increase is directly responsible for the slowdown. CPU usage is not consistently high when the API becomes unresponsive, which makes this different from a straightforward CPU-intensive operation. I have also checked the application logs and do not see a clear traceback immediately before the affected requests begin hanging. Some requests complete successfully while others remain pending, so the application is not completely dead. This partial degradation makes me wonder whether the process could be waiting on a resource such as a database connection, network operation, file descriptor, thread, or another object that is not being released correctly.
I have reviewed the application code for obvious resource-management issues and have tried to make sure that external resources are closed appropriately after they are used. Database operations are performed through a connection layer, and I am investigating whether connections are being returned correctly after every request. The application also makes requests to external services as part of certain API operations, so I am checking whether any of those calls could remain open longer than expected. However, I have not found one specific function that consistently causes the problem. The fact that the website works normally after a fresh application restart but becomes less responsive only after extended uptime makes me suspect there may be a resource leak or lifecycle issue that is difficult to see during short development tests.
I have also attempted to reproduce the problem in a development environment, but it is much harder to trigger there because the application does not run continuously under the same conditions as production. In production, the Python process remains active for long periods and receives requests throughout the day, which seems to make the problem more noticeable. I have considered whether the issue could be related to the web server or application server configuration, but I want to understand the Python-side behaviour first rather than randomly changing production settings. I am particularly interested in learning which Python profiling or diagnostic tools would be appropriate for capturing information while the application is still responsive and then comparing that information with the state of the process when requests begin hanging.
I would appreciate guidance from the Python community on how best to diagnose this type of gradual loss of responsiveness in a long-running Python web application. I would especially like advice on identifying leaked database connections, unclosed network resources, blocked threads, growing queues, excessive objects, or other resources that can accumulate without immediately causing an exception. If there are recommended Python profiling techniques, stack inspection methods, memory diagnostics, or production-safe approaches for identifying which requests or operations are waiting indefinitely, I would be grateful for suggestions. My goal is to find the underlying cause of the degradation and fix it properly so that the website’s API remains responsive during long periods of continuous operation without requiring me to restart the Python application manually. Sorry for long post!