Skip to content

feat: Add stop() method to Server for graceful shutdown and IOLoop control #284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

maboloshi
Copy link

This PR adds a stop() method to the livereload.Server class, enabling proactive and graceful shutdown of the development server—particularly important for threaded integrations such as Sublime Text plugins or testing environments.

✨ Key Changes:

  • Server.stop():

    • Closes all registered HTTP server instances (created via .listen())
    • Checks if the underlying Tornado IOLoop (which wraps asyncio) is still running via self.ioloop.asyncio_loop.is_running()
    • Uses self.ioloop.add_callback(self.ioloop.stop) to safely stop the loop from any thread
  • Thread-safe and robust: Designed to work reliably even when livereload is used in non-main threads

  • No breaking changes: This is an additive change; existing behavior remains untouched unless .stop() is explicitly called

✅ Benefits:

  • Enables Sublime Text plugins (or other threaded environments) to cleanly terminate livereload without lingering ports or resources
  • Prevents issues like OSError: address already in use when restarting livereload
  • Opens up the ability to build testable and restartable development tools on top of livereload

- Added a stop() method to livereload.Server to allow proactive shutdown
- Closes all listening HTTP servers and releases bound ports
- Uses asyncio_loop.is_running() to check IOLoop state safely
- Stops IOLoop via add_callback() to ensure thread-safe termination
- Enables better integration with threaded environments
maboloshi added a commit to maboloshi/LiveReloadPy that referenced this pull request Jul 10, 2025
• 对 livereload 上游项目添加停止功能补丁 (PR lepture/python-livereload#284)
• 在插件中直接调用 server.stop() 方法停止服务
• 移除插件中复杂的 IOLoop 直接操作
• 优化资源清理流程
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.

1 participant