Opened 17 years ago
Closed 13 years ago
#6499 closed Cleanup/optimization (invalid)
Close open file descriptors on compression
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Core (Other) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Close another file descriptor leak.
Attachments (2)
Change History (9)
by , 17 years ago
Attachment: | 0046-Close-open-file-descriptors.patch added |
---|
comment:1 by , 17 years ago
comment:2 by , 17 years ago
The cStringIO import was already there, so this might be good to put in a separate ticket, together with other Jython fixes probably. Thanks for the note though.
Every open file object will eventually be closed when the variable goes out of scope and the garbage collector frees it. The problem is that this is not guaranteed. The variable might be part of reference cycles, in which case the garbage collector will never free it, or the program terminates before running the garbage collection. Both cases might occur very seldom (if at all), but better be safe than sorry, since open file desriptors are especially annoying on Windows systems or network shares.
In python 2.5 you even can even use the new "with" statement to do this in a nicer way, but the compatible way is a try-finally wrapper.
comment:3 by , 17 years ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:4 by , 14 years ago
Needs tests: | set |
---|---|
Severity: | → Normal |
Type: | → Cleanup/optimization |
comment:5 by , 13 years ago
Component: | Uncategorized → Core (Other) |
---|---|
Easy pickings: | unset |
Needs tests: | unset |
Patch needs improvement: | unset |
UI/UX: | unset |
comment:6 by , 13 years ago
I may be missing something, but we're writing to a StringIO
, so there isn't a fd involved at all, let alone leaked.
comment:7 by , 13 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Closing the GzipFile
is useful because it flushes its buffers. Closing the StringIO
isn't necessary; it will be garbage collected eventually.
I fail to see the point of the patch: there's no fd involved in this code, and even if zbuf
were a fd, the patch wouldn't close it (see http://hg.python.org/cpython/file/91cbac92a665/Lib/gzip.py#l359).
Firstly, unconditionally importing cStringIO isn't a good idea. It won't work with Jython.
Secondly, the file descriptor should be closed as soon as the zbuf variable goes out of scope, due to its reference count decreasing to zero. What is the situation where the file descriptor is leaking here?