from twisted.internet.task import LoopingCall
from twisted.python import log as twistedLog

from src.shared import config
from src.server.game_state_manager import GameStateManager
from src.server.networking import runServer, ConnectionManager
from src.server.stdio import setupStdio

def main(args):
    connections = ConnectionManager()
    gameStateManager = GameStateManager(connections)
    connections.setGameStateHandler(gameStateManager)
    setupStdio(gameStateManager)

    loop = LoopingCall(gameStateManager.tick)
    deferred = loop.start(config.TICK_LENGTH)
    deferred.addErrback(twistedLog.err)

    runServer(args.port, connections)


from twisted.internet import reactor
from twisted.internet.task import LoopingCall
from twisted.python import log as twistedLog

from src.shared import config
from src.server.game_state_manager import GameStateManager
from src.server.networking import runServer, ConnectionManager
from src.server.stdio import setupStdio

def unhandledError(reason):
    twistedLog.err(reason, "Aborting due to unhandled error.")
    reactor.stop()

def main(args):
    connections = ConnectionManager()
    gameStateManager = GameStateManager(connections)
    connections.setGameStateHandler(gameStateManager)
    setupStdio(gameStateManager)

    loop = LoopingCall(gameStateManager.tick)
    deferred = loop.start(config.TICK_LENGTH)
    deferred.addErrback(unhandledError)

    runServer(args.port, connections)


Bring down the server on (some?) uncaught errors.
Bring down the server on (some?) uncaught errors.

I added an errback to the LoopingCall for gameStateManager.tick, so
it'll be called if any exception gets raised out of one of those calls.
The errback just prints a traceback and then brings down the server,
ensuring that other clients get disconnected as well.

This is at least some progress on #31, though it's hard to know if the
issue is really fully fixed.

+ from twisted.internet import reactor
  from twisted.internet.task import LoopingCall
  from twisted.python import log as twistedLog
  
  from src.shared import config
  from src.server.game_state_manager import GameStateManager
  from src.server.networking import runServer, ConnectionManager
  from src.server.stdio import setupStdio
+ 
+ def unhandledError(reason):
+     twistedLog.err(reason, "Aborting due to unhandled error.")
+     reactor.stop()
  
  def main(args):
      connections = ConnectionManager()
      gameStateManager = GameStateManager(connections)
      connections.setGameStateHandler(gameStateManager)
      setupStdio(gameStateManager)
  
      loop = LoopingCall(gameStateManager.tick)
      deferred = loop.start(config.TICK_LENGTH)
-     deferred.addErrback(twistedLog.err)
+     deferred.addErrback(unhandledError)
  
      runServer(args.port, connections)
  
  
Bring down the server on (some?) uncaught errors.
## Code Before:
from twisted.internet.task import LoopingCall
from twisted.python import log as twistedLog

from src.shared import config
from src.server.game_state_manager import GameStateManager
from src.server.networking import runServer, ConnectionManager
from src.server.stdio import setupStdio

def main(args):
    connections = ConnectionManager()
    gameStateManager = GameStateManager(connections)
    connections.setGameStateHandler(gameStateManager)
    setupStdio(gameStateManager)

    loop = LoopingCall(gameStateManager.tick)
    deferred = loop.start(config.TICK_LENGTH)
    deferred.addErrback(twistedLog.err)

    runServer(args.port, connections)


## Instruction:
Bring down the server on (some?) uncaught errors.
## Code After:
from twisted.internet import reactor
from twisted.internet.task import LoopingCall
from twisted.python import log as twistedLog

from src.shared import config
from src.server.game_state_manager import GameStateManager
from src.server.networking import runServer, ConnectionManager
from src.server.stdio import setupStdio

def unhandledError(reason):
    twistedLog.err(reason, "Aborting due to unhandled error.")
    reactor.stop()

def main(args):
    connections = ConnectionManager()
    gameStateManager = GameStateManager(connections)
    connections.setGameStateHandler(gameStateManager)
    setupStdio(gameStateManager)

    loop = LoopingCall(gameStateManager.tick)
    deferred = loop.start(config.TICK_LENGTH)
    deferred.addErrback(unhandledError)

    runServer(args.port, connections)

