develooper Front page | perl.perl5.porters | Postings from March 2016

[perl #127794] Strange behavior when forking in BEGIN

Thread Next
From:
Chad Granum
Date:
March 28, 2016 06:10
Subject:
[perl #127794] Strange behavior when forking in BEGIN
Message ID:
rt-4.0.18-12302-1459145406-586.127794-75-0@perl.org
# New Ticket Created by  Chad Granum 
# Please include the string:  [perl #127794]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/Ticket/Display.html?id=127794 >


This is a bug report for perl from exodist7@gmail.com,
generated with the help of perlbug 1.40 running under perl 5.20.3.


-----------------------------------------------------------------
[Please describe your issue here]

This script, should output "about to fork" and "Hi" 10 times. Instead it
prints "about to fork" 10 times, but "Hi" only once.

#!/usr/bin/env perl

    BEGIN {
        my $start = $$;
        for ( 1 .. 10 ) {
            my $pid = fork;
            if ($pid) {
                print "About to fork\n";
                waitpid($pid, 0);
            }
            else {
                last;
            }
        }
        exit 0 if $$ == $start;
    }

    print "Hi\n";

Here is where it gets even MORE interesting, add these 3 lines to the end
of the script and it prints "Hi" twice:

    __END__

    print "Hi\n";

But wait, theres more! Add those 3 lines again, so it looks like this:

    ...
    print "Hi\n";

    __END__

    print "Hi\n";

    __END__

    print "Hi\n";

And bam, it prints "Hi\n" all 10 times.

This looks like a filehandle fork bug to me, but I don't really know much
about these things.

[Please do not change anything below this line]
-----------------------------------------------------------------
---
Flags:
    category=core
    severity=medium
---
Site configuration information for perl 5.20.3:

Configured by exodist at Sat Mar  5 16:04:14 PST 2016.

Summary of my perl5 (revision 5 version 20 subversion 3) configuration:

  Platform:
    osname=linux, osvers=4.4.3-1-arch, archname=x86_64-linux-thread-multi
    uname='linux abydos 4.4.3-1-arch #1 smp preempt fri feb 26 15:09:29 cet
2016 x86_64 gnulinux '
    config_args='-de -Dprefix=/home/exodist/perl5/perlbrew/perls/main
-Dusethreads -Aeval:scriptdir=/home/exodist/perl5/perlbrew/perls/main/bin'
    hint=recommended, useposix=true, d_sigaction=define
    useithreads=define, usemultiplicity=define
    use64bitint=define, use64bitall=define, uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -fwrapv
-fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include
-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-O2',
    cppflags='-D_REENTRANT -D_GNU_SOURCE -fwrapv -fno-strict-aliasing -pipe
-fstack-protector -I/usr/local/include'
    ccversion='', gccversion='5.3.0', gccosandvers=''
    intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
    ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t',
lseeksize=8
    alignbytes=8, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -fstack-protector -L/usr/local/lib'
    libpth=/usr/local/lib
/usr/lib/gcc/x86_64-unknown-linux-gnu/5.3.0/include-fixed /usr/lib
/lib/../lib /usr/lib/../lib /lib /lib64 /usr/lib64
    libs=-lpthread -lnsl -lnm -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc
-lgdbm_compat
    perllibs=-lpthread -lnsl -lnm -ldl -lm -lcrypt -lutil -lc
    libc=libc-2.23.so, so=so, useshrplib=false, libperl=libperl.a
    gnulibc_version='2.23'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
    cccdlflags='-fPIC', lddlflags='-shared -O2 -L/usr/local/lib
-fstack-protector'


---
@INC for perl 5.20.3:
    /home/exodist/.perlbrew/libs/main@exodist
/lib/perl5/x86_64-linux-thread-multi
    /home/exodist/.perlbrew/libs/main@exodist/lib/perl5

/home/exodist/perl5/perlbrew/perls/main/lib/site_perl/5.20.3/x86_64-linux-thread-multi
    /home/exodist/perl5/perlbrew/perls/main/lib/site_perl/5.20.3

/home/exodist/perl5/perlbrew/perls/main/lib/5.20.3/x86_64-linux-thread-multi
    /home/exodist/perl5/perlbrew/perls/main/lib/5.20.3
    .

---
Environment for perl 5.20.3:
    HOME=/home/exodist
    LANG=en_US.UTF-8
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/home/exodist/.perlbrew/libs/main@exodist
/bin:/home/exodist/perl5/perlbrew/bin:/home/exodist/perl5/perlbrew/perls/main/bin:/home/exodist/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
    PERL5LIB=/home/exodist/.perlbrew/libs/main@exodist/lib/perl5
    PERLBREW_BASHRC_VERSION=0.73
    PERLBREW_HOME=/home/exodist/.perlbrew
    PERLBREW_LIB=exodist
    PERLBREW_MANPATH=/home/exodist/.perlbrew/libs/main@exodist
/man:/home/exodist/perl5/perlbrew/perls/main/man
    PERLBREW_PATH=/home/exodist/.perlbrew/libs/main@exodist
/bin:/home/exodist/perl5/perlbrew/bin:/home/exodist/perl5/perlbrew/perls/main/bin
    PERLBREW_PERL=main
    PERLBREW_ROOT=/home/exodist/perl5/perlbrew
    PERLBREW_VERSION=0.74
    PERL_BADLANG (unset)
    PERL_LOCAL_LIB_ROOT=/home/exodist/.perlbrew/libs/main@exodist
    PERL_MB_OPT=--install_base /home/exodist/.perlbrew/libs/main@exodist
    PERL_MM_OPT=INSTALL_BASE=/home/exodist/.perlbrew/libs/main@exodist
    SHELL=/usr/bin/zsh


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About