FastCGI cleanup code does not work under windows

Question!

Using apache on a windows server with mod_fastcgi, the C code looks like that:

void main() {
    init();
    while (FCGI_Accept() >= 0)
        work();
    cleanup();
}

When the service is taken down (i.e.: net stop apache2), the process terminates without getting to the cleanup code.

What am I missing here?



Answers

It seems, from reading the FCGI_Accept manpage and this FAQ entry that FCGI_Accept does not, in fact, return -1 in the case of Apache shutting down. Try setting a signal handler for SIGUSR1 and SIGTERM. There's an example (not Windows-specific, but it's worth a try) posted a while ago on a mailing list, here.



The only way to exit prematurely would be to call "exit()" somewhere inside work() (or FCGI_Accept()...)

Edit:

If you think it might be FCGI_Accept(), try using onexit() to set up a callback to be called from "exit()". At the very least this will confirm that "exit()" was called prematurely.

By : Jimmy J


You can't really "prevent" IDisposable from spreading. Some classes need to be disposed, like AutoResetEvent, and the most efficient way is to do it in the Dispose() method to avoid the overhead of finalizers. But this method must be called somehow, so exactly as in your example the classes that encapsulate or contain IDisposable have to dispose these, so they have to be disposable as well, etc. The only way to avoid it is to:

  • avoid using IDisposable classes where possible, lock or wait for events in single places, keep expensive resources in single place, etc
  • create them only when you need them and dispose them just after (the using pattern)

In some cases IDisposable can be ignored because it supports an optional case. For example, WaitHandle implements IDisposable to support a named Mutex. If a name is not being used, the Dispose method does nothing. MemoryStream is another example, it uses no system resources and its Dispose implementation also does nothing. Careful thinking about whether an unmanaged resource is being used or not can be instructional. So can examining the available sources for the .net libraries or using a decompiler.

By : Grzenio


This video can help you solving your question :)
By: admin