Skip to content

Fix issue with WSGI container error#275

Merged
lepture merged 1 commit intolepture:masterfrom
johnnadratowski:fix-wsgi-req-err
Jun 23, 2024
Merged

Fix issue with WSGI container error#275
lepture merged 1 commit intolepture:masterfrom
johnnadratowski:fix-wsgi-req-err

Conversation

@johnnadratowski
Copy link
Copy Markdown
Contributor

I'm using Flask v3 and Python 3.11. When starting up my server I got the following error (and it seems I'm not the only one):

    Traceback (most recent call last):
      File "/Users/john/venv/ema-cSETeqck/lib/python3.11/site-packages/tornado/web.py", line 1767, in _execute
        result = self.prepare()
                 ^^^^^^^^^^^^^^
      File "/Users/john/venv/ema-cSETeqck/lib/python3.11/site-packages/tornado/web.py", line 3149, in prepare
        self.fallback(self.request)
      File "/Users/john/venv/ema-cSETeqck/lib/python3.11/site-packages/livereload/server.py", line 135, in __call__
        app_response = self.wsgi_app(WSGIContainer.environ(request), start_response)
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    TypeError: WSGIContainer.environ() missing 1 required positional argument: 'request'

This fix is simple, and fixes the issue on my system.

@nickdelgrosso
Copy link
Copy Markdown

This fix worked in my system as well, in my project using Flask as the wsgi app.

Copy link
Copy Markdown

@nickdelgrosso nickdelgrosso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Necessary Bugfix!

@nickdelgrosso
Copy link
Copy Markdown

For those waiting on this fix (mentioned in #270 ), I've written a monkeypatch using wrapt that did the job for now:

import wrapt

def patch_livereload_to_fix_bug_around_wsgi_support():  
    """
    This patch is to fix the error descrbed here: https://stackoverflow.com/questions/78039400/flask-with-livereload-and-tornado-causing-error-when-i-run-my-program
    This implements the fix proposed in https://github.com/lepture/python-livereload/pull/275
    """

    from livereload.server import WSGIContainer


    @wrapt.patch_function_wrapper('livereload.server', 'LiveScriptContainer.__call__')
    def patched_call(wrapped, instance, args, kwargs):
        """
        LiveScriptContainer.__call__ is trying to call an instance method of its parent class as a static method.
        The problem is that it was changed from a static method in some change, and this is what broke things.
        Here, we trick the __call__ method into using a WSGIContainer instance instead of the type.
        """
        wrapped.__globals__['WSGIContainer'] = WSGIContainer(wsgi_application=instance.wsgi_app)
        return wrapped(*args, **kwargs)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants