8000 Try to close more resouce on windows in system command. · ipython/ipython@fa62d22 · GitHub
[go: up one dir, main page]

Skip to content

Commit fa62d22

Browse files
committed
Try to close more resouce on windows in system command.
There is a resource leak spotted in ipython/ipykernel#1291, that only affects windows, this try to properly close the BufferReader/writrers. I find it weird to have to close things we did not set on the subprocess, I guess this is because it is supposed ot be used as a context manager, unfortunately the CM does .wait(), and we don't want to wait here.
1 parent aedea31 commit fa62d22

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

IPython/utils/_process_common.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ def process_handler(
9797
sys.stderr.flush()
9898
out = None
9999
finally:
100+
# p.stdin / p.stderr / p.stdin are not the values passes to Pope, They
101+
# are proxy reader/writer, and this is usually close in the context
102+
# manager, but the __exit__ does self.wait(), and we can't
103+
# afford this (see comment below),
104+
# so we copy the first part of the exit routine.
105+
#
106+
if p.stdout:
107+
p.stdout.close()
108+
if p.stderr:
109+
p.stderr.close()
110+
try: # Flushing a BufferedWriter may raise an error
111+
if p.stdin:
112+
p.stdin.close()
113+
except (Exception, KeyboardInterrupt):
114+
print("Error closing Stdin")
115+
100116
# Make really sure that we don't leave processes behind, in case the
101117
# call above raises an exception
102118
# We start by assuming the subprocess finished (to avoid NameErrors

0 commit comments

Comments
 (0)
0