Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use poll implementation to fix file descriptor leak #72

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions fabric/context_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,19 +455,30 @@ def shell_env(**kw):


def _forwarder(chan, sock):
# Bidirectionally forward data between a socket and a Paramiko channel.
while True:
r, w, x = select.select([sock, chan], [], [])
if sock in r:
data = sock.recv(1024)
if len(data) == 0:
break
chan.send(data)
if chan in r:
data = chan.recv(1024)
if len(data) == 0:
break
sock.send(data)
"""
Bidirectionally forward data between a socket and a Paramiko channel.
"""
mapping = {
sock.fileno(): (sock, chan),
chan.fileno(): (chan, sock),
}
poller = select.poll()
for fd in mapping:
poller.register(fd, select.POLLIN)
active = True
while active:
events = poller.poll()
for fd, flag in events:
if flag & select.POLLIN:
sender, receiver = mapping[fd]
data = sender.recv(1024)
if data:
receiver.send(data)
else:
active = False
# Handle unexpected hangups
if flag & select.POLLHUP:
active = False
chan.close()
sock.close()

Expand Down
7 changes: 4 additions & 3 deletions fabric/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
import socket
import time
from select import select
import select
from collections import deque

import six
Expand Down Expand Up @@ -268,8 +268,9 @@ def input_loop(chan, f, using_pty):
if msvcrt.kbhit():
byte = msvcrt.getch()
elif waitable:
r, w, x = select([f], [], [], 0.0)
if f in r:
poller = select.poll()
poller.register(f, select.POLLIN)
if poller.poll(0):
byte = f.read(1)
else:
byte = f.read(1)
Expand Down