The easiest way is to simply replace the old process with the new,
improved version after stopping the listener for the old process.
The io_context is kept alive by handlers registered. Initially, this is
probably only the acceptor listening for incoming connections. Then, as
connections are established, handlers are registered for those too.
When you want to replace the process, you cancel the acceptor, so only
the existing connections keep the io_context alive.
At this point, the listener is closed and the port is free to use by
the new process. When all connections in the old process terminate, the
process then stops (because no more handlers are registered).
That's interesting, and simple. I guess I could extend one of my HTTP route,
or my existing WebSocket protocol handling, to have the new server communicate
with the old one, notifying him to drop the listener, and stop listening on the main
port, so that the io_context naturally can go out of scope, and the process terminate.
With possibly a grace period for existing clients to disconnect normally, before forceful termination.
There is a small timeframe during which requests might fail, because
the old process stops listening and the new process is not yet
accepting connections. Depending on your platform, there are ways
around this (e.g. SO_REUSEPORT on linux).
You mean because the main server port would not be released right away
when the older server stops the io_context's TCP acceptor? Preventing the
new server to start listening on that port right away? --DD