<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/63297>63297</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            `StartSubprocess` gets stuck when opfile limit is large.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          zu1k
      </td>
    </tr>
</table>

<pre>
    As described in https://github.com/google/sanitizers/issues/1662

https://github.com/llvm/llvm-project/blob/f9d0bf06319203a8cbb47d89c2f39d2c782f3887/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp#L465 

`compiler-rt` uses a crude approach to close file descripters, which causes child processes to get stuck and fill up the CPU when the maximum file descriptor limit is too large. 

It is better to close only the actual open file descriptors rather than traversing the entire potential range. I'm not sure if the following code is generic.

```c
void close_file_descriptors() {
    DIR *dir;
    struct dirent *entry;
 int fd;

    dir = opendir("/proc/self/fd/");
    if (dir == NULL) {
        perror("opendir failed");
        return;
 }

    while ((entry = readdir(dir)) != NULL) {
        fd = atoi(entry->d_name);

        if (fd >= 3) {
 close(fd);
        }
    }

    closedir(dir);
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVF2P2ygU_TX45WoiDBl_PPhh2jTSSKPVaqs-RxiubbYYLMCTTn_9CpLJx2xVqVbk-OJ7DucAPiIEPVrEjjx-Io-7Qqxxcr77uZbfi96pt-4pgMIgve5RgbYwxbgEwp8I2xO2H3Wc1n4j3ZwK50aDhO2DsDrqn-gDYXsdworpoawqRuiO0KfT_TdMxry-_z0s3v2LMhK2743rCdsPraL9QCtetoxy0ci-39aqaSUbeKuYrBs28KapCdtLNy_aoH_wCW90fyvuIN08O3s3tLigfxyM7qXCZSOXhTD-sq0e4VY4qegtcUVhDRhAgPSrQhDL4p2QE0QH0riAMGiD51VcYl6Vz3CctJxAigyV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