mhammond / pywin32

Python for Windows (pywin32) Extensions
4.91k stars 783 forks source link

atexit not called for pythonservice (win32) #239

Open ghost opened 18 years ago

ghost commented 18 years ago

I have a couple of functions registered with atexit which work fine when the program is executed on command-line. But when I run the same as a windows service, and stop the service using service interface, these hooks never get call backs. The service does exit normally (at least my program doesn't do os._exit() and there are no exceptions).

I am using ActiveState's distribution of python, but I would imagine this is non-specific. Here is the version information:

ActivePython 2.4.1 Build 247 (ActiveState Corp.) based on Python 2.4.1 (#65, Jun 20 2005, 17:01:55) [MSC v.1310 32 bit (Intel)] on win32

As a workaround I am calling the atexit._run_exitfuncs() manually before returning from SvcDoRun() for now.

Reported by: haridsv

Original Ticket: "pywin32/bugs/239":https://sourceforge.net/p/pywin32/bugs/239

ghost commented 18 years ago

Logged In: YES user_id=414645

I wonder if this bug is related to the following issue? http://groups.google.com/group/comp.lang.python/browse\_thread/thread/fe9e426650764ed9/972ecadc2d6fcf5b\#972ecadc2d6fcf5b

Original comment by: woodsplitter

ghost commented 18 years ago

Logged In: YES user_id=344432

Doesn't seem to be. In my case I don't see a call back at all, so the issue is not about when it gets called.

Original comment by: haridsv

ghost commented 16 years ago

Logged In: YES user_id=9565 Originator: NO

This issue is still present. I have a minimal example which reproduces it which I will attach to this bug shortly.

Note - regarding "woodsplitter"'s comment - I don't think this is related to the issue linked to there. As far as I can tell, the issue there is related to atexit being called in an order different to that expected, due to threading issues. In my minimal example, I have no threads involved; all my example code runs in SvcDoRun, in a single thread.

I believe the problem is simply that, in src/PythonService.cpp, Py_Finalize() is never called. For example, in the main() function, Py_Initialize() is called near the start, but a corresponding Py_Finalize() is not called.

Original comment by: richardb

ghost commented 16 years ago

Logged In: YES user_id=9565 Originator: NO

Well, I can't see how to attach files to this report, though there is a line listing the number of attachments (listing it as 0, though).

So, the example is at http://tartarus.org/~richard/test\_service.py If I register and run the service (with "python test_service.py start" on the command line), I get two log entries - one saying "Started" and one saying "Stopping", 5 seconds later.

If I uncomment the "sys.exitfunc()" line, I get an extra message "Atexit called", immediately after the "Stopping" message.

I would expect to see the "Atexit called" message even with the call to "sys.exitfunc()" commented out.

I don't have time to add Py_Finalize to PythonService.cpp and recompile, but I would imagine that that would cause the sys.exitfunc() to be called correctly - Py_Finalize is usually responsible for calling sys.exitfunc() to be called on interpreter shutdown.

Original comment by: richardb

ghost commented 16 years ago

Original comment by: mhammond

ghost commented 16 years ago

Logged In: YES user_id=14198 Originator: NO

Just a ping - the fix here isn't obvious given we may have multiple services in an executable. I'm confident there are reasonable work-arounds, so punting again (at least until we do host multiple services in 1 .exe)

Original comment by: mhammond