Debian Codegraph @

tests/testlib/sigpipe-remote.py

raw
1 #!/usr/bin/env python3
2
3 import io
4 import os
5 import subprocess
6 import sys
7 import time
8
9
10 if isinstance(sys.stdout.buffer, io.BufferedWriter):
11 print('SIGPIPE-HELPER: script need unbuffered output', file=sys.stderr)
12 sys.exit(255)
13
14 DEBUG_FILE = os.environ.get('SIGPIPE_REMOTE_DEBUG_FILE')
15 if DEBUG_FILE is None:
16 debug_stream = sys.stderr.buffer
17 else:
18 debug_stream = open(DEBUG_FILE, 'bw', buffering=0)
19
20 SYNCFILE1 = os.environ.get('SYNCFILE1')
21 SYNCFILE2 = os.environ.get('SYNCFILE2')
22 if SYNCFILE1 is None:
23 print('SIGPIPE-HELPER: missing variable $SYNCFILE1', file=sys.stderr)
24 sys.exit(255)
25 if SYNCFILE2 is None:
26 print('SIGPIPE-HELPER: missing variable $SYNCFILE2', file=sys.stderr)
27 sys.exit(255)
28
29
30 def _timeout_factor():
31 """return the current modification to timeout"""
32 factor_percentage = int(os.environ.get('HGTEST_TIMEOUT_PERCENTAGE', 100))
33 return factor_percentage / 100
34
35
36 def wait_file(path, timeout=10):
37 timeout *= _timeout_factor()
38 start = time.time()
39 while not os.path.exists(path):
40 if (time.time() - start) > timeout:
41 raise RuntimeError(b"timed out waiting for file: %s" % path)
42 time.sleep(0.01)
43
44
45 def write_file(path, content=b''):
46 with open(path, 'wb') as f:
47 f.write(content)
48
49
50 # end of mercurial.testing content
51
52
53 def sysbytes(s):
54 return s.encode('utf-8')
55
56
57 def sysstr(s):
58 return s.decode('latin-1')
59
60
61 debug_stream.write(b'SIGPIPE-HELPER: Starting\n')
62
63 TESTLIB_DIR = os.path.dirname(sys.argv[0])
64 WAIT_SCRIPT = os.path.join(TESTLIB_DIR, 'wait-on-file')
65
66 hooks_cmd = '%s 10 %s %s'
67 hooks_cmd %= (
68 WAIT_SCRIPT,
69 SYNCFILE2,
70 SYNCFILE1,
71 )
72
73 try:
74 cmd = ['hg']
75 cmd += sys.argv[1:]
76 sub = subprocess.Popen(
77 cmd,
78 bufsize=0,
79 close_fds=True,
80 stdin=sys.stdin,
81 stdout=subprocess.PIPE,
82 stderr=subprocess.PIPE,
83 )
84
85 basedir = os.path.dirname(sys.argv[0])
86 worker = os.path.join(basedir, 'sigpipe-worker.py')
87
88 cmd = [sys.executable, worker]
89
90 stdout_worker = subprocess.Popen(
91 cmd,
92 bufsize=0,
93 close_fds=True,
94 stdin=sub.stdout,
95 stdout=sys.stdout,
96 stderr=sys.stderr,
97 )
98
99 stderr_worker = subprocess.Popen(
100 cmd,
101 bufsize=0,
102 close_fds=True,
103 stdin=sub.stderr,
104 stdout=sys.stderr,
105 stderr=sys.stderr,
106 )
107 debug_stream.write(b'SIGPIPE-HELPER: Redirection in place\n')
108 os.close(sub.stdout.fileno())
109 os.close(sub.stderr.fileno())
110 debug_stream.write(b'SIGPIPE-HELPER: pipes closed in main\n')
111
112 try:
113 wait_file(sysbytes(SYNCFILE1))
114 except RuntimeError as exc:
115 msg = sysbytes(str(exc))
116 debug_stream.write(b'SIGPIPE-HELPER: wait failed: %s\n' % msg)
117 else:
118 debug_stream.write(b'SIGPIPE-HELPER: SYNCFILE1 detected\n')
119 stdout_worker.kill()
120 stderr_worker.kill()
121 stdout_worker.wait(10)
122 stderr_worker.wait(10)
123 debug_stream.write(b'SIGPIPE-HELPER: worker killed\n')
124
125 debug_stream.write(b'SIGPIPE-HELPER: creating SYNCFILE2\n')
126 write_file(sysbytes(SYNCFILE2))
127 finally:
128 debug_stream.write(b'SIGPIPE-HELPER: Shutting down\n')
129 if not sys.stdin.closed:
130 sys.stdin.close()
131 try:
132 sub.wait(timeout=30)
133 except subprocess.TimeoutExpired:
134 msg = b'SIGPIPE-HELPER: Server process failed to terminate\n'
135 debug_stream.write(msg)
136 sub.kill()
137 sub.wait()
138 msg = b'SIGPIPE-HELPER: Server process killed\n'
139 else:
140 msg = b'SIGPIPE-HELPER: Server process terminated with status %d\n'
141 msg %= sub.returncode
142 debug_stream.write(msg)
143 debug_stream.write(b'SIGPIPE-HELPER: Shut down\n')