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

[perl #130143] t/op/getppid.t fails under docker/travis

From:
slaven@rezic.de via RT
Date:
March 25, 2017 20:25
Subject:
[perl #130143] t/op/getppid.t fails under docker/travis
Message ID:
rt-4.0.24-8143-1490473544-1578.130143-15-0@perl.org
Dana Mon, 21 Nov 2016 17:10:30 -0800, kentfredric reče:
> On Mon, 21 Nov 2016 16:24:20 -0800, tonyc wrote:
> > If you use something like:
> > 
> > https://github.com/phusion/baseimage-docker/blob/rel-
> > 0.9.16/image/bin/my_init
> > 
> > as the init process for your container, do the tests pass?
> > 
> 
> Not able to comment on that as I don't believe I can usurp
> the init implementation under travis, and I don't presently have
> a plain docker configuration outside those provided by travis.
> 

The problem is that the travis build/test script does not run at all below the init process. The process tree in a travis-ci container while running the failing getppid.t test looks roughly like this:

   PID   PGID    SID TTY          TIME CMD
  1560     24     24 ?        00:00:00 perl
  1563     24     24 ?        00:00:00   sh
  1564     24     24 ?        00:00:00     ps
    24     24     24 ?        00:00:00 bash
  1465     24     24 ?        00:00:00   make
  1545     24     24 ?        00:00:00     sh
  1546     24     24 ?        00:00:00       perl
  1555     24     24 ?        00:00:00         perl <defunct>
  1556     24     24 ?        00:00:00         perl
  1557     24     24 ?        00:00:00         perl
  1558     24     24 ?        00:00:00         perl
     1      1      1 ?        00:00:00 init
    48     48     48 ?        00:00:00   mysqld
    50     50     50 ?        00:00:00   cron
    62     62     62 ?        00:00:00   sshd
   162    162    162 ?        00:00:00   rsyslogd
  1045   1039   1039 ?        00:00:00   postgres
  1051   1051   1051 ?        00:00:00     postgres
  1052   1052   1052 ?        00:00:00     postgres
  1053   1053   1053 ?        00:00:00     postgres
  1054   1054   1054 ?        00:00:00     postgres
  1055   1055   1055 ?        00:00:00     postgres
  1101   1099   1099 ?        00:00:00   memcached

Unlike in a traditional Unix system, there's not only one process tree, but three: a traditional init process with some standard daemons, the travis-ci build/test script (with bash as the root process), and the forked orphaned perl process, which is calling getppid() (recognizable by the additional "ps" process which I added to the getppid.t test).

The problem seems to be that orphaned processes only started in the init tree would be adopted by the init process. In the other tree this is not done by the container's init process, but by a process outside the linux container. Maybe we can call this an "incognito adoption".

Now what's getppid() returning in this case --- the parent process is outside the container? http://man7.org/linux/man-pages/man7/pid_namespaces.7.html says: "Calls to getppid(2) for such processes return 0."

Consequence is that in such an environment the test should even allow 0 as the new parent:

	cmp_ok ($second, '>=', 0, "New parent of orphaned $which grandchild")

Maybe accepting 0 should be done only on linux systems, and only if a container was detected. A possible check (adapted from http://stackoverflow.com/a/20012536/2332415) could look like this:

     sub is_container {
         my $container = 0;
         if (open my $fh, '<', "/proc/1/cgroup") {
             while(<$fh>) {
                 if (m{^\d+:pids:(.*)} && $1 ne '/init.scope') {
                     $container = 1;
                     last;
                 }
             }
         }
         $container;
     }

For non-container systems the check could still be $second >= 1.

BTW, to reproduce the issue in a docker system just make getppid.t use Test::More, copy it to the docker container and run

    docker exec -it name_of_container perl getppid.t

In contrast, running the docker container normally with "docker run -it ... bash" and "perl getppid.t" inside would give a successful test run.



---
via perlbug:  queue: perl5 status: open
https://rt.perl.org/Ticket/Display.html?id=130143



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