[Bug 1966100] [NEW] getaddrinfo not fork-safe

Josh Reed 1966100 at bugs.launchpad.net
Wed Mar 23 16:07:03 UTC 2022


Public bug reported:

When compiling the following code and executing the program, the child
process hangs forever on a mutex that was held by `getaddrinfo` on
another thread at the time of the fork. I don't see anything in the
manpage for `getaddrinfo` that suggests this shouldn't work. I suspect
glibc is missing a call to `pthread_atfork`.

mre.cpp
```
#include <thread>

#include <netdb.h>
#include <unistd.h>

void spamGetaddrinfo()
{
    for (int i = 0; i< 100; ++i)
    {
        addrinfo *addrInfo{nullptr};
        ::getaddrinfo("google.com", "80", nullptr, &addrInfo);
    }
    printf("Thread is done\n");
}

int main()
{
    auto thread = std::thread{spamGetaddrinfo};

    usleep(1);

    if (auto pid = fork(); pid != 0)
    {
        // Parent
        printf("Parent is done\n");
        thread.join();
    }
    else
    {
        // Child
        addrinfo *addrInfo{nullptr};
        ::getaddrinfo("google.com", "80", nullptr, &addrInfo);

        printf("Child is done\n");
    }
}
```

Run `g++ -std=c++17 -pthread mre.cpp && ./a.out`

Expected output:
```
Parent is done
Child is done
Thread is done
```

Actual output:
```
Parent is done
Thread is done
```

When finished, use `pkill a.out` to remove the leftover child process.

ProblemType: Bug
DistroRelease: Ubuntu 18.04
Package: libc6 2.27-3ubuntu1.5
ProcVersionSignature: Ubuntu 4.15.0-166.174-generic 4.15.18
Uname: Linux 4.15.0-166-generic x86_64
ApportVersion: 2.20.9-0ubuntu7.27
Architecture: amd64
Date: Wed Mar 23 10:32:43 2022
Dependencies:
 gcc-8-base 8.4.0-1ubuntu1~18.04
 libc6 2.27-3ubuntu1.5
 libgcc1 1:8.4.0-1ubuntu1~18.04
InstallationDate: Installed on 2019-12-07 (836 days ago)
InstallationMedia: Ubuntu-Server 18.04.3 LTS "Bionic Beaver" - Release amd64 (20190805)
ProcEnviron:
 LANG=en_US.UTF-8
 TERM=screen
 SHELL=/bin/bash
 XDG_RUNTIME_DIR=<set>
 PATH=(custom, user)
SourcePackage: glibc
UpgradeStatus: No upgrade log present (probably fresh install)

** Affects: glibc (Ubuntu)
     Importance: Undecided
         Status: New


** Tags: amd64 apport-bug bionic uec-images

-- 
You received this bug notification because you are a member of Ubuntu
Foundations Bugs, which is subscribed to glibc in Ubuntu.
https://bugs.launchpad.net/bugs/1966100

Title:
  getaddrinfo not fork-safe

Status in glibc package in Ubuntu:
  New

Bug description:
  When compiling the following code and executing the program, the child
  process hangs forever on a mutex that was held by `getaddrinfo` on
  another thread at the time of the fork. I don't see anything in the
  manpage for `getaddrinfo` that suggests this shouldn't work. I suspect
  glibc is missing a call to `pthread_atfork`.

  mre.cpp
  ```
  #include <thread>

  #include <netdb.h>
  #include <unistd.h>

  void spamGetaddrinfo()
  {
      for (int i = 0; i< 100; ++i)
      {
          addrinfo *addrInfo{nullptr};
          ::getaddrinfo("google.com", "80", nullptr, &addrInfo);
      }
      printf("Thread is done\n");
  }

  int main()
  {
      auto thread = std::thread{spamGetaddrinfo};

      usleep(1);

      if (auto pid = fork(); pid != 0)
      {
          // Parent
          printf("Parent is done\n");
          thread.join();
      }
      else
      {
          // Child
          addrinfo *addrInfo{nullptr};
          ::getaddrinfo("google.com", "80", nullptr, &addrInfo);

          printf("Child is done\n");
      }
  }
  ```

  Run `g++ -std=c++17 -pthread mre.cpp && ./a.out`

  Expected output:
  ```
  Parent is done
  Child is done
  Thread is done
  ```

  Actual output:
  ```
  Parent is done
  Thread is done
  ```

  When finished, use `pkill a.out` to remove the leftover child process.

  ProblemType: Bug
  DistroRelease: Ubuntu 18.04
  Package: libc6 2.27-3ubuntu1.5
  ProcVersionSignature: Ubuntu 4.15.0-166.174-generic 4.15.18
  Uname: Linux 4.15.0-166-generic x86_64
  ApportVersion: 2.20.9-0ubuntu7.27
  Architecture: amd64
  Date: Wed Mar 23 10:32:43 2022
  Dependencies:
   gcc-8-base 8.4.0-1ubuntu1~18.04
   libc6 2.27-3ubuntu1.5
   libgcc1 1:8.4.0-1ubuntu1~18.04
  InstallationDate: Installed on 2019-12-07 (836 days ago)
  InstallationMedia: Ubuntu-Server 18.04.3 LTS "Bionic Beaver" - Release amd64 (20190805)
  ProcEnviron:
   LANG=en_US.UTF-8
   TERM=screen
   SHELL=/bin/bash
   XDG_RUNTIME_DIR=<set>
   PATH=(custom, user)
  SourcePackage: glibc
  UpgradeStatus: No upgrade log present (probably fresh install)

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/glibc/+bug/1966100/+subscriptions




More information about the foundations-bugs mailing list