9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: adr <adr@SDF.ORG>
To: 9fans <9fans@9fans.net>
Subject: Re: [9fans] syscall silently kill processes
Date: Sun, 19 Jun 2022 05:54:52 +0000 (UTC)	[thread overview]
Message-ID: <f75f7af1-7d33-5422-17b-3cccc92ce20@SDF.ORG> (raw)
In-Reply-To: <55d376e1-fddb-135c-c7e3-ffca9ed621d7@posixcafe.org>

On Sat, 18 Jun 2022, Jacob Moody wrote:
> I've attempted to reproduce it, trying to remove the libthread/notify
> factors. I've come up with this:
>
> #include <u.h>
> #include <libc.h>
>
> static void
> proc_udp(void*)
> {
>        char resp[512];
>        char req[] = "request";
>        int fd;
>        int n;
>        int pid;
>
>        fd = dial("udp!185.157.221.201!5678", nil, nil, nil);
>        if(fd < 0)
>                exits("can't dial");
>
>        if(write(fd, req, strlen(req)) != strlen(req))
>                exits("can't write");
>
>        pid = getpid();
>        fprint(1, "start %d\n", pid);
>        n = read(fd, resp, sizeof(resp)-1);
>        fprint(1, "end %d %d\n", pid, n);
>        exits(nil);
> }
>
> void
> main(int, char**)
> {
>        int i;
>        Waitmsg *wm;
>
>        for(i = 0; i < 10; i++){
>                switch(fork()){
>                case -1:
>                        sysfatal("fork %r");
>                case 0:
>                        proc_udp(nil);
>                        sysfatal("ret");
>                default:
>                        break;
>                }
>        }
>        for(i = 0; i < 10; i++){
>                wm = wait();
>                print("proc %d died with message %s\n", wm->pid, wm->msg);
>        }
>        exits(nil);
> }
>
> This code makes it pretty obvious that we are losing some children;
> on my machine this program never exits. I see some portion of the
> readers correctly returning -1, and the parent is able to get their
> Waitmsg but not all of them.

Moody I think this old thread will interest you:

https://marc.info/?t=112730920400001&r=1&w=2

Russ Cox explained there:
  It appears that your program, at its core, it is doing this:

  void
  readproc(void *v)
  {
      int fd;
      char buf[100];
      fd = (int)v;
      read(fd, buf, sizeof buf);
  }

  void
  threadmain(int argc, char **argv)
  {
      int p[2];
      pipe(p);
      proccreate(readproc, (void*)p[0], 8192);
      proccreate(readproc, (void*)p[1], 8192);
      close(p[0]);
      /* and here you expect the first readproc to be done */
      close(p[1]);
      /* and here the second */
  }

  Each read call is holding up a reference to its channel
  inside the kernel, so that even though you've closed the fd
  and removed the ref from the fd table, there is still a reference
  to each side of the pipe in the form of the process blocked
  on the read.

  I've never been sure whether the implicit ref held during
  the system call is good behavior, but it's hard to change.

  In your case, writing 0 (or anything) makes the read
  finish, releasing the last ref to the underlying pipe when
  the system call finishes, and then everything cleans up
  as expected.  So you've found your workaround, and now
  we understand why it works.

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Tfa6823048ad90a21-M6e48031f9e8673387c0b47b8
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

  parent reply	other threads:[~2022-06-19  5:55 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-17  9:37 andrey100100100
2022-06-17 13:46 ` Thaddeus Woskowiak
2022-06-17 14:11   ` Jacob Moody
2022-06-17 14:39     ` Thaddeus Woskowiak
2022-06-17 15:06     ` andrey100100100
2022-06-17 16:08       ` Skip Tavakkolian
2022-06-17 16:11         ` Skip Tavakkolian
2022-06-17 16:16           ` Skip Tavakkolian
2022-06-17 17:42             ` adr
2022-06-17 16:11       ` Jacob Moody
2022-06-17 18:48         ` andrey100100100
2022-06-17 19:28           ` Jacob Moody
2022-06-17 21:15           ` adr
2022-06-18  6:40             ` andrey100100100
2022-06-18  8:37               ` adr
2022-06-18  9:22                 ` adr
2022-06-18 12:53                   ` Jacob Moody
2022-06-18 22:03                     ` andrey100100100
2022-06-19  5:54                     ` adr [this message]
2022-06-19  6:13                       ` Jacob Moody
2022-06-18 22:22                   ` andrey100100100
2022-06-18 16:57                 ` andrey100100100
2022-06-19  2:40                   ` adr
2022-06-19  5:01                     ` adr
2022-06-19  8:52                       ` andrey100100100
2022-06-19 10:32                         ` adr
2022-06-19 11:40                           ` andrey100100100
2022-06-19 12:01                             ` andrey100100100
2022-06-19 15:10                           ` andrey100100100
2022-06-19 16:41                             ` adr
2022-06-19 21:22                               ` andrey100100100
2022-06-19 21:26                                 ` andrey100100100
2022-06-20  4:41                                 ` adr
2022-06-20  5:39                                   ` andrey100100100
2022-06-20  5:59                                   ` adr
2022-06-20 15:56                                     ` andrey100100100
2022-06-20 22:29                                       ` Skip Tavakkolian
2022-06-21  7:07                                         ` andrey100100100
2022-06-21 11:26                                           ` adr
2022-06-21 13:03                                             ` andrey100100100
2022-06-21 13:22                                               ` adr
2022-06-28 15:28                                                 ` adr
2022-06-28 16:43                                                   ` ori
2022-06-28 18:19                                                   ` adr
2022-06-28 18:28                                                     ` adr
2022-06-28 19:09                                                   ` andrey100100100
2022-06-28 19:42                                                     ` adr
2022-06-29 13:14                                                       ` adr
2022-06-21 13:47                                             ` andrey100100100
2022-06-21  7:22                                         ` adr

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f75f7af1-7d33-5422-17b-3cccc92ce20@SDF.ORG \
    --to=adr@sdf.org \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).