Why do I constantly see "Resetting dropped connection" when uploading data to my database?
Asked Answered
R

3

47

I'm uploading hundreds of millions of items to my database via a REST API from a cloud server on Heroku to a database in AWS EC2. I'm using Python and I am constantly seeing the following INFO log message in the logs.

[requests.packages.urllib3.connectionpool] [INFO] Resetting dropped connection: <hostname>

This "resetting of the dropped connection" seems to take many seconds (sometimes 30+ sec) before my code continues to execute again.

  • Firstly what exactly is happening here and why?
  • Secondly is there a way to stop the connection from dropping so that I am able to upload data faster?

Thanks for your help. Andrew.

Recruitment answered 5/3, 2015 at 0:27 Comment(0)
C
20

Requests uses Keep-Alive by default. Resetting dropped connection, from my understanding, means a connection that should be alive was dropped somehow. Possible reasons are:

  1. Server doesn't support Keep-Alive.
  2. There's no data transfer in established connections for a while, so server drops connections.

See https://mcmap.net/q/216810/-python-requests-speed-up-using-keep-alive for more details.

Cuticula answered 23/1, 2016 at 15:25 Comment(1)
The link to the documentation is outdated. You can find it here: keep-alive docs. Sadly I could not edit the answer directly due to the queue being full.Cockroach
F
14

The problem is really that the server has closed the connection even though the client has requested it be kept alive.

This is not necessarily because the server doesn't support keepalives, but could be that the server is configured to only allow a certain number of requests on a connection. This could be done to help spread out requests on different servers, but I think this practice is/was common as a practical defence against poorly written code that operates in the server (eg. PHP) that doesn't clean up after itself after serving a request (perhaps due to an error condition etc.)

If you think this is the case for you and you'd like to not see these logs (which are logged at INFO level), then you can add the following to quieten that part of the logging:

# Really don't need to hear about connections being brought up again after server has closed it
logging.getLogger("requests.packages.urllib3.connectionpool").setLevel(logging.WARNING)
Fortunato answered 7/6, 2016 at 11:51 Comment(1)
These are logged at DEBUG level in Python 3. DEBUG:urllib3.connectionpool:Resetting dropped connectionCryotherapy
B
5

This is common practice for services that expose RESTful APIs to avoid abuse (or DoS).
If you're stressing their API they'll drop your connection.
Try getting your script to sleep a bit every once in a while to avoid the drop.

Blandishments answered 5/3, 2015 at 5:52 Comment(1)
I use flask on a local machine and I often stress their API, can I prevent it from dropping the connection?Mousebird

© 2022 - 2024 — McMap. All rights reserved.