On Dec 16, 2015, at 9:25 AM, Chris Norman <chris.norman2@googlemail.com> wrote:

Hi all,
I'm writing a MUD server, and I want a way for transports to be notified ofa shutdown before being disconnected, and the reactor being stopped.

I've tried:

for t in transports:
t.write('Shutting down.\r\n')
t.loseConnection()
reactor.stop()

This doesn't seem to notify the transports.

I also tried:
for t in transports:
t.write('Shutting down.\r\n')
t.loseConnection()
while t.connected:
 pass
reactor.stop()

That just blocked and did nothing, presumably something do with my while loop.

Is there a stopWhenEmpty function on the somewhere? I did look over the methods, and I couldn't find anything promising.

I'm just using the standard from twisted.internet import reactor reactor, so no special cases here. In case it matters the transports I'm using are twisted.protocols.basic.LineReceiver, and everything else works with them.

Cheers in advance for the help.

This is definitely doable, but before I explain it would help to know why you want to do this.

The reason I ask is: servers crash; hardware fails.  The falcon cannot hear the falconer; things fall apart; the centre cannot hold.

When those servers do crash (and they will), you don't get a clean notification of disconnects.  So if you're writing your application to rely very heavily on the ability to do a clean shutdown and get notifications of every disconnect at the time you expect reactor.stop() to be running, you are probably designing your system in a way that will be very fragile and prone to data loss.  I know, I have made this mistake more than once myself :).

So, before you continue: do you actually need to do this?  Could you just ignore the notification of the connection drop and exit gracefully, perhaps properly cleaning up whatever state is left over at next startup?  If you really need this, understanding why you need it would also help in determining which implementation technique to suggest (there are a few).

-glyph