Skip to content

Commit d64146c

Browse files
it-xvstinner
authored andcommitted
Fix socket leaks (#351)
* Fix socket leaks * Fixed sockets leak
1 parent 9c2c42c commit d64146c

File tree

1 file changed

+48
-38
lines changed

1 file changed

+48
-38
lines changed

Lib/ftplib.py

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -412,12 +412,14 @@ def retrbinary(self, cmd, callback, blocksize=8192, rest=None):
412412
"""
413413
self.voidcmd('TYPE I')
414414
conn = self.transfercmd(cmd, rest)
415-
while 1:
416-
data = conn.recv(blocksize)
417-
if not data:
418-
break
419-
callback(data)
420-
conn.close()
415+
try:
416+
while 1:
417+
data = conn.recv(blocksize)
418+
if not data:
419+
break
420+
callback(data)
421+
finally:
422+
conn.close()
421423
return self.voidresp()
422424

423425
def retrlines(self, cmd, callback = None):
@@ -435,21 +437,25 @@ def retrlines(self, cmd, callback = None):
435437
if callback is None: callback = print_line
436438
resp = self.sendcmd('TYPE A')
437439
conn = self.transfercmd(cmd)
438-
fp = conn.makefile('rb')
439-
while 1:
440-
line = fp.readline(self.maxline + 1)
441-
if len(line) > self.maxline:
442-
raise Error("got more than %d bytes" % self.maxline)
443-
if self.debugging > 2: print '*retr*', repr(line)
444-
if not line:
445-
break
446-
if line[-2:] == CRLF:
447-
line = line[:-2]
448-
elif line[-1:] == '\n':
449-
line = line[:-1]
450-
callback(line)
451-
fp.close()
452-
conn.close()
440+
fp = None
441+
try:
442+
fp = conn.makefile('rb')
443+
while 1:
444+
line = fp.readline(self.maxline + 1)
445+
if len(line) > self.maxline:
446+
raise Error("got more than %d bytes" % self.maxline)
447+
if self.debugging > 2: print '*retr*', repr(line)
448+
if not line:
449+
break
450+
if line[-2:] == CRLF:
451+
line = line[:-2]
452+
elif line[-1:] == '\n':
453+
line = line[:-1]
454+
callback(line)
455+
finally:
456+
if fp:
457+
fp.close()
458+
conn.close()
453459
return self.voidresp()
454460

455461
def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
@@ -469,12 +475,14 @@ def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
469475
"""
470476
self.voidcmd('TYPE I')
471477
conn = self.transfercmd(cmd, rest)
472-
while 1:
473-
buf = fp.read(blocksize)
474-
if not buf: break
475-
conn.sendall(buf)
476-
if callback: callback(buf)
477-
conn.close()
478+
try:
479+
while 1:
480+
buf = fp.read(blocksize)
481+
if not buf: break
482+
conn.sendall(buf)
483+
if callback: callback(buf)
484+
finally:
485+
conn.close()
478486
return self.voidresp()
479487

480488
def storlines(self, cmd, fp, callback=None):
@@ -491,17 +499,19 @@ def storlines(self, cmd, fp, callback=None):
491499
"""
492500
self.voidcmd('TYPE A')
493501
conn = self.transfercmd(cmd)
494-
while 1:
495-
buf = fp.readline(self.maxline + 1)
496-
if len(buf) > self.maxline:
497-
raise Error("got more than %d bytes" % self.maxline)
498-
if not buf: break
499-
if buf[-2:] != CRLF:
500-
if buf[-1] in CRLF: buf = buf[:-1]
501-
buf = buf + CRLF
502-
conn.sendall(buf)
503-
if callback: callback(buf)
504-
conn.close()
502+
try:
503+
while 1:
504+
buf = fp.readline(self.maxline + 1)
505+
if len(buf) > self.maxline:
506+
raise Error("got more than %d bytes" % self.maxline)
507+
if not buf: break
508+
if buf[-2:] != CRLF:
509+
if buf[-1] in CRLF: buf = buf[:-1]
510+
buf = buf + CRLF
511+
conn.sendall(buf)
512+
if callback: callback(buf)
513+
finally:
514+
conn.close()
505515
return self.voidresp()
506516

507517
def acct(self, password):

0 commit comments

Comments
 (0)