lepture / python-livereload

livereload server in python
https://livereload.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
1.02k stars 150 forks source link

fix(#170): wait for the IOLoop to stop before attempting to close it #276

Closed kkostov closed 1 month ago

kkostov commented 4 months ago

Hello!

Thank you for this amazing library 🤩 !

It works really well when watching static assets and templates, however I noticed that changes to .py files fails due to a runtime error. This issue has been previously recorded in #170, and marked as "needs more info". In my Flask project, I'm able to reproduce it continuously on every change to a .py file in the project.

After following the comments on the ticket, I realized the technique to restart tornado can be improved in order to avoid the runtime error. This PR introduces a slight change in the order of disposing of resources:

From the docs

So to avoid the crash, the call to .close() must be after the call to .start() has returned.

 # When autoreload is triggered, initiate a shutdown of the IOLoop
add_reload_hook(lambda: IOLoop.instance().stop())
# The call to start() does not return until the IOLoop is stopped.
IOLoop.instance().start()
# Once the IOLoop is stopped, the IOLoop can be closed to free resources
IOLoop.current().close(all_fds=True)

ℹ️ The tornado docs include an additional recommendation on how to handle this scenario differently (without having to reboot the loop) but that may have other unintended side effects, so I didn't consider it further.

ℹ️ My setup is as follows:

davidparsson commented 1 month ago

@lepture, any chanses of getting this merged and released anytime soon? There seem to be many of us that are affected by the problem that this resolves.