Thread suddenly stops in NT Service

Geoffrey Talvola gtalvola at nameconnector.com
Tue Jul 15 17:28:40 EDT 2003


matthew.rapoport at accenture.com wrote:
> I'm running an NT service with a thread.  The thread performs a
> calculation every so often and appends the number to a text file.
> Every so often the thread just stops - or so it seems.  The text file
> stops getting numbers added to it but the service is still running and
> no errors have been thrown.  I'm quite sure that the code would catch
> an error and post it to the event viewer if one were arising.
> 
> The only other relevant points I can think of are that the thread runs
> a while 1: loop and is set to run as a Daemon.  The service waits only
> for the stop command.  It doesn't seem to be operating system specific
> (happens on both XP and 2000).  It doesn't seem to be file size
> specific, it stops at various file sizes ranging from 5K - 50K.  It
> doesn't seem to be time specific - sometimes it'll stop after about a
> day.  Sometimes it'll stop after two days.
> 
> Any help would be great.  Thx.

Make sure that you're not intentionally or unintentionally writing to
sys.stdout or sys.stderr in your service -- those are not valid file
objects.  That will definitely cause a traceback, which could crash your
thread, but not until you've written enough for it to try to flush its
internal buffers, which could be the reason why your service is able to run
for a while before it stops.  In my service code I usually avoid the problem
by adding something like:

        sys.stdout = sys.stderr = open('nul', 'w')

in the __init__ of my service class, perhaps with an option to write to a
logfile instead of just throwing away the output.  Too bad the service
framework doesn't do this automatically...

- Geoff





More information about the Python-list mailing list