New GitHub issue #101438 from colesbury:<br>

<hr>

<pre>
The PR https://github.com/python/cpython/pull/31696 attempts to fix the "leak" of file descriptors when the iterator is not exhausted. That PR fixes the warning, but not the underlying issue that the files aren't closed until the next tracing garbage collection cycle.

Note that there isn't truly a leak of file descriptors. The file descriptors are eventually closed when the file object is finalized (at cyclic garbage collection). The point of the `ResourceWarning` (in my understanding) is that waiting until the next garbage collection cycle means that you may temporarily have a lot of unwanted open file descriptors, which could exhaust the global limit or prevent successful writes to those files on Windows.


```python
# run with ulimit -Sn 1000
import xml.etree.ElementTree as ET
import tempfile

import gc
gc.disable()

def run():
    with tempfile.NamedTemporaryFile("w") as f:
        f.write("<document />junk")

        for i in range(10000):
            it = ET.iterparse(f.name)
            del it

run()
```

On my system, after lowering the file descriptor limit to 1000 (via `ulimit -Sn 1000`) I get:

```
OSError: [Errno 24] Too many open files: '/tmp/tmpwwmd9gp6'
```
</pre>

<hr>

<a href="https://github.com/python/cpython/issues/101438">View on GitHub</a>
<p>Labels: type-bug</p>
<p>Assignee: </p>