mirror of
https://github.com/borgbackup/borg.git
synced 2024-12-24 08:45:13 +00:00
Make sure stdin and stdout are non-blocking to avoid dead locks
This commit is contained in:
parent
e80e600d41
commit
e48d7aa88c
1 changed files with 14 additions and 4 deletions
|
@ -73,7 +73,7 @@ def __init__(self, name):
|
|||
self.name = name
|
||||
|
||||
def __init__(self, location, create=False):
|
||||
self.cache = LRUCache(200)
|
||||
self.cache = LRUCache(256)
|
||||
self.to_send = ''
|
||||
self.extra = {}
|
||||
self.pending = {}
|
||||
|
@ -84,6 +84,8 @@ def __init__(self, location, create=False):
|
|||
self.p = Popen(args, bufsize=0, stdin=PIPE, stdout=PIPE)
|
||||
self.stdin_fd = self.p.stdin.fileno()
|
||||
self.stdout_fd = self.p.stdout.fileno()
|
||||
fcntl.fcntl(self.stdin_fd, fcntl.F_SETFL, fcntl.fcntl(self.stdin_fd, fcntl.F_GETFL) | os.O_NONBLOCK)
|
||||
fcntl.fcntl(self.stdout_fd, fcntl.F_SETFL, fcntl.fcntl(self.stdout_fd, fcntl.F_GETFL) | os.O_NONBLOCK)
|
||||
self.r_fds = [self.stdout_fd]
|
||||
self.x_fds = [self.stdin_fd, self.stdout_fd]
|
||||
|
||||
|
@ -97,9 +99,10 @@ def __del__(self):
|
|||
|
||||
def call(self, cmd, args, wait=True):
|
||||
self.msgid += 1
|
||||
self.p.stdin.write(msgpack.packb((1, self.msgid, cmd, args)))
|
||||
while wait:
|
||||
r, w, x = select.select(self.r_fds, [], self.x_fds, 1)
|
||||
to_send = msgpack.packb((1, self.msgid, cmd, args))
|
||||
w_fds = [self.stdin_fd]
|
||||
while wait or to_send:
|
||||
r, w, x = select.select(self.r_fds, w_fds, self.x_fds, 1)
|
||||
if x:
|
||||
raise Exception('FD exception occured')
|
||||
if r:
|
||||
|
@ -116,6 +119,13 @@ def call(self, cmd, args, wait=True):
|
|||
args = self.pending.pop(msgid, None)
|
||||
if args is not None:
|
||||
self.cache[args] = msgid, res, error
|
||||
if w:
|
||||
if to_send:
|
||||
n = os.write(self.stdin_fd, to_send)
|
||||
assert n > 0
|
||||
to_send = to_send[n:]
|
||||
else:
|
||||
w_fds = []
|
||||
|
||||
def _read(self):
|
||||
data = os.read(self.stdout_fd, BUFSIZE)
|
||||
|
|
Loading…
Reference in a new issue