taburineagle / NeewerLite-Python

NeewerLite-Python is an un-official cross-platform Neewer LED light control app - written in Python, originally based off of the NeewerLite macOS Swift project by @keefo (Xu Lian)
MIT License
61 stars 11 forks source link

Bug: Using Chrome to interface with HTTP server causes blocking of other HTTP traffic #10

Closed DanielBaulig closed 2 years ago

DanielBaulig commented 2 years ago

This is a little bit of an arcane bug, so exact steps to reproduce

Steps to reproduce:

  1. Start NeewerLite-Python using NeewerLite-Python.py --cli --http
  2. Send HTTP request using curl http://localhost:8080/NeewerLite-Python/list -v
  3. Verify that the request completes
  4. Visit http://localhost:8080/NeewerLite-Python/list in Chrome
  5. Verify that the page loads
  6. Send another HTTP request using curl http://localhost:8080/NeewerLite-Python/list -v
  7. See that the request hangs
  8. Refresh Chrome page
  9. See that the curl request completes
  10. Hit Ctrl + C in the NeewerLite-Python process
  11. See that NeewerLite-Python does not immediately quit
  12. Refresh the Chrome page
  13. See that NeewerLite-Python quits

My (completely unfounded) guess is that it might be related to connection keep-alive or something like that in Chrome, but then again, I'm just making wild guesses here.

taburineagle commented 2 years ago

This one's definitely arcane, and I checked through a few forum posts of people having the same issue with Chrome-based requests, but I think I have a solution. I changed the HTTP server implementation from a single-threaded BaseHTTPServer to a threaded ThreadingHTTPServer (the documentation even suggests this for clients that make multiple open requests) -

This is useful to handle web browsers pre-opening sockets, on which HTTPServer would wait indefinitely.

And changed the asyncio loop routine in the HTTP processor to create a new event loop for requests (as the "main" event loop doesn't exist in the separately created new threads) - everything seems to be running fine with those changes, but I want to test it in a few more situations before pushing a live commit.

taburineagle commented 2 years ago

OK, I've uploaded the latest commit - this changes the HTTP server implementation from the base HTTPServer to ThreadingHTTPServer, which allows multiple connections (I was testing it on Firefox and Chrome simultaneously on Windows, and on the both of them and Safari on Mac, with no HTTP hangups or application hangs on either), along with a few other changes to make this setup play nicely with how NeewerLite-Python/asyncio processes the commands.

taburineagle commented 2 years ago

I'm going to close this one out too @DanielBaulig as it's been a week - all is fine now in my testing using multiple browsers at the same time, but feel free to reopen if you come across an issue with this!