Skip to content

100% processor usage during GET have to wait 60s for response #1910

Closed

Description

@e-manuel

When GET request have to wait 60s for remote service response, processor usage increases to 100% - version 1 of "requests" worked in this case better.
GET is configured with "cert" data and "timeout=120" over SSL connection.

Activity

  1. Lukasa commented on Feb 11, 2014

    @Lukasa
    Member

    Do you have a publicly-accessible URL that I can test against?

  2. e-manuel commented on Feb 11, 2014

    @e-manuel
    Author

    More details: GET is fired by sub-process (multiprocessing) - GET is calling API exposed by GSM operator - by this API I am receiving SMS and MMS. Other tasks fired by sub-proceses are using CPU below 1% (server with 2 Xeons).
    I can expose on my server, over valid SSL, PHP script with 60s delay if this is what You mean?
    Unfortunately I can not give You access to API exposed by GSM operator :(

  3. Lukasa commented on Feb 11, 2014

    @Lukasa
    Member

    Hmm. In that case, can you show me your code? Feel free to remove identifying information (like URLs, header values etc.)

  4. e-manuel commented on Feb 11, 2014

    @e-manuel
    Author

    Here You are - direct call of requests.get from my library
    (which is launched by multiprocessing.Process(target=launch_function,
    args=('arg1', 'arg2')) ):

    p_par = {
    'login': 'xxx',
    'password': 'xxx',
    'serviceId': '0000',
    'timeout': 60000, # wait 60s for response
    'deleteContent': 'true',
    'manualconfirm': 'true'
    }

    p_uri_part = 'getsms.aspx'

    def __request_api2(self, p_par, p_uri_part=None):
    try:
    # GSM
    if self.__channel == '0':
    v_url = '{0}/{1}'.format(self.__API_0_URL, p_uri_part)
    v_crt = '{0}smsapi.crt'.format(self.__PATH_RES_SEC)
    v_key = '{0}smsapi.key'.format(self.__PATH_RES_SEC)
    try:
    v_rsp = requests.get(
    v_url, params=p_par
    ,cert=(v_crt, v_key)
    ,timeout=120

    ,verify='{0}ca_root_certificates.pem'.format(self.__PATH_RES_SEC)
    )
    v_res = v_rsp.text
    except Exception as e:
    self.__add_error('Error when invoking API: {0}'.format(str(e)))
    v_res = 0
    finally:
    return v_res

    ca_root_certificates.pem - is grabbed from Mozilla - CA bundle

    Best regards,
    Artur

  5. Lukasa commented on Feb 11, 2014

    @Lukasa
    Member

    GitHub's email formatting made that pretty difficult to read, so I copy-pasted it into a code block here:

    p_par = {
       'login': 'xxx',
       'password': 'xxx',
       'serviceId': '0000',
       'timeout': 60000, # wait 60s for response
       'deleteContent': 'true',
       'manualconfirm': 'true'
    }
    
    p_uri_part = 'getsms.aspx'
    
    def __request_api2(self, p_par, p_uri_part=None):
       try:
          # GSM
          if self.__channel == '0':
             v_url = '{0}/{1}'.format(self.__API_0_URL, p_uri_part)
             v_crt = '{0}smsapi.crt'.format(self.__PATH_RES_SEC)
             v_key = '{0}smsapi.key'.format(self.__PATH_RES_SEC)
             try:
                v_rsp = requests.get(
                   v_url, params=p_par
                  ,cert=(v_crt, v_key)
                  ,timeout=120
                  ,verify='{0}ca_root_certificates.pem'.format(self.__PATH_RES_SEC)
                )
                v_res = v_rsp.text
             except Exception as e:
                self.__add_error('Error when invoking API: {0}'.format(str(e)))
                v_res = 0
       finally:
          return v_res
  6. Lukasa commented on Feb 11, 2014

    @Lukasa
    Member

    Hmm, there's nothing obviously wrong with that. When I get a moment I'll try to reproduce something similar on my machine.

  7. t-8ch commented on Feb 11, 2014

    @t-8ch
    Contributor

    I am in my phone, so I'll be brief. I suspect this is pyopenssl interacting with tje timeout. Especially the makefile function in urllib3.contrib.pyopenssl busylooping when no data is being recieved

  8. Lukasa commented on Feb 11, 2014

    @Lukasa
    Member

    Wait, does it do that? That's pretty awful.

  9. t-8ch commented on Feb 11, 2014

    @t-8ch
    Contributor

    This is a guess (I think I stumbled upon this previously). I'll check it later. Maybe gevent is also an issue. Gevent and pyopenssl don't really work together.

  10. t-8ch commented on Feb 11, 2014

    @t-8ch
    Contributor

    Hm, I can't reproduce this at the moment.

    @e-manuel Could you give more information about your environment:

    • Python version
    • Operating System
    • Using gevent? (or the like)
    • Using pyOpenSSL?
    • Does it work without multiprocessing?
  11. e-manuel commented on Feb 11, 2014

    @e-manuel
    Author
    • Python 2.7.6
    • FreeBSD 9.0
    • installed gevent 1.0 but in this case is not used
    • I have installed py27- openssl-0.13
    • my test: GET fired from python prompt work the same way (100% CPU after a few seconds) as sub-process fired from multiprocessing library
  12. t-8ch commented on Feb 11, 2014

    @t-8ch
    Contributor

    Do you also have pyasn1 and ndg-httpsclient installed? (If they and pyOpenSSL are installed they trigger a different codepath)

  13. e-manuel commented on Feb 11, 2014

    @e-manuel
    Author

    Yes, I have: py27-asn1-0.1.4_1,1, py27-ndg_httpsclient-0.3.2, py-openssl 0.13 but following the FreeBSD ports tree:

    • py27-asn1 is required by py27-ndg_httpsclient
    • py27-ndg_httpsclient is required by py-urllib3 (mentioned by @t-8ch) and requires to run: py-openssl and py-asn1
      Of course, I can de-install any of them, but I like to keep full functionality of requests (SSL, cert. etc.). What I should to do - "to be or not to be, this is the question..." ;)
  14. t-8ch commented on Feb 11, 2014

    @t-8ch
    Contributor

    So I managed to reproduce it:

    Terminal 1:

    # netcat from the nmap project, should work with all ssl capable netcats
    # We complete the SSL handshake but don't send a HTTP response line!
    $ ncat --ssl -l 0.0.0.0 4000

    Code:

    request.get('https://127.0.0.1:4000', verify=False, timeout=120)

    It seems this only happens when using PyOpenSSL, timeouts and FreeBSD.
    I can't reproduce it any other way, permutating those conditions and on Linux.

    @e-manuel Pick your poison :-)

  15. t-8ch commented on Feb 11, 2014

    @t-8ch
    Contributor

    Manually waiting for the buffer to be ready seems to fix it:

    diff --git a/requests/packages/urllib3/contrib/pyopenssl.py b/requests/packages/urllib3/contrib/pyopenssl.py
    index d9bda15..67eeee4 100644
    --- a/requests/packages/urllib3/contrib/pyopenssl.py
    +++ b/requests/packages/urllib3/contrib/pyopenssl.py
    @@ -156,6 +156,7 @@ class fileobject(_fileobject):
                     try:
                         data = self._sock.recv(rbufsize)
                     except OpenSSL.SSL.WantReadError:
    +                    select.select([self._sock], [], [])
                         continue
                     if not data:
                         break
    @@ -183,6 +184,7 @@ class fileobject(_fileobject):
                     try:
                         data = self._sock.recv(left)
                     except OpenSSL.SSL.WantReadError:
    +                    select.select([self._sock], [], [])
                         continue
                     if not data:
                         break
    @@ -234,6 +236,7 @@ class fileobject(_fileobject):
                                     break
                                 buffers.append(data)
                         except OpenSSL.SSL.WantReadError:
    +                        select.select([self._sock], [], [])
                             continue
                         break
                     return "".join(buffers)
    @@ -244,6 +247,7 @@ class fileobject(_fileobject):
                     try:
                         data = self._sock.recv(self._rbufsize)
                     except OpenSSL.SSL.WantReadError:
    +                    select.select([self._sock], [], [])
                         continue
                     if not data:
                         break
    @@ -271,7 +275,8 @@ class fileobject(_fileobject):
                     try:
                         data = self._sock.recv(self._rbufsize)
                     except OpenSSL.SSL.WantReadError:
    -                        continue
    +                    select.select([self._sock], [], [])
    +                    continue
                     if not data:
                         break
                     left = size - buf_len

    (Insert lengthy rant about platform differences, especially concerning openssl here)

  16. 13 remaining items

  17. Lukasa commented on Feb 18, 2014

    @Lukasa
    Member

    Suits me. =)

  18. e-manuel commented on Feb 19, 2014

    @e-manuel
    Author

    @t-8ch You are great - now timeout works fine in every case - and may CPUs can "sleep" calmly ;)

  19. Lukasa commented on Mar 23, 2014

    @Lukasa
    Member

    This will be fixed in the next release of requests, so I'm closing this. Thanks for your help all!

  20. tgalery commented on Mar 26, 2014

    @tgalery

    Hi there, sorry to jump into this discussion, but I'm switching some legacy url resolver mechanism from urllib2 to requests 2.2.1 . Although requests works much better, the server's CPU started to scale in an erratic manner, whereas in the past it was kind of constant under 10% usage. We resolve, http and https urls, using a custom header and verify=True. Do you know if this is an issue and what might be causing it?

  21. Lukasa commented on Mar 26, 2014

    @Lukasa
    Member

    It's possible that this is your bug, if you're accessing HTTPS urls and have pyopenssl installed. If both of those things are true, try applying the above patch to your installed requests package and check if it resolves your problem.

  22. tgalery commented on Mar 26, 2014

    @tgalery

    Nope, I don't use pyopenssl and this also happens, when I don't set the verify option. I wonder whether there's any overhead associated with get methods, as opposed to initialising a Session object and have different threads calling send. If none of this matters substantially, are you aware of any other high CPU issues related to requests?

  23. Lukasa commented on Mar 26, 2014

    @Lukasa
    Member

    There's definitely overhead when using get: we create a Session object behind the scenes for each call.

    You should consider having a single Session object, but note that it's not really thread-safe. If using a single Session, you should either serialize access to it or have it run in its own thread and send messages in and out on queues.

    Otherwise, we're not aware of any high CPU issues and it'd be interesting to get diags if you still hit it.

  24. dav009 commented on Mar 28, 2014

    @dav009

    I have a similar issue. Basically there are cpu peaks after switching from urllib2 to request to make get and post petitions. Basically Im seeeing a lot of FUTEX WAIT after stracing my process.

    I'm a bit confused with respect of the default behaviour of the connection pool.

    • Is there a global Session by default shared among all the requests made?.
    • Do requests to a single host share the same connection in teh pool?
    • Is that shared connection in the pool blocking?
    • Could the pool cache size and pool size affecting?

    Thank you in advance

  25. Lukasa commented on Mar 28, 2014

    @Lukasa
    Member
    • No. As I said above, we create a Session object behind the scenes if you aren't using one yourself.
    • Not always. Connection pools belong to Sessions, so if you aren't using Sessions you aren't using connection pools. We then have a pool of connections to each host. Whether requests to that host re-use a connection or not depends on whether or not the connection is still open.
    • Requests does not perform non-blocking IO.
    • Could they affect what?
  26. dav009 commented on Mar 28, 2014

    @dav009

    Basically in the cpu peaks Im getting lots of futex(..., FUTEX_WAIT, 1, NULL)
    I thought that could be related to a some lock on some variables inside requests

  27. Lukasa commented on Mar 28, 2014

    @Lukasa
    Member

    There is some locking around the connection pool and the cookies dictionary, so it's certainly possible.

  28. locked as resolved and limited conversation to collaborators on Sep 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions