1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464
|
from __future__ import annotations
import array
import errno
import gc
import io
import os
import platform
import re
import socket
import sys
import tempfile
import threading
import time
from collections.abc import Generator, Iterable, Iterator
from contextlib import suppress
from ipaddress import IPv4Address, IPv6Address
from pathlib import Path
from socket import AddressFamily
from ssl import SSLContext, SSLError
from threading import Thread
from typing import TYPE_CHECKING, Any, Literal, NoReturn, Protocol, TypeVar, cast
from unittest import mock
from unittest.mock import MagicMock, patch
import psutil
import pytest
from _pytest.fixtures import SubRequest
from _pytest.logging import LogCaptureFixture
from _pytest.monkeypatch import MonkeyPatch
from _pytest.tmpdir import TempPathFactory
from pytest import FixtureRequest
from pytest_mock.plugin import MockerFixture
from anyio import (
BrokenResourceError,
BusyResourceError,
ClosedResourceError,
EndOfStream,
Event,
TCPConnectable,
TypedAttributeLookupError,
UNIXConnectable,
as_connectable,
connect_tcp,
connect_unix,
create_connected_udp_socket,
create_connected_unix_datagram_socket,
create_task_group,
create_tcp_listener,
create_udp_socket,
create_unix_datagram_socket,
create_unix_listener,
fail_after,
getaddrinfo,
getnameinfo,
move_on_after,
notify_closing,
wait_all_tasks_blocked,
wait_readable,
wait_socket_readable,
wait_socket_writable,
wait_writable,
)
from anyio._core._eventloop import get_async_backend
from anyio.abc import (
ConnectedUDPSocket,
ConnectedUNIXDatagramSocket,
IPSockAddrType,
Listener,
SocketAttribute,
SocketListener,
SocketStream,
UDPSocket,
UNIXDatagramSocket,
UNIXSocketStream,
)
from anyio.lowlevel import checkpoint
from anyio.streams.stapled import MultiListener
from anyio.streams.tls import TLSConnectable
from .conftest import asyncio_params, no_other_refs
if sys.version_info < (3, 11):
from exceptiongroup import ExceptionGroup
if TYPE_CHECKING:
from _typeshed import FileDescriptorLike
AnyIPAddressFamily = Literal[
AddressFamily.AF_UNSPEC, AddressFamily.AF_INET, AddressFamily.AF_INET6
]
# If a socket can bind to ::1, the current environment has IPv6 properly configured
has_ipv6 = False
if socket.has_ipv6:
try:
s = socket.socket(AddressFamily.AF_INET6)
try:
s.bind(("::1", 0))
finally:
s.close()
del s
except OSError:
pass
else:
has_ipv6 = True
skip_ipv6_mark = pytest.mark.skipif(not has_ipv6, reason="IPv6 is not available")
skip_unix_abstract_mark = pytest.mark.skipif(
not sys.platform.startswith("linux"),
reason="Abstract namespace sockets is a Linux only feature",
)
@pytest.fixture
def fake_localhost_dns(monkeypatch: MonkeyPatch) -> None:
def fake_getaddrinfo(*args: Any, **kwargs: Any) -> object:
# Make it return IPv4 addresses first so we can test the IPv6 preference
results = real_getaddrinfo(*args, **kwargs)
return sorted(results, key=lambda item: item[0])
real_getaddrinfo = socket.getaddrinfo
monkeypatch.setattr("socket.getaddrinfo", fake_getaddrinfo)
@pytest.fixture(
params=[
pytest.param(AddressFamily.AF_INET, id="ipv4"),
pytest.param(AddressFamily.AF_INET6, id="ipv6", marks=[skip_ipv6_mark]),
]
)
def family(request: SubRequest) -> AnyIPAddressFamily:
return request.param
@pytest.fixture
def check_asyncio_bug(anyio_backend_name: str, family: AnyIPAddressFamily) -> None:
if (
anyio_backend_name == "asyncio"
and sys.platform == "win32"
and family == AddressFamily.AF_INET6
):
import asyncio
policy = asyncio.get_event_loop_policy()
if policy.__class__.__name__ == "WindowsProactorEventLoopPolicy":
pytest.skip("Does not work due to a known bug (39148)")
class SockFdFactoryProtocol(Protocol):
def __call__(
self,
family: socket.AddressFamily,
kind: socket.SocketKind,
*,
bound: bool = False,
connected: bool = False,
) -> socket.socket | int: ...
@pytest.fixture(
params=[pytest.param(False, id="sock"), pytest.param(True, id="fileno")]
)
def sock_or_fd_factory(
request: SubRequest, tmp_path_factory: TempPathFactory
) -> SockFdFactoryProtocol:
def factory(
family: socket.AddressFamily,
kind: socket.SocketKind,
*,
bound: bool = False,
connected: bool = False,
) -> socket.socket | int:
sock = socket.socket(family, kind)
if bound or connected:
if family in (socket.AF_INET, socket.AF_INET6):
local_addr: str | tuple[str, int] = ("localhost", 0)
else:
local_addr = str(tmp_path_factory.mktemp("anyio") / "socket")
if bound:
sock.bind(local_addr)
if kind == socket.SOCK_STREAM:
sock.listen()
elif connected:
server_sock = socket.socket(family, kind)
request.addfinalizer(server_sock.close)
server_sock.bind(local_addr)
if kind == socket.SOCK_STREAM:
server_sock.listen()
sock.connect(server_sock.getsockname())
if request.param:
return sock.detach()
else:
request.addfinalizer(sock.close)
return sock
return factory
_T = TypeVar("_T")
def _identity(v: _T) -> _T:
return v
def fill_socket(sock: socket.socket) -> None:
try:
while True:
sock.send(b"x" * 65536)
except BlockingIOError:
pass
# _ProactorBasePipeTransport.abort() after _ProactorBasePipeTransport.close()
# does not cancel writes: https://bugs.python.org/issue44428
_ignore_win32_resource_warnings = (
pytest.mark.filterwarnings(
"ignore:unclosed <socket.socket:ResourceWarning",
"ignore:unclosed transport <_ProactorSocketTransport closing:ResourceWarning",
)
if sys.version_info < (3, 10) and sys.platform == "win32"
else _identity
)
@_ignore_win32_resource_warnings
class TestTCPStream:
@pytest.fixture
def server_sock(self, family: AnyIPAddressFamily) -> Iterator[socket.socket]:
sock = socket.socket(family, socket.SOCK_STREAM)
sock.settimeout(1)
sock.bind(("localhost", 0))
sock.listen()
yield sock
sock.close()
@pytest.fixture
def server_addr(self, server_sock: socket.socket) -> tuple[str, int]:
return server_sock.getsockname()[:2]
async def test_extra_attributes(
self,
server_sock: socket.socket,
server_addr: tuple[str, int],
family: AnyIPAddressFamily,
) -> None:
async with await connect_tcp(*server_addr) as stream:
raw_socket = stream.extra(SocketAttribute.raw_socket)
assert stream.extra(SocketAttribute.family) == family
assert (
stream.extra(SocketAttribute.local_address)
== raw_socket.getsockname()[:2]
)
assert (
stream.extra(SocketAttribute.local_port) == raw_socket.getsockname()[1]
)
assert stream.extra(SocketAttribute.remote_address) == server_addr
assert stream.extra(SocketAttribute.remote_port) == server_addr[1]
async def test_send_receive(
self, server_sock: socket.socket, server_addr: tuple[str, int]
) -> None:
async with await connect_tcp(*server_addr) as stream:
client, _ = server_sock.accept()
await stream.send(b"blah")
request = client.recv(100)
client.sendall(request[::-1])
response = await stream.receive()
client.close()
assert response == b"halb"
async def test_send_large_buffer(
self, server_sock: socket.socket, server_addr: tuple[str, int]
) -> None:
def serve() -> None:
client, _ = server_sock.accept()
client.sendall(buffer)
client.close()
buffer = (
b"\xff" * 1024 * 1024
) # should exceed the maximum kernel send buffer size
async with await connect_tcp(*server_addr) as stream:
thread = Thread(target=serve, daemon=True)
thread.start()
response = b""
while len(response) < len(buffer):
chunk = await stream.receive()
assert isinstance(chunk, bytes)
response += chunk
thread.join()
assert response == buffer
async def test_send_eof(
self, server_sock: socket.socket, server_addr: tuple[str, int]
) -> None:
def serve() -> None:
client, _ = server_sock.accept()
request = b""
while True:
data = client.recv(100)
request += data
if not data:
break
client.sendall(request[::-1])
client.close()
async with await connect_tcp(*server_addr) as stream:
thread = Thread(target=serve, daemon=True)
thread.start()
await stream.send(b"hello, ")
await stream.send(b"world\n")
await stream.send_eof()
response = await stream.receive()
thread.join()
assert response == b"\ndlrow ,olleh"
async def test_iterate(
self, server_sock: socket.socket, server_addr: tuple[str, int]
) -> None:
def serve() -> None:
client, _ = server_sock.accept()
client.sendall(b"bl")
event.wait(1)
client.sendall(b"ah")
client.close()
event = threading.Event()
thread = Thread(target=serve, daemon=True)
thread.start()
chunks = []
async with await connect_tcp(*server_addr) as stream:
async for chunk in stream:
chunks.append(chunk)
event.set()
thread.join()
assert chunks == [b"bl", b"ah"]
async def test_socket_options(
self, family: AnyIPAddressFamily, server_addr: tuple[str, int]
) -> None:
async with await connect_tcp(*server_addr) as stream:
raw_socket = stream.extra(SocketAttribute.raw_socket)
assert raw_socket.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY) != 0
@skip_ipv6_mark
@pytest.mark.parametrize(
"local_addr, expected_client_addr",
[
pytest.param("", "::1", id="dualstack"),
pytest.param("127.0.0.1", "127.0.0.1", id="ipv4"),
pytest.param("::1", "::1", id="ipv6"),
],
)
async def test_happy_eyeballs(
self, local_addr: str, expected_client_addr: str, fake_localhost_dns: None
) -> None:
client_addr = None, None
def serve() -> None:
nonlocal client_addr
client, client_addr = server_sock.accept()
client.close()
family = (
AddressFamily.AF_INET
if local_addr == "127.0.0.1"
else AddressFamily.AF_INET6
)
server_sock = socket.socket(family)
server_sock.bind((local_addr, 0))
server_sock.listen()
port = server_sock.getsockname()[1]
thread = Thread(target=serve, daemon=True)
thread.start()
async with await connect_tcp("localhost", port):
pass
thread.join()
server_sock.close()
assert client_addr[0] == expected_client_addr
@pytest.mark.skipif(
sys.implementation.name == "pypy",
reason=(
"gc.get_referrers is broken on PyPy (see "
"https://github.com/pypy/pypy/issues/5075)"
),
)
async def test_happy_eyeballs_refcycles(
self, free_tcp_port: int, anyio_backend_name: str
) -> None:
"""
Test derived from https://github.com/python/cpython/pull/124859
"""
if anyio_backend_name == "asyncio" and sys.version_info < (3, 10):
pytest.skip(
"asyncio.BaseEventLoop.create_connection creates refcycles on py 3.9"
)
exc = None
try:
async with await connect_tcp("127.0.0.1", free_tcp_port):
pass
except OSError as e:
exc = e.__cause__
assert isinstance(exc, OSError)
assert gc.get_referrers(exc) == no_other_refs()
@pytest.mark.parametrize(
"target, exception_class",
[
pytest.param(
"localhost", ExceptionGroup, id="multi", marks=[skip_ipv6_mark]
),
pytest.param("127.0.0.1", ConnectionRefusedError, id="single"),
],
)
async def test_connection_refused(
self,
target: str,
exception_class: type[ExceptionGroup] | type[ConnectionRefusedError],
fake_localhost_dns: None,
free_tcp_port: int,
) -> None:
with pytest.raises(OSError) as exc:
await connect_tcp(target, free_tcp_port)
assert exc.match("All connection attempts failed")
assert isinstance(exc.value.__cause__, exception_class)
if isinstance(exc.value.__cause__, ExceptionGroup):
for exception in exc.value.__cause__.exceptions:
assert isinstance(exception, ConnectionRefusedError)
async def test_receive_timeout(
self, server_sock: socket.socket, server_addr: tuple[str, int]
) -> None:
def serve() -> None:
conn, _ = server_sock.accept()
time.sleep(1)
conn.close()
thread = Thread(target=serve, daemon=True)
thread.start()
async with await connect_tcp(*server_addr) as stream:
start_time = time.monotonic()
with move_on_after(0.1):
while time.monotonic() - start_time < 0.3:
await stream.receive(1)
pytest.fail("The timeout was not respected")
async def test_concurrent_send(self, server_addr: tuple[str, int]) -> None:
async def send_data() -> NoReturn:
while True:
await stream.send(b"\x00" * 4096)
async with await connect_tcp(*server_addr) as stream:
async with create_task_group() as tg:
tg.start_soon(send_data)
await wait_all_tasks_blocked()
with pytest.raises(BusyResourceError) as exc:
await stream.send(b"foo")
exc.match("already writing to")
tg.cancel_scope.cancel()
async def test_concurrent_receive(self, server_addr: tuple[str, int]) -> None:
async with await connect_tcp(*server_addr) as client:
async with create_task_group() as tg:
tg.start_soon(client.receive)
await wait_all_tasks_blocked()
try:
with pytest.raises(BusyResourceError) as exc:
await client.receive()
exc.match("already reading from")
finally:
tg.cancel_scope.cancel()
async def test_close_during_receive(self, server_addr: tuple[str, int]) -> None:
async def interrupt() -> None:
await wait_all_tasks_blocked()
await stream.aclose()
async with await connect_tcp(*server_addr) as stream:
async with create_task_group() as tg:
tg.start_soon(interrupt)
with pytest.raises(ClosedResourceError):
await stream.receive()
async def test_receive_after_close(self, server_addr: tuple[str, int]) -> None:
stream = await connect_tcp(*server_addr)
await stream.aclose()
with pytest.raises(ClosedResourceError):
await stream.receive()
async def test_send_after_close(self, server_addr: tuple[str, int]) -> None:
stream = await connect_tcp(*server_addr)
await stream.aclose()
with pytest.raises(ClosedResourceError):
await stream.send(b"foo")
async def test_receive_after_peer_closed(
self, family: AnyIPAddressFamily, request: FixtureRequest
) -> None:
server_sock = socket.create_server(("localhost", 0), family=family)
request.addfinalizer(server_sock.close)
server_sock.settimeout(1)
server_addr = server_sock.getsockname()[:2]
async with await connect_tcp(*server_addr) as stream:
client_sock, _ = server_sock.accept()
client_sock.close()
with pytest.raises(EndOfStream):
await stream.receive(1)
with pytest.raises(ClosedResourceError):
await stream.receive(1)
async def test_send_after_peer_closed(
self, family: AnyIPAddressFamily, request: FixtureRequest
) -> None:
server_sock = socket.create_server(("localhost", 0), family=family)
request.addfinalizer(server_sock.close)
server_sock.settimeout(1)
server_addr = server_sock.getsockname()[:2]
async with await connect_tcp(*server_addr) as stream:
client_sock, _ = server_sock.accept()
client_sock.close()
with pytest.raises(BrokenResourceError):
for _ in range(1000):
await stream.send(b"foo")
with pytest.raises(ClosedResourceError):
await stream.send(b"foo")
async def test_connect_tcp_with_tls(
self,
server_context: SSLContext,
client_context: SSLContext,
server_sock: socket.socket,
server_addr: tuple[str, int],
) -> None:
def serve() -> None:
with suppress(socket.timeout):
client, addr = server_sock.accept()
client.settimeout(1)
client = server_context.wrap_socket(client, server_side=True)
data = client.recv(100)
client.sendall(data[::-1])
client.unwrap()
client.close()
# The TLSStream tests are more comprehensive than this one!
thread = Thread(target=serve, daemon=True)
thread.start()
async with await connect_tcp(
*server_addr, tls_hostname="localhost", ssl_context=client_context
) as stream:
await stream.send(b"hello")
response = await stream.receive()
assert response == b"olleh"
thread.join()
async def test_connect_tcp_with_tls_cert_check_fail(
self,
server_context: SSLContext,
server_sock: socket.socket,
server_addr: tuple[str, int],
) -> None:
thread_exception = None
def serve() -> None:
nonlocal thread_exception
client, addr = server_sock.accept()
with client:
client.settimeout(1)
try:
server_context.wrap_socket(client, server_side=True)
except OSError:
pass
except BaseException as exc:
thread_exception = exc
thread = Thread(target=serve, daemon=True)
thread.start()
with pytest.raises(SSLError):
await connect_tcp(*server_addr, tls_hostname="localhost")
thread.join()
assert thread_exception is None
@pytest.mark.parametrize("anyio_backend", asyncio_params)
async def test_unretrieved_future_exception_server_crash(
self, family: AnyIPAddressFamily, caplog: LogCaptureFixture
) -> None:
"""
Test that there won't be any leftover Futures that don't get their exceptions
retrieved.
See https://github.com/encode/httpcore/issues/382 for details.
"""
def serve() -> None:
sock, addr = server_sock.accept()
event.wait(3)
sock.close()
del sock
gc.collect()
with socket.socket(family, socket.SOCK_STREAM) as server_sock:
server_sock.settimeout(1)
server_sock.bind(("localhost", 0))
server_sock.listen()
server_addr = server_sock.getsockname()[:2]
event = threading.Event()
thread = Thread(target=serve)
thread.start()
async with await connect_tcp(*server_addr) as stream:
await stream.send(b"GET")
event.set()
with pytest.raises(BrokenResourceError):
await stream.receive()
thread.join()
gc.collect()
caplog_text = "\n".join(
msg
for msg in caplog.messages
if not re.search("took [0-9.]+ seconds", msg)
)
assert not caplog_text
async def test_from_socket(
self, family: AnyIPAddressFamily, sock_or_fd_factory: SockFdFactoryProtocol
) -> None:
sock_or_fd = sock_or_fd_factory(family, socket.SOCK_STREAM, connected=True)
async with await SocketStream.from_socket(sock_or_fd) as stream:
assert isinstance(stream, SocketStream)
assert stream.extra(SocketAttribute.family) == family
async def test_from_socket_not_a_socket(self) -> None:
with pytest.raises(
TypeError,
match="expected an int or socket, got str instead",
):
await SocketStream.from_socket("foo") # type: ignore[arg-type]
async def test_from_socket_pass_file_fd(self, tmp_path: Path) -> None:
with pytest.raises(
ValueError,
match="the file descriptor does not refer to a socket",
):
with tmp_path.joinpath("foo").open("wb") as fd:
await SocketStream.from_socket(fd.fileno())
async def test_from_socket_wrong_socket_type(
self, sock_or_fd_factory: SockFdFactoryProtocol
) -> None:
sock_or_fd = sock_or_fd_factory(
socket.AF_INET, socket.SocketKind.SOCK_DGRAM, connected=True
)
with pytest.raises(
ValueError,
match="socket type mismatch: expected SOCK_STREAM, got SOCK_DGRAM",
):
await SocketStream.from_socket(sock_or_fd)
async def test_from_socket_not_connected(
self, sock_or_fd_factory: SockFdFactoryProtocol
) -> None:
sock_or_fd = sock_or_fd_factory(socket.AF_INET, socket.SOCK_STREAM, bound=True)
with pytest.raises(ValueError, match="the socket must be connected"):
await SocketStream.from_socket(sock_or_fd)
@pytest.mark.network
class TestTCPListener:
async def test_extra_attributes(self, family: AnyIPAddressFamily) -> None:
async with await create_tcp_listener(
local_host="localhost", family=family
) as multi:
assert multi.extra(SocketAttribute.family) == family
for listener in multi.listeners:
raw_socket = listener.extra(SocketAttribute.raw_socket)
assert listener.extra(SocketAttribute.family) == family
assert (
listener.extra(SocketAttribute.local_address)
== raw_socket.getsockname()[:2]
)
assert (
listener.extra(SocketAttribute.local_port)
== raw_socket.getsockname()[1]
)
pytest.raises(
TypedAttributeLookupError,
listener.extra,
SocketAttribute.remote_address,
)
pytest.raises(
TypedAttributeLookupError,
listener.extra,
SocketAttribute.remote_port,
)
@pytest.mark.parametrize(
"family",
[
pytest.param(AddressFamily.AF_INET, id="ipv4"),
pytest.param(AddressFamily.AF_INET6, id="ipv6", marks=[skip_ipv6_mark]),
pytest.param(socket.AF_UNSPEC, id="both", marks=[skip_ipv6_mark]),
],
)
async def test_accept(self, family: AnyIPAddressFamily) -> None:
async with await create_tcp_listener(
local_host="localhost", family=family
) as multi:
for listener in multi.listeners:
client = socket.socket(listener.extra(SocketAttribute.family))
client.settimeout(1)
addr = listener.extra(SocketAttribute.local_address)
host, port = addr[0], addr[1]
# On Windows, connecting to ANY (:: or 0.0.0.0) is invalid (WinError 10049).
# Replace it with loopback (::1 or 127.0.0.1) to make the test portable.
if sys.platform == "win32" and host in ("::", "0.0.0.0"):
family = listener.extra(SocketAttribute.family)
if family == socket.AF_INET6:
addr = ("::1", port)
elif family == socket.AF_INET:
addr = ("127.0.0.1", port)
client.connect(addr)
assert isinstance(listener, SocketListener)
stream = await listener.accept()
client.sendall(b"blah")
request = await stream.receive()
await stream.send(request[::-1])
assert client.recv(100) == b"halb"
client.close()
await stream.aclose()
async def test_accept_after_close(self, family: AnyIPAddressFamily) -> None:
async with await create_tcp_listener(
local_host="localhost", family=family
) as multi:
for listener in multi.listeners:
await listener.aclose()
assert isinstance(listener, SocketListener)
with pytest.raises(ClosedResourceError):
await listener.accept()
async def test_socket_options(self, family: AnyIPAddressFamily) -> None:
async with await create_tcp_listener(
local_host="localhost", family=family
) as multi:
for listener in multi.listeners:
raw_socket = listener.extra(SocketAttribute.raw_socket)
if sys.platform == "win32":
assert (
raw_socket.getsockopt(
socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE
)
!= 0
)
else:
assert (
raw_socket.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)
!= 0
)
raw_socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 80000)
assert raw_socket.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF) in (
80000,
160000,
)
client = socket.socket(raw_socket.family)
client.settimeout(1)
client.connect(raw_socket.getsockname())
assert isinstance(listener, SocketListener)
async with await listener.accept() as stream:
raw_socket = stream.extra(SocketAttribute.raw_socket)
assert raw_socket.gettimeout() == 0
assert raw_socket.family == listener.extra(SocketAttribute.family)
assert (
raw_socket.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY)
!= 0
)
client.close()
@pytest.mark.skipif(
not hasattr(socket, "SO_REUSEPORT"), reason="SO_REUSEPORT option not supported"
)
async def test_reuse_port(self, family: AnyIPAddressFamily) -> None:
multi1 = await create_tcp_listener(
local_host="localhost", family=family, reuse_port=True
)
assert len(multi1.listeners) == 1
multi2 = await create_tcp_listener(
local_host="localhost",
local_port=multi1.listeners[0].extra(SocketAttribute.local_port),
family=family,
reuse_port=True,
)
assert len(multi2.listeners) == 1
assert multi1.listeners[0].extra(
SocketAttribute.local_address
) == multi2.listeners[0].extra(SocketAttribute.local_address)
await multi1.aclose()
await multi2.aclose()
async def test_close_from_other_task(self, family: AnyIPAddressFamily) -> None:
listener = await create_tcp_listener(local_host="localhost", family=family)
with pytest.raises(ExceptionGroup) as exc:
async with create_task_group() as tg:
tg.start_soon(listener.serve, lambda stream: None)
await wait_all_tasks_blocked()
await listener.aclose()
tg.cancel_scope.cancel()
assert len(exc.value.exceptions) == 1
assert isinstance(exc.value.exceptions[0], ExceptionGroup)
nested_grp = exc.value.exceptions[0]
assert len(nested_grp.exceptions) == 1
assert isinstance(nested_grp.exceptions[0], ExceptionGroup)
async def test_send_after_eof(self, family: AnyIPAddressFamily) -> None:
async def handle(stream: SocketStream) -> None:
async with stream:
await stream.send(b"Hello\n")
multi = await create_tcp_listener(family=family, local_host="localhost")
async with multi, create_task_group() as tg:
tg.start_soon(multi.serve, handle)
await wait_all_tasks_blocked()
with socket.socket(family) as client:
client.connect(multi.extra(SocketAttribute.local_address))
client.shutdown(socket.SHUT_WR)
client.setblocking(False)
with fail_after(1):
while True:
try:
message = client.recv(10)
except BlockingIOError:
await checkpoint()
else:
assert message == b"Hello\n"
break
tg.cancel_scope.cancel()
async def test_eof_after_send(self, family: AnyIPAddressFamily) -> None:
"""Regression test for #701."""
received_bytes = b""
async def handle(stream: SocketStream) -> None:
nonlocal received_bytes
async with stream:
received_bytes = await stream.receive()
with pytest.raises(EndOfStream), fail_after(1):
await stream.receive()
tg.cancel_scope.cancel()
multi = await create_tcp_listener(family=family, local_host="localhost")
async with multi, create_task_group() as tg:
with socket.socket(family) as client:
client.connect(multi.extra(SocketAttribute.local_address))
client.send(b"Hello")
client.shutdown(socket.SHUT_WR)
await multi.serve(handle)
assert received_bytes == b"Hello"
@skip_ipv6_mark
@pytest.mark.skipif(
sys.platform == "win32",
reason="Windows does not support interface name suffixes",
)
async def test_bind_link_local(self) -> None:
# Regression test for #554
link_local_ipv6_address = next(
(
addr.address
for addresses in psutil.net_if_addrs().values()
for addr in addresses
if addr.address.startswith("fe80::") and "%" in addr.address
),
None,
)
if link_local_ipv6_address is None:
pytest.fail("Could not find a link-local IPv6 interface")
async with await create_tcp_listener(local_host=link_local_ipv6_address):
pass
async def test_from_socket(
self, family: AnyIPAddressFamily, sock_or_fd_factory: SockFdFactoryProtocol
) -> None:
sock_or_fd = sock_or_fd_factory(family, socket.SOCK_STREAM, bound=True)
async with await SocketListener.from_socket(sock_or_fd) as listener:
assert isinstance(listener, SocketListener)
assert listener.extra(SocketAttribute.family) == family
async def test_from_socket_not_bound(
self, family: AnyIPAddressFamily, sock_or_fd_factory: SockFdFactoryProtocol
) -> None:
sock_or_fd = sock_or_fd_factory(family, socket.SOCK_STREAM)
with pytest.raises(ValueError, match="the socket must be bound"):
await SocketListener.from_socket(sock_or_fd)
@pytest.mark.parametrize(
"local_host,family,expected_listeners,local_port",
[
(None, AddressFamily.AF_UNSPEC, 1 if socket.has_dualstack_ipv6() else 2, 0),
("localhost", AddressFamily.AF_UNSPEC, 2, 0),
("localhost", AddressFamily.AF_INET, 1, 0),
("::", AddressFamily.AF_UNSPEC, 1, 0),
("127.0.0.1", AddressFamily.AF_INET, 1, 0),
("::1", AddressFamily.AF_INET6, 1, 0),
(
None,
AddressFamily.AF_UNSPEC,
1 if socket.has_dualstack_ipv6() else 2,
54321,
),
("localhost", AddressFamily.AF_UNSPEC, 2, 54321),
("localhost", AddressFamily.AF_INET, 1, 54321),
],
)
async def test_tcp_listener_same_port(
self,
local_host: str | None,
family: AnyIPAddressFamily,
expected_listeners: int,
local_port: int,
) -> None:
async with await create_tcp_listener(
local_host=local_host, family=family, local_port=local_port
) as multi:
ports = {
listener.extra(SocketAttribute.local_port)
for listener in multi.listeners
}
assert len(ports) == 1
if local_port != 0:
assert ports == {local_port}
assert len(multi.listeners) == expected_listeners
@skip_ipv6_mark
async def test_tcp_listener_dualstack_disabled(
self, monkeypatch: MonkeyPatch
) -> None:
monkeypatch.setattr(socket, "has_dualstack_ipv6", lambda: False)
async with await create_tcp_listener(
local_host=None, family=AddressFamily.AF_UNSPEC
) as multi:
families = {
listener.extra(SocketAttribute.family) for listener in multi.listeners
}
assert families == {socket.AF_INET, socket.AF_INET6}
assert len(multi.listeners) == 2
ports = {
listener.extra(SocketAttribute.local_port)
for listener in multi.listeners
}
assert len(ports) == 1
async def test_tcp_listener_retry_after_partial_failure(self) -> None:
"""
Simulate a case where the first bind() succeeds with an ephemeral port,
the second bind() fails with EADDRINUSE, and verify that create_tcp_listener
retries and eventually succeeds with all listeners bound to the same port.
"""
real_bind = socket.socket.bind
bind_count = 0
fail_once = True
def fake_bind(
self: socket.socket, addr: tuple[str, int] | tuple[str, int, int, int]
) -> None:
nonlocal bind_count, fail_once
port = addr[1] if isinstance(addr, tuple) and len(addr) >= 2 else None
bind_count += 1
if bind_count == 1 and port == 0:
return real_bind(self, addr)
if fail_once and port != 0:
fail_once = False
raise OSError(errno.EADDRINUSE, "simulated collision on second bind")
return real_bind(self, addr)
with patch.object(socket.socket, "bind", new=fake_bind):
async with await create_tcp_listener(
local_host="localhost", family=socket.AF_UNSPEC, local_port=0
) as multi:
assert bind_count >= 2
ports = {
listener.extra(SocketAttribute.local_port)
for listener in multi.listeners
}
assert len(ports) == 1
assert all(
isinstance(listener, SocketListener) for listener in multi.listeners
)
async def test_tcp_listener_total_bind_failure(self) -> None:
"""
Test for a situation where bind() always fails when other listeners are being
bound to the same port as the first listener which was randomly assigned a free
port by the kernel.
"""
def raise_oserror(addr: tuple[str, int]) -> None:
# Pretend that every explicitly requested port is already in use
if addr[1] != 0:
raise OSError(errno.EADDRINUSE, "bind failure")
mock_socket_instance = MagicMock()
mock_socket_instance.bind.side_effect = raise_oserror
asynclib = get_async_backend()
with (
patch("anyio._core._sockets.socket") as mock_anyio_sockets,
patch.object(
asynclib, "create_tcp_listener", return_value=MagicMock(SocketListener)
),
pytest.raises(
OSError, match="Could not create 2 listeners with a consistent port"
),
):
mock_anyio_sockets.socket.configure_mock(return_value=mock_socket_instance)
await create_tcp_listener(local_host="localhost")
@pytest.mark.skipif(
sys.platform == "win32", reason="UNIX sockets are not available on Windows"
)
class TestUNIXStream:
@pytest.fixture(
params=[
"path",
pytest.param("abstract", marks=[skip_unix_abstract_mark]),
]
)
def socket_path(self, request: SubRequest) -> Generator[Path, None, None]:
# Use stdlib tempdir generation
# Fixes `OSError: AF_UNIX path too long` from pytest generated temp_path
with tempfile.TemporaryDirectory() as path:
if request.param == "path":
yield Path(path) / "socket"
else:
yield Path(f"\0{path}") / "socket"
@pytest.fixture(params=[False, True], ids=["str", "path"])
def socket_path_or_str(self, request: SubRequest, socket_path: Path) -> Path | str:
return socket_path if request.param else str(socket_path)
@pytest.fixture
def server_sock(self, socket_path: Path) -> Iterable[socket.socket]:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.settimeout(1)
sock.bind(str(socket_path))
sock.listen()
yield sock
sock.close()
async def test_extra_attributes(
self, server_sock: socket.socket, socket_path: Path
) -> None:
async with await connect_unix(socket_path) as stream:
raw_socket = stream.extra(SocketAttribute.raw_socket)
assert stream.extra(SocketAttribute.family) == socket.AF_UNIX
assert (
stream.extra(SocketAttribute.local_address) == raw_socket.getsockname()
)
remote_addr = stream.extra(SocketAttribute.remote_address)
if isinstance(remote_addr, str):
assert stream.extra(SocketAttribute.remote_address) == str(socket_path)
else:
assert isinstance(remote_addr, bytes)
assert stream.extra(SocketAttribute.remote_address) == bytes(
socket_path
)
pytest.raises(
TypedAttributeLookupError, stream.extra, SocketAttribute.local_port
)
pytest.raises(
TypedAttributeLookupError, stream.extra, SocketAttribute.remote_port
)
async def test_send_receive(
self, server_sock: socket.socket, socket_path_or_str: Path | str
) -> None:
async with await connect_unix(socket_path_or_str) as stream:
client, _ = server_sock.accept()
await stream.send(b"blah")
request = client.recv(100)
client.sendall(request[::-1])
response = await stream.receive()
client.close()
assert response == b"halb"
async def test_receive_large_buffer(
self, server_sock: socket.socket, socket_path: Path
) -> None:
def serve() -> None:
client, _ = server_sock.accept()
client.sendall(buffer)
client.close()
buffer = (
b"\xff" * 1024 * 512 + b"\x00" * 1024 * 512
) # should exceed the maximum kernel send buffer size
async with await connect_unix(socket_path) as stream:
thread = Thread(target=serve, daemon=True)
thread.start()
response = b""
while len(response) < len(buffer):
response += await stream.receive()
thread.join()
assert response == buffer
async def test_send_large_buffer(
self, server_sock: socket.socket, socket_path: Path
) -> None:
response = b""
def serve() -> None:
nonlocal response
client, _ = server_sock.accept()
while True:
data = client.recv(1024)
if not data:
break
response += data
client.close()
buffer = (
b"\xff" * 1024 * 512 + b"\x00" * 1024 * 512
) # should exceed the maximum kernel send buffer size
async with await connect_unix(socket_path) as stream:
thread = Thread(target=serve, daemon=True)
thread.start()
await stream.send(buffer)
thread.join()
assert response == buffer
async def test_receive_fds(
self, server_sock: socket.socket, socket_path: Path, tmp_path: Path
) -> None:
def serve() -> None:
path1 = tmp_path / "file1"
path2 = tmp_path / "file2"
path1.write_text("Hello, ")
path2.write_text("World!")
with path1.open() as file1, path2.open() as file2:
fdarray = array.array("i", [file1.fileno(), file2.fileno()])
client, _ = server_sock.accept()
cmsg = (socket.SOL_SOCKET, socket.SCM_RIGHTS, fdarray)
with client:
client.sendmsg([b"test"], [cmsg])
async with await connect_unix(socket_path) as stream:
thread = Thread(target=serve, daemon=True)
thread.start()
message, fds = await stream.receive_fds(10, 2)
thread.join()
text = ""
for fd in fds:
with os.fdopen(fd) as file:
text += file.read()
assert message == b"test"
assert text == "Hello, World!"
async def test_receive_fds_bad_args(
self, server_sock: socket.socket, socket_path: Path
) -> None:
async with await connect_unix(socket_path) as stream:
for msglen in (-1, "foo"):
with pytest.raises(
ValueError, match="msglen must be a non-negative integer"
):
await stream.receive_fds(msglen, 0) # type: ignore[arg-type]
for maxfds in (0, "foo"):
with pytest.raises(
ValueError, match="maxfds must be a positive integer"
):
await stream.receive_fds(0, maxfds) # type: ignore[arg-type]
async def test_send_fds(
self, server_sock: socket.socket, socket_path: Path, tmp_path: Path
) -> None:
def serve() -> None:
fds = array.array("i")
client, _ = server_sock.accept()
msg, ancdata, *_ = client.recvmsg(10, socket.CMSG_LEN(2 * fds.itemsize))
client.close()
assert msg == b"test"
for cmsg_level, cmsg_type, cmsg_data in ancdata:
assert cmsg_level == socket.SOL_SOCKET
assert cmsg_type == socket.SCM_RIGHTS
fds.frombytes(
cmsg_data[: len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]
)
text = ""
for fd in fds:
with os.fdopen(fd) as file:
text += file.read()
assert text == "Hello, World!"
path1 = tmp_path / "file1"
path2 = tmp_path / "file2"
path1.write_text("Hello, ")
path2.write_text("World!")
with path1.open() as file1, path2.open() as file2, fail_after(2):
assert isinstance(file1, io.TextIOWrapper)
assert isinstance(file2, io.TextIOWrapper)
async with await connect_unix(socket_path) as stream:
thread = Thread(target=serve, daemon=True)
thread.start()
await stream.send_fds(b"test", [file1, file2])
thread.join()
async def test_send_eof(
self, server_sock: socket.socket, socket_path: Path
) -> None:
def serve() -> None:
client, _ = server_sock.accept()
request = b""
while True:
data = client.recv(100)
request += data
if not data:
break
client.sendall(request[::-1])
client.close()
async with await connect_unix(socket_path) as stream:
thread = Thread(target=serve, daemon=True)
thread.start()
await stream.send(b"hello, ")
await stream.send(b"world\n")
await stream.send_eof()
response = await stream.receive()
thread.join()
assert response == b"\ndlrow ,olleh"
async def test_iterate(self, server_sock: socket.socket, socket_path: Path) -> None:
def serve() -> None:
client, _ = server_sock.accept()
client.sendall(b"bl")
time.sleep(0.05)
client.sendall(b"ah")
client.close()
thread = Thread(target=serve, daemon=True)
thread.start()
async with await connect_unix(socket_path) as stream:
chunks = [chunk async for chunk in stream]
thread.join()
assert chunks == [b"bl", b"ah"]
async def test_send_fds_bad_args(
self, server_sock: socket.socket, socket_path: Path
) -> None:
async with await connect_unix(socket_path) as stream:
with pytest.raises(ValueError, match="message must not be empty"):
await stream.send_fds(b"", [0])
with pytest.raises(ValueError, match="fds must not be empty"):
await stream.send_fds(b"test", [])
async def test_concurrent_send(
self, server_sock: socket.socket, socket_path: Path
) -> None:
async def send_data() -> NoReturn:
while True:
await client.send(b"\x00" * 4096)
async with await connect_unix(socket_path) as client:
async with create_task_group() as tg:
tg.start_soon(send_data)
await wait_all_tasks_blocked()
with pytest.raises(BusyResourceError) as exc:
await client.send(b"foo")
exc.match("already writing to")
tg.cancel_scope.cancel()
async def test_concurrent_receive(
self, server_sock: socket.socket, socket_path: Path
) -> None:
async with await connect_unix(socket_path) as client:
async with create_task_group() as tg:
tg.start_soon(client.receive)
await wait_all_tasks_blocked()
try:
with pytest.raises(BusyResourceError) as exc:
await client.receive()
exc.match("already reading from")
finally:
tg.cancel_scope.cancel()
async def test_close_during_receive(
self, server_sock: socket.socket, socket_path: Path
) -> None:
async def interrupt() -> None:
await wait_all_tasks_blocked()
await stream.aclose()
async with await connect_unix(socket_path) as stream:
async with create_task_group() as tg:
tg.start_soon(interrupt)
with pytest.raises(ClosedResourceError):
await stream.receive()
async def test_receive_after_close(
self, server_sock: socket.socket, socket_path: Path
) -> None:
stream = await connect_unix(socket_path)
await stream.aclose()
with pytest.raises(ClosedResourceError):
await stream.receive()
async def test_send_after_close(
self, server_sock: socket.socket, socket_path: Path
) -> None:
stream = await connect_unix(socket_path)
await stream.aclose()
with pytest.raises(ClosedResourceError):
await stream.send(b"foo")
async def test_cannot_connect(self, socket_path: Path) -> None:
if str(socket_path).startswith("\0"):
with pytest.raises(ConnectionRefusedError):
await connect_unix(socket_path)
else:
with pytest.raises(FileNotFoundError):
await connect_unix(socket_path)
async def test_connecting_using_bytes(
self, server_sock: socket.socket, socket_path: Path
) -> None:
async with await connect_unix(str(socket_path).encode()):
pass
@pytest.mark.skipif(
platform.system() == "Darwin", reason="macOS requires valid UTF-8 paths"
)
async def test_connecting_with_non_utf8(self, socket_path: Path) -> None:
actual_path = str(socket_path).encode() + b"\xf0"
with socket.socket(socket.AF_UNIX) as server:
server.bind(actual_path)
server.listen(1)
async with await connect_unix(actual_path):
pass
async def test_from_socket(
self, socket_path: Path, sock_or_fd_factory: SockFdFactoryProtocol
) -> None:
sock_or_fd = sock_or_fd_factory(
socket.AF_UNIX, socket.SOCK_STREAM, connected=True
)
async with await UNIXSocketStream.from_socket(sock_or_fd) as stream:
assert isinstance(stream, UNIXSocketStream)
async def test_from_socket_wrong_socket_type(
self, sock_or_fd_factory: SockFdFactoryProtocol
) -> None:
sock_or_fd = sock_or_fd_factory(
socket.AF_UNIX, socket.SOCK_DGRAM, connected=True
)
with pytest.raises(
ValueError,
match="socket type mismatch: expected SOCK_STREAM, got SOCK_DGRAM",
):
await UNIXSocketStream.from_socket(sock_or_fd)
async def test_from_socket_wrong_address_family(
self, sock_or_fd_factory: SockFdFactoryProtocol
) -> None:
sock_or_fd = sock_or_fd_factory(
socket.AF_INET, socket.SOCK_STREAM, connected=True
)
with pytest.raises(
ValueError,
match="address family mismatch: expected AF_UNIX, got AF_INET",
):
await UNIXSocketStream.from_socket(sock_or_fd)
async def test_from_socket_not_connected(
self, sock_or_fd_factory: SockFdFactoryProtocol
) -> None:
sock_or_fd = sock_or_fd_factory(socket.AF_UNIX, socket.SOCK_STREAM)
with pytest.raises(ValueError, match="the socket must be connected"):
await UNIXSocketStream.from_socket(sock_or_fd)
@pytest.mark.skipif(
sys.platform == "win32", reason="UNIX sockets are not available on Windows"
)
class TestUNIXListener:
@pytest.fixture(
params=[
"path",
pytest.param("abstract", marks=[skip_unix_abstract_mark]),
]
)
def socket_path(self, request: SubRequest) -> Generator[Path, None, None]:
# Use stdlib tempdir generation
# Fixes `OSError: AF_UNIX path too long` from pytest generated temp_path
with tempfile.TemporaryDirectory() as path:
if request.param == "path":
yield Path(path) / "socket"
else:
yield Path(f"\0{path}") / "socket"
@pytest.fixture(params=[False, True], ids=["str", "path"])
def socket_path_or_str(self, request: SubRequest, socket_path: Path) -> Path | str:
return socket_path if request.param else str(socket_path)
async def test_extra_attributes(self, socket_path: Path) -> None:
async with await create_unix_listener(socket_path) as listener:
raw_socket = listener.extra(SocketAttribute.raw_socket)
assert listener.extra(SocketAttribute.family) == socket.AF_UNIX
assert (
listener.extra(SocketAttribute.local_address)
== raw_socket.getsockname()
)
pytest.raises(
TypedAttributeLookupError, listener.extra, SocketAttribute.local_port
)
pytest.raises(
TypedAttributeLookupError,
listener.extra,
SocketAttribute.remote_address,
)
pytest.raises(
TypedAttributeLookupError, listener.extra, SocketAttribute.remote_port
)
async def test_accept(self, socket_path_or_str: Path | str) -> None:
async with await create_unix_listener(socket_path_or_str) as listener:
client = socket.socket(socket.AF_UNIX)
client.settimeout(1)
client.connect(str(socket_path_or_str))
stream = await listener.accept()
client.sendall(b"blah")
request = await stream.receive()
await stream.send(request[::-1])
assert client.recv(100) == b"halb"
client.close()
await stream.aclose()
async def test_socket_options(self, socket_path: Path) -> None:
async with await create_unix_listener(socket_path) as listener:
listener_socket = listener.extra(SocketAttribute.raw_socket)
assert listener_socket.family == socket.AF_UNIX
listener_socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 80000)
assert listener_socket.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF) in (
80000,
160000,
)
client = socket.socket(listener_socket.family)
client.settimeout(1)
client.connect(listener_socket.getsockname())
async with await listener.accept() as stream:
assert stream.extra(SocketAttribute.raw_socket).gettimeout() == 0
assert stream.extra(SocketAttribute.family) == listener_socket.family
client.close()
async def test_send_after_eof(self, socket_path: Path) -> None:
async def handle(stream: SocketStream) -> None:
async with stream:
await stream.send(b"Hello\n")
async with (
await create_unix_listener(socket_path) as listener,
create_task_group() as tg,
):
tg.start_soon(listener.serve, handle)
await wait_all_tasks_blocked()
with socket.socket(socket.AF_UNIX) as client:
client.connect(str(socket_path))
client.shutdown(socket.SHUT_WR)
client.setblocking(False)
with fail_after(1):
while True:
try:
message = client.recv(10)
except BlockingIOError:
await checkpoint()
else:
assert message == b"Hello\n"
break
tg.cancel_scope.cancel()
async def test_bind_twice(self, socket_path: Path) -> None:
"""Test that the previous socket is removed before binding to the path."""
for _ in range(2):
async with await create_unix_listener(socket_path):
pass
async def test_listening_bytes_path(self, socket_path: Path) -> None:
async with await create_unix_listener(str(socket_path).encode()):
pass
@pytest.mark.skipif(
platform.system() == "Darwin", reason="macOS requires valid UTF-8 paths"
)
async def test_listening_invalid_ascii(self, socket_path: Path) -> None:
real_path = str(socket_path).encode() + b"\xf0"
async with await create_unix_listener(real_path):
pass
async def test_from_socket(self, sock_or_fd_factory: SockFdFactoryProtocol) -> None:
sock_or_fd = sock_or_fd_factory(socket.AF_UNIX, socket.SOCK_STREAM, bound=True)
async with await SocketListener.from_socket(sock_or_fd) as listener:
assert isinstance(listener, SocketListener)
assert listener.extra(SocketAttribute.family) == socket.AF_UNIX
async def test_multi_listener(tmp_path_factory: TempPathFactory) -> None:
async def handle(stream: SocketStream) -> None:
client_addresses.append(stream.extra(SocketAttribute.remote_address))
event.set()
await stream.aclose()
client_addresses: list[str | IPSockAddrType] = []
listeners: list[Listener] = [await create_tcp_listener(local_host="localhost")]
with tempfile.TemporaryDirectory() as path:
if sys.platform != "win32":
listeners.append(await create_unix_listener(Path(path) / "socket"))
expected_addresses: list[str | IPSockAddrType] = []
async with MultiListener(listeners) as multi_listener:
async with create_task_group() as tg:
tg.start_soon(multi_listener.serve, handle)
for listener in multi_listener.listeners:
event = Event()
local_address = listener.extra(SocketAttribute.local_address)
if (
sys.platform != "win32"
and listener.extra(SocketAttribute.family) == socket.AF_UNIX
):
assert isinstance(local_address, str)
stream: SocketStream = await connect_unix(local_address)
else:
assert isinstance(local_address, tuple)
host, port = local_address
host = (
"::1"
if listener.extra(SocketAttribute.family) == socket.AF_INET6
else "127.0.0.1"
)
stream = await connect_tcp(host, port)
expected_addresses.append(
stream.extra(SocketAttribute.local_address)
)
await event.wait()
await stream.aclose()
tg.cancel_scope.cancel()
assert client_addresses == expected_addresses
@pytest.mark.network
@pytest.mark.usefixtures("check_asyncio_bug")
class TestUDPSocket:
async def test_extra_attributes(self, family: AnyIPAddressFamily) -> None:
async with await create_udp_socket(
family=family, local_host="localhost"
) as udp:
raw_socket = udp.extra(SocketAttribute.raw_socket)
assert raw_socket.gettimeout() == 0
assert udp.extra(SocketAttribute.family) == family
assert (
udp.extra(SocketAttribute.local_address) == raw_socket.getsockname()[:2]
)
assert udp.extra(SocketAttribute.local_port) == raw_socket.getsockname()[1]
pytest.raises(
TypedAttributeLookupError, udp.extra, SocketAttribute.remote_address
)
pytest.raises(
TypedAttributeLookupError, udp.extra, SocketAttribute.remote_port
)
async def test_send_receive(self, family: AnyIPAddressFamily) -> None:
async with await create_udp_socket(
local_host="localhost", family=family
) as sock:
host, port = sock.extra(SocketAttribute.local_address) # type: ignore[misc]
await sock.sendto(b"blah", host, port)
request, addr = await sock.receive()
assert request == b"blah"
assert addr == sock.extra(SocketAttribute.local_address)
await sock.sendto(b"halb", host, port)
response, addr = await sock.receive()
assert response == b"halb"
assert addr == (host, port)
async def test_iterate(self, family: AnyIPAddressFamily) -> None:
async def serve() -> None:
async for packet, addr in server:
await server.send((packet[::-1], addr))
async with await create_udp_socket(
family=family, local_host="localhost"
) as server:
host, port = server.extra( # type: ignore[misc]
SocketAttribute.local_address
)
async with await create_udp_socket(
family=family, local_host="localhost"
) as client:
async with create_task_group() as tg:
tg.start_soon(serve)
await client.sendto(b"FOOBAR", host, port)
assert await client.receive() == (b"RABOOF", (host, port))
await client.sendto(b"123456", host, port)
assert await client.receive() == (b"654321", (host, port))
tg.cancel_scope.cancel()
@pytest.mark.skipif(
not hasattr(socket, "SO_REUSEPORT"), reason="SO_REUSEPORT option not supported"
)
async def test_reuse_port(self, family: AnyIPAddressFamily) -> None:
async with await create_udp_socket(
family=family, local_host="localhost", reuse_port=True
) as udp:
port = udp.extra(SocketAttribute.local_port)
assert port != 0
async with await create_udp_socket(
family=family, local_host="localhost", local_port=port, reuse_port=True
) as udp2:
assert port == udp2.extra(SocketAttribute.local_port)
async def test_concurrent_receive(self) -> None:
async with await create_udp_socket(
family=AddressFamily.AF_INET, local_host="localhost"
) as udp:
async with create_task_group() as tg:
tg.start_soon(udp.receive)
await wait_all_tasks_blocked()
try:
with pytest.raises(BusyResourceError) as exc:
await udp.receive()
exc.match("already reading from")
finally:
tg.cancel_scope.cancel()
async def test_close_during_receive(self) -> None:
async def close_when_blocked() -> None:
await wait_all_tasks_blocked()
await udp.aclose()
async with await create_udp_socket(
family=AddressFamily.AF_INET, local_host="localhost"
) as udp:
async with create_task_group() as tg:
tg.start_soon(close_when_blocked)
with pytest.raises(ClosedResourceError):
await udp.receive()
async def test_receive_after_close(self) -> None:
udp = await create_udp_socket(
family=AddressFamily.AF_INET, local_host="localhost"
)
await udp.aclose()
with pytest.raises(ClosedResourceError):
await udp.receive()
async def test_send_after_close(self) -> None:
udp = await create_udp_socket(
family=AddressFamily.AF_INET, local_host="localhost"
)
host, port = udp.extra(SocketAttribute.local_address) # type: ignore[misc]
await udp.aclose()
with pytest.raises(ClosedResourceError):
await udp.sendto(b"foo", host, port)
async def test_create_unbound_socket(self, family: AnyIPAddressFamily) -> None:
"""Regression test for #360."""
async with await create_udp_socket(family=family) as udp:
local_address = cast(
IPSockAddrType, udp.extra(SocketAttribute.local_address)
)
assert local_address[1] > 0
async def test_from_socket(
self, family: AnyIPAddressFamily, sock_or_fd_factory: SockFdFactoryProtocol
) -> None:
sock_or_fd = sock_or_fd_factory(family, socket.SOCK_DGRAM, bound=True)
async with await UDPSocket.from_socket(sock_or_fd) as udp_socket:
assert isinstance(udp_socket, UDPSocket)
assert udp_socket.extra(SocketAttribute.family) == family
async def test_from_socket_wrong_socket_type(
self, sock_or_fd_factory: SockFdFactoryProtocol
) -> None:
sock_or_fd = sock_or_fd_factory(
socket.AF_INET, socket.SOCK_STREAM, connected=True
)
with pytest.raises(
ValueError,
match="socket type mismatch: expected SOCK_DGRAM, got SOCK_STREAM",
):
await UDPSocket.from_socket(sock_or_fd)
@pytest.mark.network
@pytest.mark.usefixtures("check_asyncio_bug")
class TestConnectedUDPSocket:
async def test_extra_attributes(self, family: AnyIPAddressFamily) -> None:
async with await create_connected_udp_socket(
"localhost", 5000, family=family
) as udp:
raw_socket = udp.extra(SocketAttribute.raw_socket)
assert udp.extra(SocketAttribute.family) == family
assert (
udp.extra(SocketAttribute.local_address) == raw_socket.getsockname()[:2]
)
assert udp.extra(SocketAttribute.local_port) == raw_socket.getsockname()[1]
assert (
udp.extra(SocketAttribute.remote_address)
== raw_socket.getpeername()[:2]
)
assert udp.extra(SocketAttribute.remote_port) == 5000
async def test_send_receive(self, family: AnyIPAddressFamily) -> None:
async with await create_udp_socket(
family=family, local_host="localhost"
) as udp1:
host, port = udp1.extra(SocketAttribute.local_address) # type: ignore[misc]
async with await create_connected_udp_socket(
host, port, local_host="localhost", family=family
) as udp2:
host, port = udp2.extra(
SocketAttribute.local_address # type: ignore[misc]
)
await udp2.send(b"blah")
request = await udp1.receive()
assert request == (b"blah", (host, port))
await udp1.sendto(b"halb", host, port)
response = await udp2.receive()
assert response == b"halb"
async def test_iterate(self, family: AnyIPAddressFamily) -> None:
async def serve() -> None:
async for packet in udp2:
await udp2.send(packet[::-1])
async with await create_udp_socket(
family=family, local_host="localhost"
) as udp1:
host, port = udp1.extra(SocketAttribute.local_address) # type: ignore[misc]
async with await create_connected_udp_socket(host, port) as udp2:
host, port = udp2.extra( # type: ignore[misc]
SocketAttribute.local_address
)
async with create_task_group() as tg:
tg.start_soon(serve)
await udp1.sendto(b"FOOBAR", host, port)
assert await udp1.receive() == (b"RABOOF", (host, port))
await udp1.sendto(b"123456", host, port)
assert await udp1.receive() == (b"654321", (host, port))
tg.cancel_scope.cancel()
@pytest.mark.skipif(
not hasattr(socket, "SO_REUSEPORT"), reason="SO_REUSEPORT option not supported"
)
async def test_reuse_port(self, family: AnyIPAddressFamily) -> None:
async with await create_connected_udp_socket(
"localhost", 6000, family=family, local_host="localhost", reuse_port=True
) as udp:
port = udp.extra(SocketAttribute.local_port)
assert port != 0
async with await create_connected_udp_socket(
"localhost",
6001,
family=family,
local_host="localhost",
local_port=port,
reuse_port=True,
) as udp2:
assert port == udp2.extra(SocketAttribute.local_port)
async def test_concurrent_receive(self) -> None:
async with await create_connected_udp_socket(
"localhost", 5000, local_host="localhost", family=AddressFamily.AF_INET
) as udp:
async with create_task_group() as tg:
tg.start_soon(udp.receive)
await wait_all_tasks_blocked()
try:
with pytest.raises(BusyResourceError) as exc:
await udp.receive()
exc.match("already reading from")
finally:
tg.cancel_scope.cancel()
async def test_close_during_receive(self) -> None:
async def close_when_blocked() -> None:
await wait_all_tasks_blocked()
await udp.aclose()
async with await create_connected_udp_socket(
"localhost", 5000, local_host="localhost", family=AddressFamily.AF_INET
) as udp:
async with create_task_group() as tg:
tg.start_soon(close_when_blocked)
with pytest.raises(ClosedResourceError):
await udp.receive()
async def test_receive_after_close(self, family: AnyIPAddressFamily) -> None:
udp = await create_connected_udp_socket(
"localhost", 5000, local_host="localhost", family=family
)
await udp.aclose()
with pytest.raises(ClosedResourceError):
await udp.receive()
async def test_send_after_close(self, family: AnyIPAddressFamily) -> None:
udp = await create_connected_udp_socket(
"localhost", 5000, local_host="localhost", family=family
)
await udp.aclose()
with pytest.raises(ClosedResourceError):
await udp.send(b"foo")
async def test_from_socket(
self, family: AnyIPAddressFamily, sock_or_fd_factory: SockFdFactoryProtocol
) -> None:
sock_or_fd = sock_or_fd_factory(family, socket.SOCK_DGRAM, connected=True)
async with await ConnectedUDPSocket.from_socket(sock_or_fd) as udp_socket:
assert isinstance(udp_socket, ConnectedUDPSocket)
assert udp_socket.extra(SocketAttribute.family) == family
async def test_from_socket_wrong_socket_type(
self, sock_or_fd_factory: SockFdFactoryProtocol
) -> None:
sock_or_fd = sock_or_fd_factory(
socket.AF_INET, socket.SOCK_STREAM, connected=True
)
with pytest.raises(
ValueError,
match="socket type mismatch: expected SOCK_DGRAM, got SOCK_STREAM",
):
await ConnectedUDPSocket.from_socket(sock_or_fd)
async def test_from_socket_not_connected(
self, sock_or_fd_factory: SockFdFactoryProtocol
) -> None:
sock_or_fd = sock_or_fd_factory(socket.AF_INET, socket.SOCK_DGRAM, bound=True)
with pytest.raises(ValueError, match="the socket must be connected"):
await ConnectedUDPSocket.from_socket(sock_or_fd)
@pytest.mark.skipif(
sys.platform == "win32", reason="UNIX sockets are not available on Windows"
)
class TestUNIXDatagramSocket:
@pytest.fixture(
params=[
"path",
pytest.param("abstract", marks=[skip_unix_abstract_mark]),
]
)
def socket_path(self, request: SubRequest) -> Generator[Path, None, None]:
# Use stdlib tempdir generation
# Fixes `OSError: AF_UNIX path too long` from pytest generated temp_path
with tempfile.TemporaryDirectory() as path:
if request.param == "path":
yield Path(path) / "socket"
else:
yield Path(f"\0{path}") / "socket"
@pytest.fixture(params=[False, True], ids=["str", "path"])
def socket_path_or_str(self, request: SubRequest, socket_path: Path) -> Path | str:
return socket_path if request.param else str(socket_path)
@pytest.fixture
def peer_socket_path(self) -> Generator[Path, None, None]:
# Use stdlib tempdir generation
# Fixes `OSError: AF_UNIX path too long` from pytest generated temp_path
with tempfile.TemporaryDirectory() as path:
yield Path(path) / "peer_socket"
async def test_extra_attributes(self, socket_path: Path) -> None:
async with await create_unix_datagram_socket(local_path=socket_path) as unix_dg:
raw_socket = unix_dg.extra(SocketAttribute.raw_socket)
assert raw_socket.gettimeout() == 0
assert unix_dg.extra(SocketAttribute.family) == socket.AF_UNIX
assert (
unix_dg.extra(SocketAttribute.local_address) == raw_socket.getsockname()
)
pytest.raises(
TypedAttributeLookupError, unix_dg.extra, SocketAttribute.local_port
)
pytest.raises(
TypedAttributeLookupError, unix_dg.extra, SocketAttribute.remote_address
)
pytest.raises(
TypedAttributeLookupError, unix_dg.extra, SocketAttribute.remote_port
)
async def test_send_receive(self, socket_path_or_str: Path | str) -> None:
async with await create_unix_datagram_socket(
local_path=socket_path_or_str,
) as sock:
path = str(socket_path_or_str)
await sock.sendto(b"blah", path)
request, addr = await sock.receive()
assert request == b"blah"
if isinstance(addr, bytes):
assert addr == path.encode()
else:
assert addr == path
await sock.sendto(b"halb", path)
response, addr = await sock.receive()
assert response == b"halb"
if isinstance(addr, bytes):
assert addr == path.encode()
else:
assert addr == path
async def test_iterate(self, peer_socket_path: Path, socket_path: Path) -> None:
async def serve() -> None:
async for packet, addr in server:
await server.send((packet[::-1], addr))
async with await create_unix_datagram_socket(
local_path=peer_socket_path,
) as server:
peer_path = str(peer_socket_path)
async with await create_unix_datagram_socket(
local_path=socket_path
) as client:
async with create_task_group() as tg:
tg.start_soon(serve)
await client.sendto(b"FOOBAR", peer_path)
assert await client.receive() == (b"RABOOF", peer_path)
await client.sendto(b"123456", peer_path)
assert await client.receive() == (b"654321", peer_path)
tg.cancel_scope.cancel()
async def test_concurrent_receive(self) -> None:
async with await create_unix_datagram_socket() as unix_dg:
async with create_task_group() as tg:
tg.start_soon(unix_dg.receive)
await wait_all_tasks_blocked()
try:
with pytest.raises(BusyResourceError) as exc:
await unix_dg.receive()
exc.match("already reading from")
finally:
tg.cancel_scope.cancel()
async def test_close_during_receive(self) -> None:
async def close_when_blocked() -> None:
await wait_all_tasks_blocked()
await unix_dg.aclose()
async with await create_unix_datagram_socket() as unix_dg:
async with create_task_group() as tg:
tg.start_soon(close_when_blocked)
with pytest.raises(ClosedResourceError):
await unix_dg.receive()
async def test_receive_after_close(self) -> None:
unix_dg = await create_unix_datagram_socket()
await unix_dg.aclose()
with pytest.raises(ClosedResourceError):
await unix_dg.receive()
async def test_send_after_close(self, socket_path: Path) -> None:
unix_dg = await create_unix_datagram_socket(local_path=socket_path)
path = str(socket_path)
await unix_dg.aclose()
with pytest.raises(ClosedResourceError):
await unix_dg.sendto(b"foo", path)
async def test_local_path_bytes(self, socket_path: Path) -> None:
async with await create_unix_datagram_socket(
local_path=str(socket_path).encode()
):
pass
@pytest.mark.skipif(
platform.system() == "Darwin", reason="macOS requires valid UTF-8 paths"
)
async def test_local_path_invalid_ascii(self, socket_path: Path) -> None:
real_path = str(socket_path).encode() + b"\xf0"
async with await create_unix_datagram_socket(local_path=real_path):
pass
async def test_from_socket(self, sock_or_fd_factory: SockFdFactoryProtocol) -> None:
sock_or_fd = sock_or_fd_factory(socket.AF_UNIX, socket.SOCK_DGRAM)
async with await UNIXDatagramSocket.from_socket(sock_or_fd) as udp_socket:
assert isinstance(udp_socket, UNIXDatagramSocket)
assert udp_socket.extra(SocketAttribute.family) == socket.AF_UNIX
async def test_from_socket_wrong_socket_type(
self, sock_or_fd_factory: SockFdFactoryProtocol
) -> None:
sock_or_fd = sock_or_fd_factory(socket.AF_UNIX, socket.SOCK_STREAM)
with pytest.raises(
ValueError,
match="socket type mismatch: expected SOCK_DGRAM, got SOCK_STREAM",
):
await UNIXDatagramSocket.from_socket(sock_or_fd)
@pytest.mark.skipif(
sys.platform == "win32", reason="UNIX sockets are not available on Windows"
)
class TestConnectedUNIXDatagramSocket:
@pytest.fixture(
params=[
"path",
pytest.param("abstract", marks=[skip_unix_abstract_mark]),
]
)
def socket_path(self, request: SubRequest) -> Generator[Path, None, None]:
# Use stdlib tempdir generation
# Fixes `OSError: AF_UNIX path too long` from pytest generated temp_path
with tempfile.TemporaryDirectory() as path:
if request.param == "path":
yield Path(path) / "socket"
else:
yield Path(f"\0{path}") / "socket"
@pytest.fixture(params=[False, True], ids=["str", "path"])
def socket_path_or_str(self, request: SubRequest, socket_path: Path) -> Path | str:
return socket_path if request.param else str(socket_path)
@pytest.fixture(
params=[
pytest.param("path", id="path-peer"),
pytest.param(
"abstract", marks=[skip_unix_abstract_mark], id="abstract-peer"
),
]
)
def peer_socket_path(self) -> Generator[Path, None, None]:
# Use stdlib tempdir generation
# Fixes `OSError: AF_UNIX path too long` from pytest generated temp_path
with tempfile.TemporaryDirectory() as path:
yield Path(path) / "peer_socket"
@pytest.fixture(params=[False, True], ids=["peer_str", "peer_path"])
def peer_socket_path_or_str(
self, request: SubRequest, peer_socket_path: Path
) -> Path | str:
return peer_socket_path if request.param else str(peer_socket_path)
@pytest.fixture
def peer_sock(self, peer_socket_path: Path) -> Iterable[socket.socket]:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
sock.settimeout(1)
sock.bind(str(peer_socket_path))
yield sock
sock.close()
async def test_extra_attributes(
self,
socket_path: Path,
peer_socket_path: Path,
peer_sock: socket.socket,
) -> None:
async with await create_connected_unix_datagram_socket(
remote_path=peer_socket_path,
local_path=socket_path,
) as unix_dg:
raw_socket = unix_dg.extra(SocketAttribute.raw_socket)
assert raw_socket is not None
assert unix_dg.extra(SocketAttribute.family) == AddressFamily.AF_UNIX
assert os.fsencode(
cast(os.PathLike, unix_dg.extra(SocketAttribute.local_address))
) == os.fsencode(socket_path)
assert os.fsencode(
cast(os.PathLike, unix_dg.extra(SocketAttribute.remote_address))
) == os.fsencode(peer_socket_path)
pytest.raises(
TypedAttributeLookupError, unix_dg.extra, SocketAttribute.local_port
)
pytest.raises(
TypedAttributeLookupError, unix_dg.extra, SocketAttribute.remote_port
)
async def test_send_receive(
self,
socket_path_or_str: Path | str,
peer_socket_path_or_str: Path | str,
) -> None:
async with await create_unix_datagram_socket(
local_path=peer_socket_path_or_str,
) as unix_dg1:
async with await create_connected_unix_datagram_socket(
peer_socket_path_or_str,
local_path=socket_path_or_str,
) as unix_dg2:
socket_path = os.fsdecode(socket_path_or_str)
await unix_dg2.send(b"blah")
data, remote_addr = await unix_dg1.receive()
assert (data, os.fsdecode(remote_addr)) == (b"blah", socket_path)
await unix_dg1.sendto(b"halb", socket_path)
response = await unix_dg2.receive()
assert response == b"halb"
async def test_iterate(
self,
socket_path: Path,
peer_socket_path: Path,
) -> None:
async def serve() -> None:
async for packet in unix_dg2:
await unix_dg2.send(packet[::-1])
async with await create_unix_datagram_socket(
local_path=peer_socket_path,
) as unix_dg1:
async with await create_connected_unix_datagram_socket(
peer_socket_path, local_path=socket_path
) as unix_dg2:
path = os.fsdecode(socket_path)
async with create_task_group() as tg:
tg.start_soon(serve)
await unix_dg1.sendto(b"FOOBAR", path)
data, addr = await unix_dg1.receive()
assert (data, os.fsdecode(addr)) == (b"RABOOF", path)
await unix_dg1.sendto(b"123456", path)
data, addr = await unix_dg1.receive()
assert (data, os.fsdecode(addr)) == (b"654321", path)
tg.cancel_scope.cancel()
async def test_concurrent_receive(
self, peer_socket_path: Path, peer_sock: socket.socket
) -> None:
async with await create_connected_unix_datagram_socket(
peer_socket_path
) as unix_dg:
async with create_task_group() as tg:
tg.start_soon(unix_dg.receive)
await wait_all_tasks_blocked()
try:
with pytest.raises(BusyResourceError) as exc:
await unix_dg.receive()
exc.match("already reading from")
finally:
tg.cancel_scope.cancel()
async def test_close_during_receive(
self, peer_socket_path_or_str: Path | str, peer_sock: socket.socket
) -> None:
async def close_when_blocked() -> None:
await wait_all_tasks_blocked()
await udp.aclose()
async with await create_connected_unix_datagram_socket(
peer_socket_path_or_str
) as udp:
async with create_task_group() as tg:
tg.start_soon(close_when_blocked)
with pytest.raises(ClosedResourceError):
await udp.receive()
async def test_receive_after_close(
self, peer_socket_path_or_str: Path | str, peer_sock: socket.socket
) -> None:
udp = await create_connected_unix_datagram_socket(peer_socket_path_or_str)
await udp.aclose()
with pytest.raises(ClosedResourceError):
await udp.receive()
async def test_send_after_close(
self, peer_socket_path_or_str: Path | str, peer_sock: socket.socket
) -> None:
udp = await create_connected_unix_datagram_socket(peer_socket_path_or_str)
await udp.aclose()
with pytest.raises(ClosedResourceError):
await udp.send(b"foo")
async def test_from_socket(self, sock_or_fd_factory: SockFdFactoryProtocol) -> None:
sock_or_fd = sock_or_fd_factory(
socket.AF_UNIX, socket.SOCK_DGRAM, connected=True
)
async with await ConnectedUNIXDatagramSocket.from_socket(
sock_or_fd
) as udp_socket:
assert isinstance(udp_socket, ConnectedUNIXDatagramSocket)
assert udp_socket.extra(SocketAttribute.family) == socket.AF_UNIX
async def test_from_socket_wrong_socket_type(
self, socket_path: Path, sock_or_fd_factory: SockFdFactoryProtocol
) -> None:
sock_or_fd = sock_or_fd_factory(
socket.AF_UNIX, socket.SOCK_STREAM, connected=True
)
with pytest.raises(
ValueError,
match="socket type mismatch: expected SOCK_DGRAM, got SOCK_STREAM",
):
await ConnectedUNIXDatagramSocket.from_socket(sock_or_fd)
async def test_from_socket_not_connected(
self, sock_or_fd_factory: SockFdFactoryProtocol
) -> None:
sock_or_fd = sock_or_fd_factory(socket.AF_UNIX, socket.SOCK_DGRAM, bound=True)
with pytest.raises(ValueError, match="the socket must be connected"):
await ConnectedUNIXDatagramSocket.from_socket(sock_or_fd)
async def test_tcp_connectable(mocker: MockerFixture) -> None:
mock_connect_tcp = mocker.patch("anyio._core._sockets.connect_tcp")
connectable = TCPConnectable("localhost", 1234)
await connectable.connect()
mock_connect_tcp.assert_called_once_with("localhost", 1234)
async def test_unix_connectable(mocker: MockerFixture) -> None:
mock_connect_tcp = mocker.patch("anyio._core._sockets.connect_unix")
connectable = UNIXConnectable("/foo/bar")
await connectable.connect()
mock_connect_tcp.assert_called_once_with("/foo/bar")
class TestAsConnectable:
def test_pass_connectable(self) -> None:
connectable = TCPConnectable("localhost", 1234)
assert as_connectable(connectable) is connectable
@pytest.mark.parametrize(
"addr",
[
pytest.param("localhost", id="string"),
pytest.param(IPv4Address("127.0.0.1"), id="ipv4addr"),
pytest.param(IPv6Address("::1"), id="ipv6addr"),
],
)
def test_tcp(self, addr: str | IPv4Address | IPv6Address) -> None:
connectable = as_connectable((addr, 1234))
assert isinstance(connectable, TCPConnectable)
assert connectable.host == addr
assert connectable.port == 1234
@pytest.mark.parametrize(
"path",
[
pytest.param("/foo/bar", id="str"),
pytest.param(b"/foo/bar", id="bytes"),
pytest.param(Path("/foo/bar"), id="strpath"),
],
)
def test_unix(self, path: str | bytes | os.PathLike[str]) -> None:
connectable = as_connectable(path)
assert isinstance(connectable, UNIXConnectable)
assert connectable.path == path
def test_bad_type(self) -> None:
with pytest.raises(TypeError, match="cannot convert 1234 to a connectable"):
as_connectable(1234) # type: ignore[arg-type]
def test_tls_true(self) -> None:
connectable = as_connectable(
"/foo/bar",
tls=True,
tls_hostname="example.com",
tls_standard_compatible=False,
)
assert isinstance(connectable, TLSConnectable)
assert connectable.hostname == "example.com"
assert not connectable.standard_compatible
assert isinstance(connectable, TLSConnectable)
def test_tls_explicit_context(self, client_context: SSLContext) -> None:
connectable = as_connectable(
"/foo/bar", tls=True, ssl_context=client_context, tls_hostname="example.com"
)
assert isinstance(connectable, TLSConnectable)
assert connectable.ssl_context is client_context
assert connectable.hostname == "example.com"
def test_tls_tcp_implicit_hostname(self, client_context: SSLContext) -> None:
connectable = as_connectable(("localhost", 1234), tls=True)
assert isinstance(connectable, TLSConnectable)
assert connectable.hostname == "localhost"
@pytest.mark.network
async def test_getaddrinfo() -> None:
# IDNA 2003 gets this wrong
correct = await getaddrinfo("faß.de", 0)
wrong = await getaddrinfo("fass.de", 0)
assert correct != wrong
@pytest.mark.parametrize("sock_type", [socket.SOCK_STREAM, socket.SOCK_STREAM])
async def test_getaddrinfo_ipv6addr(
sock_type: Literal[socket.SocketKind.SOCK_STREAM],
) -> None:
# IDNA trips up over raw IPv6 addresses
proto = 0 if platform.system() == "Windows" else 6
assert await getaddrinfo("::1", 0, type=sock_type) == [
(
socket.AF_INET6,
socket.SOCK_STREAM,
proto,
"",
("::1", 0),
)
]
async def test_getaddrinfo_ipv6_disabled() -> None:
gai_result = [(AddressFamily.AF_INET6, socket.SOCK_STREAM, 6, "", (1, b""))]
with mock.patch.object(get_async_backend(), "getaddrinfo", return_value=gai_result):
assert await getaddrinfo("::1", 0) == []
async def test_getnameinfo() -> None:
expected_result = socket.getnameinfo(("127.0.0.1", 6666), 0)
result = await getnameinfo(("127.0.0.1", 6666))
assert result == expected_result
async def test_connect_tcp_getaddrinfo_context() -> None:
"""
See https://github.com/agronholm/anyio/issues/815
"""
with pytest.raises(socket.gaierror) as exc_info:
async with await connect_tcp("anyio.invalid", 6666):
pass
assert exc_info.value.__context__ is None
@pytest.mark.parametrize("socket_type", ["socket", "fd"])
@pytest.mark.parametrize("event", ["readable", "writable"])
async def test_wait_socket(event: str, socket_type: str) -> None:
wait = wait_readable if event == "readable" else wait_writable
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_sock:
server_sock.bind(("127.0.0.1", 0))
port = server_sock.getsockname()[1]
server_sock.listen()
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_sock:
client_sock.connect(("127.0.0.1", port))
client_sock.sendall(b"Hello, world")
conn, addr = server_sock.accept()
with conn:
sock_or_fd: FileDescriptorLike = (
conn.fileno() if socket_type == "fd" else conn
)
with fail_after(3):
await wait(sock_or_fd)
assert conn.recv(1024) == b"Hello, world"
async def test_deprecated_wait_socket(anyio_backend_name: str) -> None:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
with pytest.warns(
DeprecationWarning,
match="This function is deprecated; use `wait_readable` instead",
):
with move_on_after(0.1):
await wait_socket_readable(sock)
with pytest.warns(
DeprecationWarning,
match="This function is deprecated; use `wait_writable` instead",
):
with move_on_after(0.1):
await wait_socket_writable(sock)
@pytest.mark.parametrize("socket_type", ["socket", "fd"])
async def test_interrupted_by_close(socket_type: str) -> None:
a_sock, b = socket.socketpair()
with a_sock, b:
a_sock.setblocking(False)
b.setblocking(False)
a: FileDescriptorLike = a_sock.fileno() if socket_type == "fd" else a_sock
async def reader() -> None:
with pytest.raises(ClosedResourceError):
await wait_readable(a)
async def writer() -> None:
with pytest.raises(ClosedResourceError):
await wait_writable(a)
fill_socket(a_sock)
async with create_task_group() as tg:
tg.start_soon(reader)
tg.start_soon(writer)
await wait_all_tasks_blocked()
notify_closing(a_sock)
a_sock.close()
|