Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python application using Twisted stops running after user logs off of Windows XP

I inherited a project using the Twisted Python library. The application is terminating after the user logs off of Windows XP.

The Python code has been converted to an executable using bbfreeze. In addition, the bbfreeze generated executable is registered as a Windows service using the instsrv.exe and srvany.exe.

I've taken a simple chat example from the Twisted website and create an executable from bbfreeze and registered it with instsrv and srvany and the same problem occurs: the executable stops running after the user logs off.

I'm inclined to think that something about Windows XP and the Twisted library's is causing the application to terminate or stop running. In particular, I think it might be something within the reactor code that's causing the application code to stop running. However, I haven't been able to confirm this.

Has anybody else seen this or have any ideas on what might be causing this?

Thanks, Mark

like image 795
Mark G Avatar asked Nov 05 '22 03:11

Mark G


1 Answers

  • "Logging Off" MSDN page says that on log off,
    • WM_QUERYENDSESSION is sent to every window [on the current desktop];
    • CTRL_LOGOFF_EVENT is sent to every process.
  • ejabberd service stops on user logoff/login suggests that a process can terminate if it has no handler for CTRL_LOGOFF_EVENT.

Judging by "I can also reproduce this with a simple chat sample" comment, Twisted is the culprit. Folks on the Internet do report that Twisted services fail in this manner sometimes: Re: SIGBREAK on windows.

Twisted has an internal logging facility. An example of using it is in the answer to Twisted starting/stopping factory/protocol less noisy log messages. Had you used it, you would already have seen the "received SIGBREAK..." message pointing to the root cause.


BTW, I use the code like below to log unhandled exceptions in my scripts. This is always a good idea if the script is to be ever run unattended (or by others that you diagnose problems for :^) ).

# set up logging #####################################
import sys,os,logging
logfile = os.path.splitext(os.path.basename(sys.argv[0]))[0]+".log"
logging.basicConfig(\
    format='%(asctime)s %(levelname)-8s %(message)s',\
    filename=logfile,\
    level=logging.DEBUG)
l = logging.getLogger()
#to avoid multiple copies after restart from pdb prompt
if len(l.handlers)<=1: l.addHandler(logging.StreamHandler(sys.stdout))
#hook to log unhandled exceptions
def excepthook(type,value,traceback):
    logging.exception("Unhandled exception occured",exc_info=(type,value,traceback))
    old_excepthook(type,value,traceback)
old_excepthook = sys.excepthook
sys.excepthook = excepthook
# ####################################################
like image 124
ivan_pozdeev Avatar answered Nov 07 '22 22:11

ivan_pozdeev