Skip to content

disabling wsgi interface fails under python3 #1377

Closed

Description

Originally reported by: Tim Miller (Bitbucket: lashni, GitHub: Unknown)


http://docs.cherrypy.org/en/latest/advanced.html#no-need-for-the-wsgi-interface

Error is encountered when the example code from the link above is run under a python 3.4.3 virtualenv on archlinux, traceback triggers when the server is accessed. Error doesn't occur with python 2.7.10.

Happens with both current head and CherryPy 3.8.0.

[19/Jul/2015:17:41:01] ENGINE Listening for SIGUSR1.
[19/Jul/2015:17:41:01] ENGINE Listening for SIGTERM.
[19/Jul/2015:17:41:01] ENGINE Listening for SIGHUP.
[19/Jul/2015:17:41:01] ENGINE Bus STARTING
CherryPy Checker:
The Application mounted at '' has an empty config.

[19/Jul/2015:17:41:01] ENGINE Started monitor thread '_TimeoutMonitor'.
[19/Jul/2015:17:41:01] ENGINE Started monitor thread 'Autoreloader'.
[19/Jul/2015:17:41:01] ENGINE Serving on http://127.0.0.1:8080
[19/Jul/2015:17:41:01] ENGINE Bus STARTED
[19/Jul/2015:17:41:05] NATIVE_ADAPTER Traceback (most recent call last):
  File "/home/lashni/dev/serve/lib/python3.4/site-packages/cherrypy/_cpnative_server.py", line 27, in respond
    sn = cherrypy.tree.script_name(req.uri or "/")
  File "/home/lashni/dev/serve/lib/python3.4/site-packages/cherrypy/_cptree.py", line 257, in script_name
    path = path[:path.rfind("/")]
TypeError: 'str' does not support the buffer interface

ValueError('invalid literal for int() with base 10: "b\'5"',)
Traceback (most recent call last):
  File "/home/lashni/dev/serve/lib/python3.4/site-packages/cherrypy/_cpnative_server.py", line 27, in respond
    sn = cherrypy.tree.script_name(req.uri or "/")
  File "/home/lashni/dev/serve/lib/python3.4/site-packages/cherrypy/_cptree.py", line 257, in script_name
    path = path[:path.rfind("/")]
TypeError: 'str' does not support the buffer interface

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/lashni/dev/serve/lib/python3.4/site-packages/cherrypy/wsgiserver/wsgiserver3.py", line 1068, in communicate
    req.respond()
  File "/home/lashni/dev/serve/lib/python3.4/site-packages/cherrypy/wsgiserver/wsgiserver3.py", line 856, in respond
    self.server.gateway(self).respond()
  File "/home/lashni/dev/serve/lib/python3.4/site-packages/cherrypy/_cpnative_server.py", line 88, in respond
    self.send_response(s, h, b)
  File "/home/lashni/dev/serve/lib/python3.4/site-packages/cherrypy/_cpnative_server.py", line 101, in send_response
    req.send_headers()
  File "/home/lashni/dev/serve/lib/python3.4/site-packages/cherrypy/wsgiserver/wsgiserver3.py", line 912, in send_headers
    status = int(self.status[:3])
ValueError: invalid literal for int() with base 10: "b'5"

Activity

  1. ghost added on Apr 30, 2016
  2. sevmonster commented on May 27, 2016

    @sevmonster

    This also occurs for me, not tested under other Python versions.
    The fact that this has existed since as early as 3.8.0 up to 5.4.0 surprises me.
    OS is Win7, Python 3.5.1, CherryPy 5.4.0, not behind any other proxies or load balancers.

    Example server setup, based on my own:

    cherrypy.config.update({'global': {
        'server.socket_host':                   '0.0.0.0',
        'server.socket_port':                   8081,
    
        'server.https.socket_host':             '0.0.0.0',
        'server.https.socket_port':             444,
        'server.https.ssl_module':             'builtin',
        'server.https.ssl_certificate':        'example_cert.pem',
        'server.https.ssl_private_key':        'example_key.pem',
        'server.https.ssl_certificate_chain' : 'example_chain.pem',
    }})
    
    from cherrypy._cpnative_server import CPHTTPServer
    for s in (cherrypy.server, *cherrypy.servers.values()):
        s.httpserver = CPHTTPServer(s)

    Trace:

    >python -mexample_http_server
    [26/May/2016:18:41:41] ENGINE Bus STARTING
    [26/May/2016:18:41:41] ENGINE Started monitor thread 'Autoreloader'.
    [26/May/2016:18:41:41] ENGINE Started monitor thread '_TimeoutMonitor'.
    [26/May/2016:18:41:42] ENGINE Serving on http://0.0.0.0:8081
    [26/May/2016:18:41:42] ENGINE Serving on https://0.0.0.0:444
    [26/May/2016:18:41:42] ENGINE Bus STARTED
    
    ...making a request to either server...
    
    [26/May/2016:18:41:42] NATIVE_ADAPTER Traceback (most recent call last):
      File "C:\Python35\lib\site-packages\cherrypy\_cpnative_server.py", line 27, in respond
        sn = cherrypy.tree.script_name(req.uri or "/")
      File "C:\Python35\lib\site-packages\cherrypy\_cptree.py", line 257, in script_name
        path = path[:path.rfind("/")]
    TypeError: a bytes-like object is required, not 'str'
    
    ValueError('invalid literal for int() with base 10: "b\'5"',)
    Traceback (most recent call last):
      File "C:\Python35\lib\site-packages\cherrypy\_cpnative_server.py", line 27, in respond
        sn = cherrypy.tree.script_name(req.uri or "/")
      File "C:\Python35\lib\site-packages\cherrypy\_cptree.py", line 257, in script_name
        path = path[:path.rfind("/")]
    TypeError: a bytes-like object is required, not 'str'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "C:\Python35\lib\site-packages\cherrypy\wsgiserver\wsgiserver3.py", line 1091, in communicate
        req.respond()
      File "C:\Python35\lib\site-packages\cherrypy\wsgiserver\wsgiserver3.py", line 879, in respond
        self.server.gateway(self).respond()
      File "C:\Python35\lib\site-packages\cherrypy\_cpnative_server.py", line 88, in respond
        self.send_response(s, h, b)
      File "C:\Python35\lib\site-packages\cherrypy\_cpnative_server.py", line 101, in send_response
        req.send_headers()
      File "C:\Python35\lib\site-packages\cherrypy\wsgiserver\wsgiserver3.py", line 935, in send_headers
        status = int(self.status[:3])
    ValueError: invalid literal for int() with base 10: "b'5"
    
    ...NATIVE_ADAPTER traceback repeated 5 more times...

    I followed the docs to the letter, so I'm not entirely sure what the issue is.
    Happens for both server instances, as well as when instantiating and accessing just one.

  3. jaraco commented on Dec 27, 2016

    @jaraco
    Member

    I can confirm this issue persists even after the wsgiserver refactor:

      File "/Users/jaraco/Dropbox/code/public/cherrypy/cherrypy/_cpnative_server.py", line 27, in respond
        sn = cherrypy.tree.script_name(req.uri or '/')
      File "/Users/jaraco/Dropbox/code/public/cherrypy/cherrypy/_cptree.py", line 259, in script_name
        path = path[:path.rfind('/')]
    TypeError: a bytes-like object is required, not 'str'
    
    ValueError('invalid literal for int() with base 10: "b\'5"',)
    Traceback (most recent call last):
      File "/Users/jaraco/Dropbox/code/public/cherrypy/cherrypy/_cpnative_server.py", line 27, in respond
        sn = cherrypy.tree.script_name(req.uri or '/')
      File "/Users/jaraco/Dropbox/code/public/cherrypy/cherrypy/_cptree.py", line 259, in script_name
        path = path[:path.rfind('/')]
    TypeError: a bytes-like object is required, not 'str'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/Users/jaraco/Dropbox/code/public/cherrypy/cherrypy/wsgiserver/__init__.py", line 1408, in communicate
        req.respond()
      File "/Users/jaraco/Dropbox/code/public/cherrypy/cherrypy/wsgiserver/__init__.py", line 862, in respond
        self.server.gateway(self).respond()
      File "/Users/jaraco/Dropbox/code/public/cherrypy/cherrypy/_cpnative_server.py", line 88, in respond
        self.send_response(s, h, b)
      File "/Users/jaraco/Dropbox/code/public/cherrypy/cherrypy/_cpnative_server.py", line 101, in send_response
        req.send_headers()
      File "/Users/jaraco/Dropbox/code/public/cherrypy/cherrypy/wsgiserver/__init__.py", line 923, in send_headers
        status = int(self.status[:3])
    ValueError: invalid literal for int() with base 10: "b'5"
  4. webknjaz commented on Sep 29, 2017

    @webknjaz
    Member

    Hmm.. What about now + cheroot?

  5. Kodiologist commented on May 23, 2018

    @Kodiologist
    Contributor

    Looks like this bug is still there. I just hit it with Python 3.6.5 and CherryPy 15.0.0. Using the documentation's example:

        import cherrypy
    
        class Root(object):
            @cherrypy.expose
            def index(self):
                return "Hello World!"
    
        if __name__ == '__main__':
            from cherrypy._cpnative_server import CPHTTPServer
            cherrypy.server.httpserver = CPHTTPServer(cherrypy.server)
    
            cherrypy.quickstart(Root(), '/')

    the stack trace I get is:

        [23/May/2018:10:21:16] NATIVE_ADAPTER Traceback (most recent call last):
          File "/usr/local/lib/python3.6/dist-packages/cherrypy/_cpnative_server.py", line 30, in respond
            sn = cherrypy.tree.script_name(req.uri or '/')
          File "/usr/local/lib/python3.6/dist-packages/cherrypy/_cptree.py", line 268, in script_name
            path = path[:path.rfind('/')]
        TypeError: a bytes-like object is required, not 'str'
    
        ValueError('invalid literal for int() with base 10: "b\'5"',)
        Traceback (most recent call last):
          File "/usr/local/lib/python3.6/dist-packages/cherrypy/_cpnative_server.py", line 30, in respond
            sn = cherrypy.tree.script_name(req.uri or '/')
          File "/usr/local/lib/python3.6/dist-packages/cherrypy/_cptree.py", line 268, in script_name
            path = path[:path.rfind('/')]
        TypeError: a bytes-like object is required, not 'str'
    
        During handling of the above exception, another exception occurred:
    
        Traceback (most recent call last):
          File "/usr/local/lib/python3.6/dist-packages/cheroot/server.py", line 1193, in communicate
            req.respond()
          File "/usr/local/lib/python3.6/dist-packages/cheroot/server.py", line 997, in respond
            self.server.gateway(self).respond()
          File "/usr/local/lib/python3.6/dist-packages/cherrypy/_cpnative_server.py", line 91, in respond
            self.send_response(s, h, b)
          File "/usr/local/lib/python3.6/dist-packages/cherrypy/_cpnative_server.py", line 105, in send_response
            req.send_headers()
          File "/usr/local/lib/python3.6/dist-packages/cheroot/server.py", line 1061, in send_headers
            status = int(self.status[:3])
        ValueError: invalid literal for int() with base 10: "b'5"
  6. webknjaz commented on May 23, 2018

    @webknjaz
    Member

    @jaraco turns out, there's incorrect processing of bytes vs str in several places over this module.

    ValueError: invalid literal for int() with base 10: "b'5"

    It tried to "b'500 Internal Server Error'" get 3 digits from this thing, which looks like a repr.

    TypeError: a bytes-like object is required, not 'str'

    This originates in this line:

    sn = cherrypy.tree.script_name(req.uri or '/')


    req.uri is of type bytes as well as req.status, but something went wrong while passing those things back and forth.

  7. webknjaz commented on May 23, 2018

    @webknjaz
    Member

    I've started fixing some things, but still need some time/help: #1712

    /cc: @jaraco

  8. 33 remaining items

  9. added a commit that references this issue on Jun 22, 2019
    89a5a83
  10. webknjaz commented on Jun 22, 2019

    @webknjaz
    Member

    Apparently, a similar test already exists: https://github.com/cherrypy/cherrypy/pull/1712/files#diff-38455dfb545934fdb8aaae27bbfd32adR33. And it doesn't fail for a mysterious reason.

    cc @jaraco

  11. webknjaz commented on Jun 22, 2019

    @webknjaz
    Member

    I've submitted a fix here: #1785.
    It works for me when I use that reproducer. But I want to integrate a proper test before merging.

  12. added a commit that references this issue on Jun 23, 2019
    6719bb3
  13. webknjaz commented on Jun 23, 2019

    @webknjaz
    Member

    I've released the fix under v18.1.2 and v17.4.2.
    Thanks @Kodiologist for assistance with the reproducer!

  14. MSayfullin commented on Jul 13, 2019

    @MSayfullin

    It looks like this fix breaks server in some circumstances, @webknjaz
    After upgrading to the latest v18.1.2 our servers stop responding from time to time and we have such exceptions in log:

    [08/Jul/2019:17:00:48] ENGINE socket.error 1
    Traceback (most recent call last):
      File "/home/pt/.local/lib/python3.5/site-packages/cheroot/server.py", line 1219, in communicate
        req.parse_request()
      File "/home/pt/.local/lib/python3.5/site-packages/cheroot/server.py", line 689, in parse_request
        success = self.read_request_line()
      File "/home/pt/.local/lib/python3.5/site-packages/cheroot/server.py", line 728, in read_request_line
        request_line = self.rfile.readline()
      File "/home/pt/.local/lib/python3.5/site-packages/cheroot/server.py", line 277, in readline
        data = self.rfile.readline(256)
      File "/usr/lib/python3.5/_pyio.py", line 510, in readline
        b = self.read(nreadahead())
      File "/usr/lib/python3.5/_pyio.py", line 494, in nreadahead
        readahead = self.peek(1)
      File "/usr/lib/python3.5/_pyio.py", line 1062, in peek
        return self._peek_unlocked(size)
      File "/usr/lib/python3.5/_pyio.py", line 1069, in _peek_unlocked
        current = self.raw.read(to_read)
      File "/usr/lib/python3.5/socket.py", line 575, in readinto
        return self._sock.recv_into(b)
      File "/usr/lib/python3.5/ssl.py", line 929, in recv_into
        return self.read(nbytes, buffer)
      File "/usr/lib/python3.5/ssl.py", line 791, in read
        return self._sslobj.read(len, buffer)
      File "/usr/lib/python3.5/ssl.py", line 575, in read
        v = self._sslobj.read(len, buffer)
    ssl.SSLError: [SSL: DECRYPTION_FAILED_OR_BAD_RECORD_MAC] decryption failed or bad record mac (_ssl.c:1977)
    

    Sometimes there will be several of them, sometimes hundreds but at the end application becomes completely unresponsive. No CPU load, no additional memory consumption, nothing new in logs. All requests are just timing out.
    Downgrading back to v18.1.1 resolves this issue.

    This was happening sporadically but constantly. Most probably because of some specific combination of request data which, unfortunately, we couldn't recognize.

    We use Ubuntu 16.04 LTS.

  15. webknjaz commented on Jul 13, 2019

    @webknjaz
    Member

    @MSayfullin looks like that's a Cheroot issue which manifests itself with a combination with a certain version of the OpenSSL. Please create a new issue under Cheroot and fill in all of the required details and include a minimal reproducer if possible.

  16. MSayfullin commented on Jul 17, 2019

    @MSayfullin

    @webknjaz thanks for getting back to me.
    Unfortunately, we've already rolled back to a previous version of cherrypy and production environment doesn't fail anymore. We cannot reproduce it in a test environment as well because we didn't catch that specific combination of request params.

    What concerns me also is that according to a history of releases in GitHub cheroot's latest one is dated by January 14 and cherrypy version which works for us (v18.1.1) is released on March 27th. This makes me believe that both versions v18.1.1 and v18.1.2 use the same cheroot version and only change that could affect our app is PR #1785

  17. webknjaz commented on Jul 17, 2019

    @webknjaz
    Member

    This doesn't look related to that PR at all. Are you sure you used the same Cheroot version in both cases? Do you pin your env deps versions?

  18. MSayfullin commented on Jul 18, 2019

    @MSayfullin

    We didn't update or downgrade cheroot version and still use 6.2.4. Hope this helps, @webknjaz

  19. webknjaz commented on Jul 26, 2019

    @webknjaz
    Member

    Well, then my best guess is that you were updating something related to openssl on the OS level maybe.

  20. MSayfullin commented on Jul 26, 2019

    @MSayfullin

    That's the thing, only thing updated was cherrypy :)

  21. webknjaz commented on Jul 26, 2019

    @webknjaz
    Member

    Anyway, this needs a real reproducer and a separate issue. The info provided isn't sufficient to conclude any relation to this change.

  22. jaraco commented on Oct 4, 2019

    @jaraco
    Member

    I don't think we can realistically roll back this change, but do feel free to open a new bug (preferably with a means to reproduce the issue) to track the regression/degradation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions