| 1 |
|
| 2 |
"""Exercise the absolute deadline on peer-controlled daemon handshake reads. |
| 3 |
|
| 4 |
The deadline is separate from the transfer's idle timeout. These probes cover |
| 5 |
the pre-module phase, anonymous modules, both argument reads, module-local |
| 6 |
policy, trickling input, and transfer timeout combinations. Raw sockets are |
| 7 |
intentional: a normal rsync client never leaves a handshake line unfinished. |
| 8 |
""" |
| 9 |
|
| 10 |
import select |
| 11 |
from pathlib import Path |
| 12 |
import socket |
| 13 |
import time |
| 14 |
|
| 15 |
from rsyncfns import ( |
| 16 |
RSYNC, SCRATCHDIR, makepath, require_tcp, rmtree, start_test_daemon, |
| 17 |
test_fail, write_daemon_conf, |
| 18 |
) |
| 19 |
|
| 20 |
require_tcp("raw stalled clients need a real TCP daemon; run with --use-tcp") |
| 21 |
|
| 22 |
BUILTIN = 60 |
| 23 |
FAST = 2 |
| 24 |
SLOP = 8 |
| 25 |
|
| 26 |
base = SCRATCHDIR / 'daemon-handshake-timeout' |
| 27 |
rmtree(base) |
| 28 |
mod = base / 'mod' |
| 29 |
makepath(mod) |
| 30 |
(mod / 'f').write_text('data\n') |
| 31 |
secrets = base / 'secrets' |
| 32 |
secrets.write_text('user:password\n') |
| 33 |
secrets.chmod(0o600) |
| 34 |
|
| 35 |
|
| 36 |
def port_of(url): |
| 37 |
return int(url.rsplit(':', 1)[1].rstrip('/')) |
| 38 |
|
| 39 |
|
| 40 |
def recv_until(sock, marker, deadline): |
| 41 |
"""Read through marker using an absolute monotonic deadline.""" |
| 42 |
data = b'' |
| 43 |
while marker not in data: |
| 44 |
left = deadline - time.monotonic() |
| 45 |
if left <= 0: |
| 46 |
test_fail(f"timed out waiting for {marker!r}; received {data!r}") |
| 47 |
sock.settimeout(left) |
| 48 |
try: |
| 49 |
chunk = sock.recv(4096) |
| 50 |
except OSError as e: |
| 51 |
test_fail(f"connection closed waiting for {marker!r}: {e}; got {data!r}") |
| 52 |
if not chunk: |
| 53 |
test_fail(f"EOF waiting for {marker!r}; received {data!r}") |
| 54 |
data += chunk |
| 55 |
return data |
| 56 |
|
| 57 |
|
| 58 |
def connect(port): |
| 59 |
deadline = time.monotonic() + 10 |
| 60 |
left = deadline - time.monotonic() |
| 61 |
try: |
| 62 |
sock = socket.create_connection(('127.0.0.1', port), timeout=left) |
| 63 |
except OSError as e: |
| 64 |
test_fail(f"could not connect to test daemon on port {port}: {e}") |
| 65 |
recv_until(sock, b'\n', deadline) |
| 66 |
return sock |
| 67 |
|
| 68 |
|
| 69 |
def open_module(port, name): |
| 70 |
sock = connect(port) |
| 71 |
sock.sendall(b'@RSYNCD: 31.0\n' + name.encode() + b'\n') |
| 72 |
got = recv_until(sock, b'\n', time.monotonic() + 10) |
| 73 |
if b'@RSYNCD: OK' not in got: |
| 74 |
test_fail(f"module {name!r} was not accepted: {got!r}") |
| 75 |
return sock |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
_TIMEOUT_MARKERS = ("daemon handshake timeout", "io timeout after") |
| 81 |
|
| 82 |
_LOG_TAIL_BYTES = 4096 |
| 83 |
|
| 84 |
|
| 85 |
def _log_mark(path): |
| 86 |
"""Size of the daemon log now, so a later read can ignore what came before. |
| 87 |
|
| 88 |
The daemons are long-lived and shared across observations, so an older |
| 89 |
timeout line must not be read as evidence about this one. |
| 90 |
""" |
| 91 |
if not path: |
| 92 |
return None |
| 93 |
try: |
| 94 |
return Path(path).stat().st_size |
| 95 |
except OSError: |
| 96 |
return 0 |
| 97 |
|
| 98 |
|
| 99 |
def _log_since(path, mark): |
| 100 |
"""The daemon's log text written since `mark`, bounded and printable.""" |
| 101 |
if not path or mark is None: |
| 102 |
return "" |
| 103 |
try: |
| 104 |
with open(path, 'rb') as fh: |
| 105 |
fh.seek(mark) |
| 106 |
raw = fh.read(_LOG_TAIL_BYTES + 1) |
| 107 |
except OSError: |
| 108 |
return "" |
| 109 |
clipped = len(raw) > _LOG_TAIL_BYTES |
| 110 |
text = raw[:_LOG_TAIL_BYTES].decode('utf-8', 'replace') |
| 111 |
|
| 112 |
|
| 113 |
text = ''.join(c if c == '\n' or ' ' <= c <= '~' else '.' for c in text) |
| 114 |
return text + ('...[truncated]' if clipped else '') |
| 115 |
|
| 116 |
|
| 117 |
def _timed_out_since(path, mark): |
| 118 |
"""Did the daemon record its own deadline firing during this observation?""" |
| 119 |
text = _log_since(path, mark) |
| 120 |
return any(m in text for m in _TIMEOUT_MARKERS) |
| 121 |
|
| 122 |
|
| 123 |
def _clock_note(wall, mono): |
| 124 |
"""State the clock discrepancy without drawing a conclusion from it. |
| 125 |
|
| 126 |
A stalled guest resyncing its wall clock produces this, but so does an NTP |
| 127 |
or manual step, and either can coexist with a genuine rsync failure -- so |
| 128 |
report the numbers and leave the cause to the log. |
| 129 |
""" |
| 130 |
if mono is None: |
| 131 |
return "" |
| 132 |
if abs(wall - mono) < 1.0: |
| 133 |
return "" |
| 134 |
return (f" [wall={wall:.2f}s vs monotonic={mono:.2f}s: wall time moved " |
| 135 |
f"{abs(wall - mono):.2f}s relative to monotonic during this " |
| 136 |
f"observation; the cause of the close is not settled by that alone]") |
| 137 |
|
| 138 |
|
| 139 |
def _daemon_log_note(path, mark): |
| 140 |
"""The daemon's own log for this observation, so a close can say why.""" |
| 141 |
if not path: |
| 142 |
return "" |
| 143 |
text = _log_since(path, mark).strip() |
| 144 |
if not text: |
| 145 |
return f" [daemon log {path} recorded nothing during this observation]" |
| 146 |
return " [daemon log: " + " | ".join(text.splitlines()[-12:]) + "]" |
| 147 |
|
| 148 |
|
| 149 |
def observe_close(sock, expected, label, *, trickle=None, must_close=True, |
| 150 |
daemon_log=None): |
| 151 |
"""Observe EOF/reset by recv() before an absolute deadline. |
| 152 |
|
| 153 |
A failed send is only a hint: keep receiving until EOF or a terminal read |
| 154 |
error. This avoids the clean-FIN race where a later send can still succeed |
| 155 |
on Cygwin and some TCP stacks. |
| 156 |
""" |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
start = time.monotonic() |
| 164 |
start_wall = time.time() |
| 165 |
log_mark = _log_mark(daemon_log) |
| 166 |
deadline = start + expected + SLOP |
| 167 |
next_send = start if trickle is not None else deadline |
| 168 |
can_send = trickle is not None |
| 169 |
closed = None |
| 170 |
closed_mono = None |
| 171 |
|
| 172 |
while True: |
| 173 |
now = time.monotonic() |
| 174 |
if now >= deadline: |
| 175 |
break |
| 176 |
wait = min(deadline, next_send) - now if can_send else deadline - now |
| 177 |
try: |
| 178 |
readable, _, _ = select.select([sock], [], [], max(0, wait)) |
| 179 |
except OSError as e: |
| 180 |
test_fail(f"{label}: select failed before closure was observed: {e}") |
| 181 |
|
| 182 |
if readable: |
| 183 |
try: |
| 184 |
data = sock.recv(65536) |
| 185 |
except OSError: |
| 186 |
closed, closed_mono = time.time() - start_wall, time.monotonic() - start |
| 187 |
break |
| 188 |
if not data: |
| 189 |
closed, closed_mono = time.time() - start_wall, time.monotonic() - start |
| 190 |
break |
| 191 |
|
| 192 |
now = time.monotonic() |
| 193 |
if can_send and now >= next_send: |
| 194 |
try: |
| 195 |
sock.sendall(trickle) |
| 196 |
except OSError: |
| 197 |
|
| 198 |
|
| 199 |
can_send = False |
| 200 |
next_send = now + 0.20 |
| 201 |
|
| 202 |
if closed is None: |
| 203 |
if must_close: |
| 204 |
test_fail(f"{label}: connection stayed open past {expected + SLOP:.1f}s" |
| 205 |
f"{_clock_note(time.time() - start_wall, time.monotonic() - start)}" |
| 206 |
f"{_daemon_log_note(daemon_log, log_mark)}") |
| 207 |
return None |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
minimum = max(0.5, expected - 1.25) |
| 212 |
if closed < minimum: |
| 213 |
test_fail(f"{label}: connection closed after {closed:.2f}s, before the " |
| 214 |
f"expected timeout window ({minimum:.2f}s); this was not the " |
| 215 |
f"deadline.{_clock_note(closed, closed_mono)}" |
| 216 |
f"{_daemon_log_note(daemon_log, log_mark)}") |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
if closed_mono is not None and closed_mono < minimum \ |
| 223 |
and not _timed_out_since(daemon_log, log_mark): |
| 224 |
test_fail(f"{label}: wall time says the close was on time ({closed:.2f}s) " |
| 225 |
f"but monotonic says it came early ({closed_mono:.2f}s), and the " |
| 226 |
f"daemon logged no timeout of its own in that window, so this is " |
| 227 |
f"not demonstrably the deadline." |
| 228 |
f"{_clock_note(closed, closed_mono)}" |
| 229 |
f"{_daemon_log_note(daemon_log, log_mark)}") |
| 230 |
if not must_close: |
| 231 |
test_fail(f"{label}: connection unexpectedly closed after {closed:.2f}s" |
| 232 |
f"{_clock_note(closed, closed_mono)}" |
| 233 |
f"{_daemon_log_note(daemon_log, log_mark)}") |
| 234 |
return closed |
| 235 |
|
| 236 |
|
| 237 |
def stall_greeting(port, expected, label, *, daemon_log=None): |
| 238 |
sock = connect(port) |
| 239 |
try: |
| 240 |
sock.sendall(b'@RSYNCD: 31.0') |
| 241 |
return observe_close(sock, expected, label, trickle=b'x', |
| 242 |
daemon_log=daemon_log) |
| 243 |
finally: |
| 244 |
sock.close() |
| 245 |
|
| 246 |
|
| 247 |
def transfer_args(client_timeout=None): |
| 248 |
args = [b'--server', b'--sender', b'-logDtpre.iLsfxCIvu'] |
| 249 |
if client_timeout is not None: |
| 250 |
args.append(f'--timeout={client_timeout}'.encode()) |
| 251 |
args += [b'.', b'/'] |
| 252 |
return b'\0'.join(args) + b'\0\0' |
| 253 |
|
| 254 |
|
| 255 |
def stalled_transfer(port, module, expected, label, *, client_timeout=None, |
| 256 |
must_close=True, daemon_log=None): |
| 257 |
sock = open_module(port, module) |
| 258 |
try: |
| 259 |
sock.sendall(transfer_args(client_timeout)) |
| 260 |
return observe_close(sock, expected, label, must_close=must_close, |
| 261 |
daemon_log=daemon_log) |
| 262 |
finally: |
| 263 |
sock.close() |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
conf_fast = write_daemon_conf( |
| 268 |
[ |
| 269 |
('zero', {'path': str(mod), 'timeout': '0', 'read only': 'yes'}), |
| 270 |
('long', {'path': str(mod), 'timeout': '5', 'read only': 'yes'}), |
| 271 |
('clientlow', {'path': str(mod), 'timeout': '8', 'read only': 'yes'}), |
| 272 |
('modulelow', {'path': str(mod), 'timeout': '3', 'read only': 'yes'}), |
| 273 |
], |
| 274 |
globals={ |
| 275 |
'timeout': str(FAST), |
| 276 |
'pid file': str(base / 'fast.pid'), |
| 277 |
'log file': str(base / 'fast.log'), |
| 278 |
}, |
| 279 |
name='handshake-timeout-fast.conf', |
| 280 |
) |
| 281 |
port_fast = port_of(start_test_daemon(conf_fast, 12987, rsync_cmd=RSYNC)) |
| 282 |
global_took = stall_greeting(port_fast, FAST, 'global handshake timeout', |
| 283 |
daemon_log=base / 'fast.log') |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
conf_args = write_daemon_conf( |
| 290 |
[ |
| 291 |
('args', {'path': str(mod), 'timeout': str(FAST), 'read only': 'yes'}), |
| 292 |
('auth', { |
| 293 |
'path': str(mod), |
| 294 |
'timeout': str(FAST), |
| 295 |
'read only': 'yes', |
| 296 |
'auth users': 'user', |
| 297 |
'secrets file': str(secrets), |
| 298 |
}), |
| 299 |
], |
| 300 |
globals={ |
| 301 |
'timeout': '20', |
| 302 |
'pid file': str(base / 'args.pid'), |
| 303 |
'log file': str(base / 'args.log'), |
| 304 |
}, |
| 305 |
name='handshake-timeout-args.conf', |
| 306 |
) |
| 307 |
port_args = port_of(start_test_daemon(conf_args, 12988, rsync_cmd=RSYNC)) |
| 308 |
|
| 309 |
s = connect(port_args) |
| 310 |
try: |
| 311 |
s.sendall(b'@RSYNCD: 31.0\nauth\n') |
| 312 |
auth_reply = recv_until(s, b'\n', time.monotonic() + 10) |
| 313 |
if b'@RSYNCD: AUTHREQD ' not in auth_reply: |
| 314 |
test_fail(f"authenticated module did not issue a challenge: {auth_reply!r}") |
| 315 |
auth_took = observe_close(s, FAST, 'unauthenticated claimed slot', trickle=b'x', |
| 316 |
daemon_log=base / 'args.log') |
| 317 |
finally: |
| 318 |
s.close() |
| 319 |
|
| 320 |
s = open_module(port_args, 'args') |
| 321 |
try: |
| 322 |
first_took = observe_close(s, FAST, 'anonymous first argument read', trickle=b'x', |
| 323 |
daemon_log=base / 'args.log') |
| 324 |
finally: |
| 325 |
s.close() |
| 326 |
|
| 327 |
s = open_module(port_args, 'args') |
| 328 |
try: |
| 329 |
|
| 330 |
|
| 331 |
s.sendall(b'--server\0--sender\0-s\0\0') |
| 332 |
second_took = observe_close(s, FAST, 'anonymous secluded-args read', trickle=b'x', |
| 333 |
daemon_log=base / 'args.log') |
| 334 |
finally: |
| 335 |
s.close() |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
long_took = stalled_transfer(port_fast, 'long', 5, |
| 342 |
'module transfer timeout above global', |
| 343 |
daemon_log=base / 'fast.log') |
| 344 |
client_took = stalled_transfer(port_fast, 'clientlow', 3, |
| 345 |
'client timeout below module', client_timeout=3, |
| 346 |
daemon_log=base / 'fast.log') |
| 347 |
module_took = stalled_transfer(port_fast, 'modulelow', 3, |
| 348 |
'module timeout below client', client_timeout=8, |
| 349 |
daemon_log=base / 'fast.log') |
| 350 |
stalled_transfer(port_fast, 'zero', 4, |
| 351 |
'explicit module timeout=0 transfer', must_close=False, |
| 352 |
daemon_log=base / 'fast.log') |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
conf_default = write_daemon_conf( |
| 357 |
[('default', {'path': str(mod), 'read only': 'yes'})], |
| 358 |
globals={ |
| 359 |
'pid file': str(base / 'default.pid'), |
| 360 |
'log file': str(base / 'default.log'), |
| 361 |
}, |
| 362 |
name='handshake-timeout-default.conf', |
| 363 |
) |
| 364 |
port_default = port_of(start_test_daemon(conf_default, 12989, rsync_cmd=RSYNC)) |
| 365 |
builtin_took = stall_greeting(port_default, BUILTIN, 'built-in handshake timeout', |
| 366 |
daemon_log=base / 'default.log') |
| 367 |
|
| 368 |
print( |
| 369 |
'daemon handshake deadlines: ' |
| 370 |
f'global={global_took:.1f}s, auth={auth_took:.1f}s, ' |
| 371 |
f'anonymous args={first_took:.1f}/{second_took:.1f}s, ' |
| 372 |
f'transfer module/client limits={long_took:.1f}/{client_took:.1f}/{module_took:.1f}s, ' |
| 373 |
f'built-in={builtin_took:.1f}s; explicit module timeout=0 stayed open' |
| 374 |
) |