<commit_before>import sys
import os
import time
import signal
import traceback
import logging
import cwbot.util.DebugThreading as threading
from cwbot.processArgv import processArgv
from cwbot.sys.BotSystem import BotSystem
from cwbot.common.exceptions import ManualException, ManualRestartException
from cwbot.kolextra.manager.ChatManager import ChatManager
from cwbot.kolextra.request.SendMessageRequest import SendMessageRequest
from cwbot.kolextra.manager.InventoryManager import InventoryManager
from cwbot.util.tryRequest import tryRequest
from kol.Session import Session
import kol.Error
from cwbot.sys.database import Database


databaseName = 'data/cwbot.db'

def openSession(props):
    """ Log in to the KoL servers. """
    log = logging.getLogger()
    s = Session()
    s.login(props.userName, props.password)
    log.info("Logged in.")
    onlineTime = time.time()
    return s


def createChatManager(s):
    """ Open a new chat manager. """
    log = logging.getLogger()
    log.debug("Opening chat...")    
    c = ChatManager(s)
    log.info("Logged in to chat.")
    return c

    
def createInventoryManager(s, db):
    """ Create a new inventory manager. """
    inv = InventoryManager(s, db)
    return inv


def loginLoop(myDb, props):
    """ Main part of the login loop. """
    log = logging.getLogger()
    s = None
    c = None
    onlineTime = time.time()
    successfulShutdown = False
    fastCrash = False
	cman = None
    try:
        loginWait = 60
        s = openSession(props)
        inv = createInventoryManager(s, myDb)
        cman = createChatManager(s)
        bsys = BotSystem(s, cman, props, inv, 'modules.ini', myDb)
        
        # run the bot main loop. If this function returns, then we are logging
        # out for rollover. If it throws an exception, then we are either
        # shutting down manually or crashing.
        bsys.loop()

        loginWait = props.rolloverWait
        successfulShutdown = True
        log.info("Preparing for rollover...")
    except (SystemExit, KeyboardInterrupt):
        # manual quit
        log.info("Exiting application.")
        loginWait = -1
        successfulShutdown = True
    except kol.Error.Error as inst:
        # standard error with a pyKol component
        if hasattr(inst, 'timeToWait'):
            loginWait = inst.timeToWait
        if props.debug:
            log.exception("Exception raised.".format(loginWait))
            raise
        if inst.code == kol.Error.NIGHTLY_MAINTENANCE and s is None:
            log.info("Nightly maintenance; waiting to try again...")
            loginWait = 60
            successfulShutdown = True
        else:
            log.exception("Exception raised; waiting {} seconds."
                          .format(loginWait))
    except ManualException as inst:
        # die for some amount of time
        if hasattr(inst, 'timeToWait'):
            loginWait = inst.timeToWait
        if props.debug:
            log.exception("Exception raised.".format(loginWait))
            raise
        log.exception("Exception raised; waiting {} seconds."
                      .format(loginWait))
        successfulShutdown = True
    except ManualRestartException:
        # restart the process
        loginWait = -2
        successfulShutdown = True
        log.info("Restarting process...")
    except:
        log.exception("Unknown error.")
        
        # kmail the administrators
        etype, value, tb = sys.exc_info()
        if s is not None and s.isConnected and c is not None:
            for uid in props.getAdmins('crash_notify'):
                errorText = ''.join(traceback.format_exception(
                                                        etype, value, tb))
                if len(errorText) > 1800:
                    errorText = "..." + errorText[-1800:]
                                                        
                alert1 = SendMessageRequest(
                        s, {'userId': uid, 
                            'text': "NOTICE: CWbot encountered an "
                                    "unknown error: {}".format(errorText)})
                try:
                    tryRequest(alert1)
                except Exception:
                    log.exception("Exception sending error notice.")
        del tb
        if props.debug:
            raise
    finally:
        # shut down
        sys.last_traceback = None
        log.debug("Shutting down...")
        if (not successfulShutdown and time.time() - onlineTime < 300):
            # if shutdown was too fast: do an exponential back-off
            fastCrash = True
            if s is not None:
                log.warning("Immediate crash detected. "
                            "Backing off before next startup.")
                if not props.debug:
                    try:
                        cman.sendChatMessage("I seem to have crashed quite"
                                             " quickly. I'll try again "
                                             "in a little while.",
                                             useEmoteFormat=True)
                    except:
                        pass
            if cman is not None:
                try:
                    log.debug("Closing chat...")
                    cman.close()
                except:
                    log.exception("Error closing chat session.")
        if s is not None:
            try:
                log.debug("Closing session...")
                s.logout()
            except:
                log.exception("Error closing KoL session.")
            s = None
        log.info("----- Logged out. -----\n")
    return (loginWait, fastCrash)

    <commit_msg>chat is properly closed<commit_after>import sys
import os
import time
import signal
import traceback
import logging
import cwbot.util.DebugThreading as threading
from cwbot.processArgv import processArgv
from cwbot.sys.BotSystem import BotSystem
from cwbot.common.exceptions import ManualException, ManualRestartException
from cwbot.kolextra.manager.ChatManager import ChatManager
from cwbot.kolextra.request.SendMessageRequest import SendMessageRequest
from cwbot.kolextra.manager.InventoryManager import InventoryManager
from cwbot.util.tryRequest import tryRequest
from kol.Session import Session
import kol.Error
from cwbot.sys.database import Database


databaseName = 'data/cwbot.db'

def openSession(props):
    """ Log in to the KoL servers. """
    log = logging.getLogger()
    s = Session()
    s.login(props.userName, props.password)
    log.info("Logged in.")
    onlineTime = time.time()
    return s


def createChatManager(s):
    """ Open a new chat manager. """
    log = logging.getLogger()
    log.debug("Opening chat...")    
    c = ChatManager(s)
    log.info("Logged in to chat.")
    return c

    
def createInventoryManager(s, db):
    """ Create a new inventory manager. """
    inv = InventoryManager(s, db)
    return inv


def loginLoop(myDb, props):
    """ Main part of the login loop. """
    log = logging.getLogger()
    s = None
    c = None
    onlineTime = time.time()
    successfulShutdown = False
    fastCrash = False
    cman = None
    try:
        loginWait = 60
        s = openSession(props)
        inv = createInventoryManager(s, myDb)
        cman = createChatManager(s)
        bsys = BotSystem(s, cman, props, inv, 'modules.ini', myDb)
        
        # run the bot main loop. If this function returns, then we are logging
        # out for rollover. If it throws an exception, then we are either
        # shutting down manually or crashing.
        bsys.loop()

        loginWait = props.rolloverWait
        successfulShutdown = True
        log.info("Preparing for rollover...")
    except (SystemExit, KeyboardInterrupt):
        # manual quit
        log.info("Exiting application.")
        loginWait = -1
        successfulShutdown = True
    except kol.Error.Error as inst:
        # standard error with a pyKol component
        if hasattr(inst, 'timeToWait'):
            loginWait = inst.timeToWait
        if props.debug:
            log.exception("Exception raised.".format(loginWait))
            raise
        if inst.code == kol.Error.NIGHTLY_MAINTENANCE and s is None:
            log.info("Nightly maintenance; waiting to try again...")
            loginWait = 60
            successfulShutdown = True
        else:
            log.exception("Exception raised; waiting {} seconds."
                          .format(loginWait))
    except ManualException as inst:
        # die for some amount of time
        if hasattr(inst, 'timeToWait'):
            loginWait = inst.timeToWait
        if props.debug:
            log.exception("Exception raised.".format(loginWait))
            raise
        log.exception("Exception raised; waiting {} seconds."
                      .format(loginWait))
        successfulShutdown = True
    except ManualRestartException:
        # restart the process
        loginWait = -2
        successfulShutdown = True
        log.info("Restarting process...")
    except:
        log.exception("Unknown error.")
        
        # kmail the administrators
        etype, value, tb = sys.exc_info()
        if s is not None and s.isConnected and c is not None:
            for uid in props.getAdmins('crash_notify'):
                errorText = ''.join(traceback.format_exception(
                                                        etype, value, tb))
                if len(errorText) > 1800:
                    errorText = "..." + errorText[-1800:]
                                                        
                alert1 = SendMessageRequest(
                        s, {'userId': uid, 
                            'text': "NOTICE: CWbot encountered an "
                                    "unknown error: {}".format(errorText)})
                try:
                    tryRequest(alert1)
                except Exception:
                    log.exception("Exception sending error notice.")
        del tb
        if props.debug:
            raise
    finally:
        # shut down
        sys.last_traceback = None
        log.debug("Shutting down...")
        if (not successfulShutdown and time.time() - onlineTime < 300):
            # if shutdown was too fast: do an exponential back-off
            fastCrash = True
            if s is not None:
                log.warning("Immediate crash detected. "
                            "Backing off before next startup.")
                if not props.debug:
                    try:
                        cman.sendChatMessage("I seem to have crashed quite"
                                             " quickly. I'll try again "
                                             "in a little while.",
                                             useEmoteFormat=True)
                    except:
                        pass
		if cman is not None:
			try:
				log.info("Closing chat...")
				cman.close()
				cman = None
			except:
				log.exception("Error closing chat session.")
        if s is not None:
            try:
                log.info("Closing session...")
                s.logout()
            except:
                log.exception("Error closing KoL session.")
            s = None
        log.info("----- Logged out. -----\n")
    return (loginWait, fastCrash)

    
{'fraction_non_alphanumeric': 0.08634803274359651, 'fraction_numerical': 0.0033447759880292226, 'mean_word_length': 1.6349721706864564, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6995186', 'n_tokens_mistral': 2972, 'n_tokens_neox': 2843, 'n_words': 879}