AlfaProject has asked for the wisdom of the Perl Monks concerning the following question:

Hi
I have a issue with fork function ,
i have some code which registers users and sends mail to them.
register_user(@some_params_here); my $pid = fork(); if( $pid == 0 ){ send_mail($mail_params); exit 0; } write_log(@some_params_here);

That peace of code always worked well , till I get a message from one user that he receives an error:
ModPerl::Util::exit: (120000) exit was called
That happens in line of 'exit 0;' in code.
I don't understand why he gets output from child, and not keep working with the parent.
Thanks for help :)

Replies are listed 'Best First'.
Re: fork issue
by moritz (Cardinal) on Jan 08, 2012 at 09:49 UTC

    It seems you're working in a mod_perl environment. There exit is replaced by Modperl::Util::exit, which in turns throws an exception, which the modperl controller is supposed to catch, and terminate your request.

    If you have an exception handler somewhere in your code, maybe it catches this exception from the emulated exit?

      From what i know , it always shows me that error when the 'exit' functions is called.
      Maybe because of CGI::Carp(fatalsToBrowser);
      I don't want it interact with the user's browser , but only send email
      He should keep working with the parent ,
      How I do that ?
      Thanks
Re: fork issue
by JavaFan (Canon) on Jan 08, 2012 at 15:26 UTC
    It probably doesn't have to do anything with your observed problem, but do note that you are not checking whether the fork fails. If it fails, $pid is undefined, hence $pid == 0 is true and the *parent* (is you can talk of a parent if there is no child) sends mails and exits; the log will not be written.
Re: fork issue
by Khen1950fx (Canon) on Jan 08, 2012 at 13:54 UTC
    I'd do it differently. Try this:
    #!/usr/bin/perl -l use strict; use warnings; use ModPerl::Util; use ModPerl::Const -compile => 'EXIT'; register_user(); my $pid = fork(); if( $pid == 0 ){ send_mail(); eval { exit; }; exit if $@ && ref $@ eq 'APR::Error' && $@ == ModPerl::EXIT; print "Still running"; } write_log(); sub register_user { print "Registering user." } sub send_mail { print "Mail sent..."; }; sub write_log { print "Written to log..."; };