3 ms·
7.5 years to find out how to close a file descriptor in a multithreaded process
- cperciva 7y agoTo elaborate on this: I figured out the dup2 part of the solution a long time ago, but I was stuck on "how do we make sure the descriptor we closed is the one we dup2ed and not a different one"; the obvious answer of using stat only works if you can create a sentinel file. So really the hard part was realizing that you can use MSG_PEEK since it's guaranteed to not have side effects (and with a random cookie can be used to identify whether a descriptor is "our" socket).