My PyLucene app freezes when run as a daemon

Boehm, Hans
Fri Mar 23 21:49:00 GMT 2007

It sounds like you're forking a gcj-compiled process, and then trying to
continue to run indefinitely in the child?

If your _fork() call eventually translates to a Linux (or other Posix)
fork() call, that fundamentally won't work.  Under Posix rules, only one
of the original threads survives in the child, and the child is thus
extremely restricted in what it can do:

From the SUSV3 fork man page: "Consequently, to avoid errors, the child
process may only execute async-signal-safe operations until such time as
one of the exec functions is called."

This applies only to multithreaded processes.  But Java (and gcj)
processes are always multithreaded.


> -----Original Message-----
> From: [] 
> On Behalf Of Ofer Nave
> Sent: Friday, March 23, 2007 2:24 PM
> To:
> Subject: My PyLucene app freezes when run as a daemon
> 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.
> Context:
> I have a PyLucene searcher application running under a 
> multithreaded python web server called Paste 
> (  I've added a command-line argument 
> to my script to run the script as a daemon based on the code 
> in this article: 
> 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 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

More information about the Java mailing list