getaddrinfo not fork-safe
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
glibc (Ubuntu) |
New
|
Undecided
|
Unassigned |
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};
}
printf("Thread is done\n");
}
int main()
{
auto thread = std::thread{
usleep(1);
if (auto pid = fork(); pid != 0)
{
// Parent
}
else
{
// Child
addrinfo *addrInfo{nullptr};
}
}
```
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
ProcVersionSign
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~
libc6 2.27-3ubuntu1.5
libgcc1 1:8.4.0-
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_
PATH=(custom, user)
SourcePackage: glibc
UpgradeStatus: No upgrade log present (probably fresh install)