if sudo:
args.append('sudo')
omit_sudo = False
- args += ['adjust-ulimits', 'daemon-helper', 'kill', py_version, '-c', pyscript]
+ args += ['stdin-killer', '--', py_version, '-c', pyscript]
return self.client_remote.run(args=args, wait=False, stdin=run.PIPE,
stdout=StringIO(), omit_sudo=omit_sudo)
if write:
pyscript = dedent("""
+ import fcntl
+ import os
+ import sys
import time
+ fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fcntl.fcntl(sys.stdin, fcntl.F_GETFL) | os.O_NONBLOCK)
+
with open("{path}", 'w') as f:
f.write("{content}")
f.flush()
while True:
- time.sleep(1)
+ print("open_background: keeping file open", file=sys.stderr)
+ try:
+ if os.read(0, 4096) == b"":
+ break
+ except BlockingIOError:
+ pass
+ time.sleep(2)
""").format(path=path, content=content)
else:
pyscript = dedent("""
+ import fcntl
+ import os
+ import sys
import time
+ fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fcntl.fcntl(sys.stdin, fcntl.F_GETFL) | os.O_NONBLOCK)
+
with open("{path}", 'r') as f:
while True:
- time.sleep(1)
+ print("open_background: keeping file open", file=sys.stderr)
+ try:
+ if os.read(0, 4096) == b"":
+ break
+ except BlockingIOError:
+ pass
+ time.sleep(2)
""").format(path=path)
rproc = self._run_python(pyscript)
path = os.path.join(self.hostfs_mntpt, basename)
pyscript = dedent("""
+ import fcntl
+ import sys
import time
import os
+ fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fcntl.fcntl(sys.stdin, fcntl.F_GETFL) | os.O_NONBLOCK)
+
os.mkdir("{path}")
fd = os.open("{path}", os.O_RDONLY)
while True:
- time.sleep(1)
+ print("open_dir_background: keeping dir open", file=sys.stderr)
+ try:
+ if os.read(0, 4096) == b"":
+ break
+ except BlockingIOError:
+ pass
+ time.sleep(2)
""").format(path=path)
rproc = self._run_python(pyscript)
path = os.path.join(self.hostfs_mntpt, basename)
script_builder = """
+ import sys
import time
import fcntl
- import struct"""
+ import os
+ import struct
+
+ fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fcntl.fcntl(sys.stdin, fcntl.F_GETFL) | os.O_NONBLOCK)
+ """
if do_flock:
script_builder += """
f1 = open("{path}-1", 'w')
- fcntl.flock(f1, fcntl.LOCK_EX | fcntl.LOCK_NB)"""
+ fcntl.flock(f1, fcntl.LOCK_EX | fcntl.LOCK_NB)
+ """
script_builder += """
f2 = open("{path}-2", 'w')
lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
fcntl.fcntl(f2, fcntl.F_SETLK, lockdata)
while True:
- time.sleep(1)
+ print("lock_background: keeping lock", file=sys.stderr)
+ try:
+ if os.read(0, 4096) == b"":
+ break
+ except BlockingIOError:
+ pass
+ time.sleep(2)
"""
pyscript = dedent(script_builder).format(path=path)
import time
import fcntl
import struct
+ import sys
f1 = open("{path}-1", 'w')
fcntl.flock(f1, fcntl.LOCK_EX)
f2 = open("{path}-2", 'w')
script_builder = """
import fcntl
import errno
- import struct"""
+ import struct
+ import sys
+ """
if do_flock:
script_builder += """
f1 = open("{path}-1", 'r')
path = os.path.join(self.hostfs_mntpt, basename)
pyscript = dedent("""
+ import fcntl
import os
+ import sys
import time
+ fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fcntl.fcntl(sys.stdin, fcntl.F_GETFL) | os.O_NONBLOCK)
+
fd = os.open("{path}", os.O_RDWR | os.O_CREAT, 0o644)
try:
while True:
+ print("write_background: writing", file=sys.stderr)
os.write(fd, b'content')
- time.sleep(1)
if not {loop}:
break
+ try:
+ if os.read(0, 4096) == b"":
+ break
+ except BlockingIOError:
+ pass
+ time.sleep(2)
except IOError as e:
pass
os.close(fd)
abs_path = os.path.join(self.hostfs_mntpt, fs_path)
pyscript = dedent("""
+ import fcntl
import sys
import time
import os
+ fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fcntl.fcntl(sys.stdin, fcntl.F_GETFL) | os.O_NONBLOCK)
+
n = {count}
abs_path = "{abs_path}"
handles.append(open(path, 'w'))
print("waiting with handles open", file=sys.stderr)
- while os.read(0, 4096) != b"":
- time.sleep(1)
+ while True:
+ print("open_n_background: keeping files open", file=sys.stderr)
+ try:
+ if os.read(0, 4096) == b"":
+ break
+ except BlockingIOError:
+ pass
+ time.sleep(2)
print("stdin closed, goodbye!", file=sys.stderr)
""").format(abs_path=abs_path, count=count)