Skip to content

Threading server should close connections on exit #1488

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
aaugustin opened this issue Aug 22, 2024 · 3 comments
Open

Threading server should close connections on exit #1488

aaugustin opened this issue Aug 22, 2024 · 3 comments

Comments

@aaugustin
Copy link
Member

Essentially, it should behave like the asyncio server. It could also gain the connections attribute.

@aaugustin
Copy link
Member Author

This would also be more elegant than f9cea9c.

@aaugustin
Copy link
Member Author

Implementing this may hit #1596.

@darkhaniop
Copy link

Hi, I just ran some tests to try to understand how the (sync) connections are handled in WebSockets (I described one in #1596).

So, what about the server side? I noticed that a server process with active connections also has some issues when exiting. I think graceful server shutdowns/restarts can be handled within the library.

I have an idea on how to fix it. Would it be okay to work on it?

Proposed approach

We can store a set of initiated threads/connections in the Server instance. Then, gracefully close all active connections in Server.shutdown(). This set, e.g. called connections, must also be accessible from conn_handler(), so we can initiate it within def serve() and either set the connections property of the newly created Server instance from this function, or (maybe more reasonable way) pass it as a constructor argument.

With this fix, it would be easier for users to implement graceful shutdowns/service restarts. Of course, threads can still be blocked in the handler functions, but that'd be a different issue.

What do you think?

darkhaniop added a commit to darkhaniop/websockets that referenced this issue Apr 6, 2025
* Implement sync server connection tracking.
* Add ServerConnection.close() call for exising connections on server
  shutdown. This is useful for cleanly terminating/restarting the server
  process.

Issue python-websockets#1488
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants