When I create socket on Linux, it's possible to specify the flag O_CLOEXEC on creation time:
auto fd = socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
So there is no way, that some other thread will do fork()+exec() with this socket remaining open.
But on Mac, according to the manual, I can't do the same trick:
The socket has the indicated type, which specifies the semantics of communication. Currently defined types are: SOCK_STREAM SOCK_DGRAM SOCK_RAW SOCK_SEQPACKET SOCK_RDM
The flag O_CLOEXEC can only be set with a call to fcntl(). And it's not an atomic way - some thread may do exec() between calls to socket() and fcntl().
How to resolve this issue?