Fixes for shutdown sequence, including prevention of a race condition

* Throw an exception if any events, runnables or messages are posted after shutdown. These were previosuly being silently ignored. We really should know about them.
* Don't exit the connection manager thread until the object manager thread exits. This fixes a race condition where the wakeup time of the conmgr thread would determine if it shutdown before or after the object manager, meaning it would sometimes ignore messages around shutdown time.
* Add a shutdown constraint to make the client manager shutdown before the invoker/event/conmgr threads. This gives a single point in the shutdown sequence that applications can use as a constraint (TODO: shutdown groups?)
* Emit a log message for each shutdown method call. This could be very useful in production.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5491 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Jamie Doornbos
2008-11-03 22:20:16 +00:00
parent 0cb3330de7
commit 82d4f36c1a
4 changed files with 43 additions and 5 deletions
@@ -255,6 +255,13 @@ public class ConnectionManager extends LoopingThread
report.append(avgOut).append(" avg size, ");
report.append(bytesOut*1000/sinceLast).append(" bps\n");
}
@Override // from LoopingThread
public boolean isRunning ()
{
// Prevent exiting our thread until the object manager is done.
return super.isRunning() || _omgr.isRunning();
}
/**
* Notifies the connection observers of a connection event. Used internally.
@@ -722,6 +729,9 @@ public class ConnectionManager extends LoopingThread
sendOutgoingMessages(System.currentTimeMillis());
// unbind our listening socket
// Note: because we wait for the object manager to exit before we do, we will still be
// accepting connections as long as there are events pending.
// TODO: consider shutting down the listen socker earlier, like in the shutdown method
try {
_ssocket.socket().close();
} catch (IOException ioe) {
@@ -846,6 +856,10 @@ public class ConnectionManager extends LoopingThread
*/
void postMessage (Connection conn, DownstreamMessage msg)
{
if (!isRunning()) {
throw new IllegalStateException("Connection manager has been shutdown");
}
// sanity check
if (conn == null || msg == null) {
log.warning("postMessage() bogosity", "conn", conn, "msg", msg, new Exception());