My PyLucene app freezes when run as a daemon

David Daney ddaney@avtrex.com
Fri Mar 23 21:54:00 GMT 2007


Ofer Nave wrote:
> This might be a bit off the usual topic range here, but I was pointed to
> this list.
> 
> Background:
> 
> PyLucene is a GCJ-compiled version of Java Lucene integrated with Python.
> http://pylucene.osafoundation.org/
> 
> Context:
> 
> I have a PyLucene searcher application running under a multithreaded python
> web server called Paste (http://pythonpaste.org).  I've added a command-line
> argument to my script to run the script as a daemon based on the code in
> this article: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731
> 
> Problem:
> 
> When the script is run as a normal process, it works fine.  When it is run
> as a daemon, it freezes after about a dozen requests.  The point it's
> freezing at is in a call to a class that is known to not be thread-safe, so
> that might be the problem - but on the other hand, the only difference
> between the good and bad scenarios is whether it's running as a daemon, so I
> tend to lean towards that as my top clue.
> 
> How I Got Here:
> 
> I posted this to the PyLucene list, and was told: "You might want to ask
> java@gcc.gnu.org about daemonizing a process using libgcj and boehm-gc."  So
> I'm giving it a shot.  Does anyone have wisdom on the care and feeding of
> daemonized processes built with GCJ?  Is there anything special I need to
> know about?
> 
> Appendix 1 - My daemonize() Function:
> 
> # call to detach yourself from a terminal
> def daemonize(logfile=None):
>     # fork twice to sever any ties
>     _fork()
>     os.setsid()
>     _fork()
>     os.chdir(WORKDIR)
>     os.umask(UMASK)
> 
>     # close standard I/O channels
>     os.close(0)  # stdin
>     os.close(1)  # stdout
>     os.close(2)  # stderr
> 
>     # redirect standard output channels to logfile
>     if logfile:
>         sys.stdout = sys.stderr = logfile
> 
>     # set up SIGTERM handler for cleanup
>     signal(SIGTERM, _sigterm_handler)
> 
> -ofer
> 

It would not surprise me if that caused problems.  Does it work with 
Sun's runtime?

If you want to daemonize, I would recommend doing it in C code before 
calling JvRunMain.  I am also unsure what happens if you close stdout 
and stderr.  I would hope that you don't do anything that would cause 
libgcj to try to write to those descriptors.  It might help to open 
stdout and stderr to /dev/null

Other that that I would recommend attaching gdb to it to see where it is 
stuck.

David Daney



More information about the Java mailing list