Skip to content

Server

palfrey.server

Core ASGI server implementation managing connection lifecycle and protocol handoff.

This module implements PalfreyServer, the main async server orchestrating TCP/UNIX socket listening, connection state tracking, and HTTP/1.1→HTTP/2→HTTP/3 protocol negotiation. Key responsibilities include: accepting connections, applying SSL/TLS encryption, managing concurrent request pipelining via the httptools parser, handling connection keep-alive and timeouts, and graceful shutdown coordination with the lifespan manager.

The module tracks active connections via _ConnectionState and _TrackedConnection, enforces PIPELINE_QUEUE_LIMIT to bound concurrent streams per connection, and delegates protocol-specific handling to run_http_asgi, serve_http2_connection, handle_websocket, and create_http3_server based on ALPN negotiation or HTTP upgrade headers.

Key Classes
  • PalfreyServer: Main async server orchestrating listening, protocol selection, and request/response cycling.
  • _ConnectionState: Tracks per-connection state (request queue, trailers, timeouts).
  • _TrackedConnection: Wrapper for stream writers with backpressure tracking.
  • ConnectionContext: Metadata container for client/server addresses and TLS status.

ConnectionContext dataclass

Metadata container for an active network connection.

This class stores identifying information about the client and server endpoints, encryption status, and callbacks for HTTP-specific signaling like '100 Continue'.

ATTRIBUTE DESCRIPTION
client

The remote address of the connecting client.

TYPE: ClientAddress

server

The local address the server is listening on.

TYPE: ServerAddress

is_tls

Flag indicating if the connection is wrapped in SSL/TLS.

TYPE: bool

on_100_continue

Optional async callback to trigger an HTTP 100 Continue response.

TYPE: Callable[[], Awaitable[None]] | None

Source code in palfrey/server.py
@dataclass(slots=True)
class ConnectionContext:
    """
    Metadata container for an active network connection.

    This class stores identifying information about the client and server endpoints,
    encryption status, and callbacks for HTTP-specific signaling like '100 Continue'.

    Attributes:
        client (ClientAddress): The remote address of the connecting client.
        server (ServerAddress): The local address the server is listening on.
        is_tls (bool): Flag indicating if the connection is wrapped in SSL/TLS.
        on_100_continue (Callable[[], Awaitable[None]] | None): Optional async
            callback to trigger an HTTP 100 Continue response.
    """

    client: ClientAddress
    server: ServerAddress
    is_tls: bool
    on_100_continue: Callable[[], Awaitable[None]] | None = None

ServerState dataclass

Shared state shared across all connections and tasks within the server.

This mimics Uvicorn's state container, tracking global metrics and active resources.

ATTRIBUTE DESCRIPTION
total_requests

Cumulative count of requests processed.

TYPE: int

connections

Registry of active _TrackedConnection objects.

TYPE: set[Any]

tasks

Registry of active asyncio tasks.

TYPE: set[Task[None]]

default_headers

Cached headers to be included in every response.

TYPE: list[tuple[bytes, bytes]]

Source code in palfrey/server.py
@dataclass(slots=True)
class ServerState:
    """
    Shared state shared across all connections and tasks within the server.

    This mimics Uvicorn's state container, tracking global metrics and
    active resources.

    Attributes:
        total_requests (int): Cumulative count of requests processed.
        connections (set[Any]): Registry of active _TrackedConnection objects.
        tasks (set[asyncio.Task[None]]): Registry of active asyncio tasks.
        default_headers (list[tuple[bytes, bytes]]): Cached headers to be
            included in every response.
    """

    total_requests: int = 0
    connections: set[Any] = field(default_factory=set)
    tasks: set[asyncio.Task[None]] = field(default_factory=set)
    default_headers: list[tuple[bytes, bytes]] = field(default_factory=list)

PalfreyServer dataclass

The core Palfrey server responsible for managing the ASGI lifecycle.

This class handles socket binding, signal management, connection orchestration, and the main execution loop. It supports HTTP/1.1 and WebSocket protocols via pluggable backends.

ATTRIBUTE DESCRIPTION
config

Configuration object defining server behavior.

TYPE: PalfreyConfig

server_state

Container for runtime metrics and tracking.

TYPE: ServerState

Source code in palfrey/server.py
 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
@dataclass(slots=True)
class PalfreyServer:
    """
    The core Palfrey server responsible for managing the ASGI lifecycle.

    This class handles socket binding, signal management, connection
    orchestration, and the main execution loop. It supports HTTP/1.1
    and WebSocket protocols via pluggable backends.

    Attributes:
        config (PalfreyConfig): Configuration object defining server behavior.
        server_state (ServerState): Container for runtime metrics and tracking.
    """

    config: PalfreyConfig
    server_state: ServerState = field(default_factory=ServerState)
    _resolved_app: ResolvedApp | None = None
    _shutdown_event: asyncio.Event = field(default_factory=asyncio.Event)
    _active_requests: int = 0
    _server: asyncio.Server | None = None
    _servers: list[asyncio.Server] = field(default_factory=list)
    _external_sockets: list[socket.socket] = field(default_factory=list)
    _lifespan: LifespanManager | None = None
    _max_requests_before_exit: int | None = None
    _base_default_headers: list[tuple[bytes, bytes]] = field(default_factory=list)
    _last_notified: float = field(default_factory=time.time)
    _force_exit: bool = False
    _started: bool = False
    _captured_signals: list[int] = field(default_factory=list)

    @property
    def started(self) -> bool:
        """
        Indicates if the server has successfully entered the listening state.
        """
        return self._started or self._server is not None

    async def serve(self, sockets: list[socket.socket] | None = None) -> None:
        """
        Public entry point to start the server asynchronously.

        Args:
            sockets (list[socket.socket] | None): Optional list of pre-bound
                sockets to use for listening.
        """
        with self.capture_signals():
            await self._serve(sockets=sockets)

    async def _serve(self, sockets: list[socket.socket] | None = None) -> None:
        """
        Internal implementation of the server startup and lifecycle.
        """
        configure_logging(self.config)
        logger.info("Started server process [%d]", os.getpid())
        self._validate_protocol_backends()
        if not self.config.loaded:
            self.config.load()

        self._resolved_app = ResolvedApp(
            app=self.config.loaded_app,
            interface=self.config.interface,
        )
        self._max_requests_before_exit = self._compute_max_requests_before_exit()
        self._base_default_headers = self._build_static_default_headers()

        # Handle ASGI Lifespan protocol (startup/shutdown events)
        if self.config.lifespan_class is not None:
            self._lifespan = self.config.lifespan_class(
                self._resolved_app.app,
                lifespan_mode=self.config.lifespan,
            )
            try:
                await self._lifespan.startup()
            except RuntimeError as exc:
                logger.error("Application startup failed: %s", exc)
                return
            if self._lifespan.should_exit:
                return

        loop = asyncio.get_running_loop()
        self._log_runtime_configuration(loop)
        for sig in (signal.SIGINT, signal.SIGTERM):
            with contextlib.suppress(NotImplementedError, RuntimeError):
                loop.add_signal_handler(sig, lambda _sig=sig: self._handle_exit_signal(_sig))

        if self.config.effective_http == "h3":
            await self._serve_http3(sockets=sockets)
            return

        ssl_context = self._build_ssl_context()
        use_protocol_factory = self._use_protocol_factory_mode()
        protocol_factory = self._build_protocol_factory(loop) if use_protocol_factory else None
        self._external_sockets = list(sockets) if sockets is not None else []

        try:
            if sockets is not None:
                self._servers = []
                for sock in sockets:
                    if use_protocol_factory:
                        assert protocol_factory is not None
                        server = await loop.create_server(
                            protocol_factory,
                            sock=sock,
                            ssl=ssl_context,
                            backlog=self.config.backlog,
                        )
                    else:
                        server = await asyncio.start_server(
                            self._handle_connection,
                            sock=sock,
                            ssl=ssl_context,
                            backlog=self.config.backlog,
                        )
                    self._servers.append(server)
                self._server = self._servers[0] if self._servers else None
            elif self.config.fd is not None:
                # Support for file-descriptor based socket inheritance
                server_socket = socket.fromfd(self.config.fd, SOCKET_AF_UNIX, socket.SOCK_STREAM)
                server_socket.setblocking(False)
                if use_protocol_factory:
                    assert protocol_factory is not None
                    self._server = await loop.create_server(
                        protocol_factory,
                        sock=server_socket,
                        ssl=ssl_context,
                        backlog=self.config.backlog,
                    )
                else:
                    self._server = await asyncio.start_server(
                        self._handle_connection,
                        sock=server_socket,
                        ssl=ssl_context,
                        backlog=self.config.backlog,
                    )
            elif self.config.uds:
                # Logic for Unix Domain Socket binding and permission handling
                uds_perms = 0o666
                if os.path.exists(self.config.uds):
                    uds_perms = os.stat(self.config.uds).st_mode
                if use_protocol_factory:
                    assert protocol_factory is not None
                    create_unix_server = getattr(loop, "create_unix_server", None)
                    if not callable(create_unix_server):
                        raise OSError("Unix domain sockets are not supported on this platform.")
                    self._server = await create_unix_server(
                        protocol_factory,
                        path=self.config.uds,
                        backlog=self.config.backlog,
                        ssl=ssl_context,
                    )
                else:
                    start_unix_server = getattr(asyncio, "start_unix_server", None)
                    if not callable(start_unix_server):
                        raise OSError("Unix domain sockets are not supported on this platform.")
                    self._server = await start_unix_server(
                        self._handle_connection,
                        path=self.config.uds,
                        backlog=self.config.backlog,
                        ssl=ssl_context,
                    )
                with contextlib.suppress(OSError):
                    os.chmod(self.config.uds, uds_perms)
            else:
                # Standard TCP host/port binding
                if use_protocol_factory:
                    assert protocol_factory is not None
                    self._server = await loop.create_server(
                        protocol_factory,
                        host=self.config.host,
                        port=self.config.port,
                        backlog=self.config.backlog,
                        ssl=ssl_context,
                        reuse_port=self.config.workers_count > 1,
                    )
                else:
                    self._server = await asyncio.start_server(
                        self._handle_connection,
                        host=self.config.host,
                        port=self.config.port,
                        backlog=self.config.backlog,
                        ssl=ssl_context,
                        reuse_port=self.config.workers_count > 1,
                    )
        except OSError as exc:
            logger.error("%s", exc)
            if self._lifespan is not None:
                await self._lifespan.shutdown()
            return

        if not self._servers:
            self._servers = [self._server] if self._server is not None else []

        listening_sockets: list[socket.socket] = []
        for server in self._servers:
            bound_sockets = cast(list[socket.socket] | None, getattr(server, "sockets", None))
            if bound_sockets:
                listening_sockets.extend(bound_sockets)
        self._log_running_messages(listening_sockets)
        self._started = True

        await self._main_loop()
        await self._shutdown()

    def run(self, sockets: list[socket.socket] | None = None) -> None:
        """
        Blocks while running the server in a new event loop.
        """
        asyncio.run(self.serve(sockets=sockets))

    def request_shutdown(self) -> None:
        """
        Signals the server to begin the graceful shutdown process.
        """
        self._shutdown_event.set()

    def _handle_exit_signal(self, sig: signal.Signals) -> None:
        """
        Handles OS signals like SIGINT or SIGTERM.
        """
        if self._shutdown_event.is_set() and sig == signal.SIGINT:
            self._force_exit = True
            return
        self.request_shutdown()

    @contextlib.contextmanager
    def capture_signals(self):
        """
        Context manager to intercept OS signals for graceful termination.
        """
        if threading.current_thread() is not threading.main_thread():
            yield
            return

        original_handlers = {sig: signal.signal(sig, self.handle_exit) for sig in HANDLED_SIGNALS}
        try:
            yield
        finally:
            for sig, handler in original_handlers.items():
                signal.signal(sig, handler)

        for captured_signal in reversed(self._captured_signals):
            signal.raise_signal(captured_signal)

    def handle_exit(self, sig: int, _frame: FrameType | None) -> None:
        """
        Callback for signal.signal to register captured signals.
        """
        self._captured_signals.append(sig)
        self._handle_exit_signal(signal.Signals(sig))

    async def _main_loop(self) -> None:
        """
        Main execution sleep loop that periodically executes maintenance ticks.
        """
        counter = 0
        should_exit = await self._on_tick(counter)
        while not should_exit:
            counter = (counter + 1) % 864000
            await asyncio.sleep(0.1)
            should_exit = await self._on_tick(counter)

    async def _on_tick(self, counter: int) -> bool:
        """
        Logic executed every 100ms for status updates and limit checking.
        """
        if counter % 10 == 0:
            if not self._base_default_headers:
                self._base_default_headers = self._build_static_default_headers()
            current_time = time.time()
            current_date = cached_http_date_header()
            current_headers = list(self._base_default_headers)
            header_names = {name for name, _ in current_headers}
            if self.config.date_header and b"date" not in header_names:
                current_headers.insert(0, (b"date", current_date))
            self.server_state.default_headers = current_headers

            if (
                self.config.callback_notify is not None
                and current_time - self._last_notified > self.config.timeout_notify
            ):
                self._last_notified = current_time
                await self.config.callback_notify()

        if self._shutdown_event.is_set():
            return True

        if self._max_requests_before_exit is None:
            self._max_requests_before_exit = self._compute_max_requests_before_exit()
        if (
            self._max_requests_before_exit is not None
            and self.server_state.total_requests >= self._max_requests_before_exit
        ):
            logger.info(
                "Maximum request limit of %d exceeded. Terminating process.",
                self._max_requests_before_exit,
            )
            self.request_shutdown()
            return True

        return False

    async def _shutdown(self) -> None:
        """
        Handles the stopping of listeners and draining of active connections.
        """
        logger.info("Shutting down")

        servers = list(self._servers)
        if not servers and self._server is not None:
            servers = [self._server]

        for server in servers:
            close = getattr(server, "close", None)
            if callable(close):
                close()
        for server in servers:
            wait_closed = getattr(server, "wait_closed", None)
            if callable(wait_closed):
                await wait_closed()
        self._servers.clear()
        self._server = None

        for sock in self._external_sockets:
            with contextlib.suppress(OSError):
                sock.close()
        self._external_sockets.clear()

        for connection in list(self.server_state.connections):
            connection.shutdown()
        await asyncio.sleep(0.1)

        try:
            await asyncio.wait_for(
                self._wait_tasks_to_complete(),
                timeout=self.config.timeout_graceful_shutdown,
            )
        except asyncio.TimeoutError:
            logger.error(
                "Cancel %s running task(s), timeout graceful shutdown exceeded",
                len(self.server_state.tasks),
            )
            for task in list(self.server_state.tasks):
                task.cancel(msg="Task cancelled, timeout graceful shutdown exceeded")

        if self._lifespan is not None and not self._force_exit:
            await self._lifespan.shutdown()

    async def _wait_tasks_to_complete(self) -> None:
        """
        Drains active connections and tasks until the set is empty or forced out.
        """
        if self.server_state.connections and not self._force_exit:
            logger.info("Waiting for connections to close. (CTRL+C to force quit)")
            while self.server_state.connections and not self._force_exit:
                await asyncio.sleep(0.1)

        if self.server_state.tasks and not self._force_exit:
            logger.info("Waiting for background tasks to complete. (CTRL+C to force quit)")
            while self.server_state.tasks and not self._force_exit:
                await asyncio.sleep(0.1)

    async def _handle_connection(
        self,
        reader: asyncio.StreamReader,
        writer: asyncio.StreamWriter,
    ) -> None:
        """
        Individual connection handler for each incoming stream.
        """
        if self._resolved_app is None:
            return

        # Set TCP_NODELAY to disable Nagle algorithm (reduces latency for small packets)
        transport = getattr(writer, "transport", None) or getattr(writer, "_transport", None)
        if transport is not None:
            sock = (
                transport.get_extra_info("socket") if hasattr(transport, "get_extra_info") else None
            )
            if sock is not None and hasattr(socket, "TCP_NODELAY"):
                try:
                    sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
                except (OSError, AttributeError):
                    # Socket may not support TCP options (e.g., Unix domain socket)
                    pass

        peername = writer.get_extra_info("peername")
        sockname = writer.get_extra_info("sockname")
        ssl_object = writer.get_extra_info("ssl_object")

        client = self._normalize_address(peername, default_host="0.0.0.0", default_port=0)
        server = self._normalize_address(
            sockname,
            default_host=self.config.host,
            default_port=self.config.port,
        )

        context = ConnectionContext(client=client, server=server, is_tls=ssl_object is not None)
        tracked_connection = _TrackedConnection(writer=writer)
        self.server_state.connections.add(tracked_connection)
        current_task = asyncio.current_task()
        if current_task is not None:
            self.server_state.tasks.add(current_task)

        if self.config.effective_http == "h2":
            try:
                await self._handle_http2_connection(
                    reader=reader,
                    writer=writer,
                    context=context,
                )
            except Exception as exc:
                logger.exception("HTTP/2 connection handler failed: %s", exc)
            finally:
                self.server_state.connections.discard(tracked_connection)
                if current_task is not None:
                    self.server_state.tasks.discard(current_task)
                writer.close()
                with contextlib.suppress(Exception):
                    await writer.wait_closed()
            return

        keep_processing = True
        keep_alive_timeout = self.config.timeout_keep_alive
        # Use a bounded queue to enforce backpressure: when full, pauses socket reads to prevent unbounded memory
        request_queue: asyncio.Queue[_QueuedRequest] = asyncio.Queue(maxsize=PIPELINE_QUEUE_LIMIT)

        # Start the pipelining reader task
        request_reader_task = asyncio.create_task(
            self._queue_connection_requests(
                reader=reader,
                queue=request_queue,
                keep_alive_timeout=keep_alive_timeout,
            )
        )

        async def stop_request_reader() -> None:
            """
            Cancels and waits for the request reader task to terminate.
            """
            if request_reader_task.done():
                return
            request_reader_task.cancel()
            with contextlib.suppress(asyncio.CancelledError):
                await request_reader_task

        try:
            while keep_processing:
                queued_request = await request_queue.get()
                if queued_request.error is not None:
                    raise queued_request.error

                request = queued_request.request
                if request is None:
                    break

                # WebSocket Upgrade handling
                if is_websocket_upgrade(request):
                    await stop_request_reader()
                    if self.config.effective_ws == "none":
                        error_response = HTTPResponse(
                            status=400,
                            headers=[(b"content-type", b"text/plain")],
                            body_chunks=[b"Bad Request"],
                        )
                        await self._write_response(writer, error_response, keep_alive=False)
                        break

                    if self._use_custom_ws_protocol_mode():
                        await self._run_custom_ws_protocol(
                            request=request,
                            reader=reader,
                            writer=writer,
                        )
                    else:
                        websocket_headers = [
                            (
                                name.decode("latin-1") if isinstance(name, bytes) else str(name),
                                value.decode("latin-1") if isinstance(value, bytes) else str(value),
                            )
                            for name, value in request.headers
                        ]
                        await handle_websocket(
                            self._resolved_app.app,
                            self.config,
                            reader=reader,
                            writer=writer,
                            headers=websocket_headers,
                            target=request.target,
                            client=context.client,
                            server=context.server,
                            is_tls=context.is_tls,
                        )
                    break

                # HTTP 100-Continue handshake
                if requires_100_continue(request):

                    async def send_continue() -> None:
                        writer.write(_STATUS_LINES[100])
                        await writer.drain()

                    context.on_100_continue = send_continue
                else:
                    context.on_100_continue = None

                if self._is_concurrency_limit_exceeded():
                    await self._write_response(
                        writer,
                        self._service_unavailable_response(),
                        keep_alive=False,
                    )
                    break

                # Check concurrency limits to ensure fair resource distribution across connections
                acquired = self._enter_request_slot()
                if not acquired:
                    await self._write_response(
                        writer, self._service_unavailable_response(), keep_alive=False
                    )
                    break

                try:
                    response = await self._handle_http_request(request, context, writer=writer)
                finally:
                    self._leave_request_slot()

                keep_processing = should_keep_alive(request, response)
                if not response.streamed:
                    await self._write_response(writer, response, keep_alive=keep_processing)

                self.server_state.total_requests += 1
                if self._max_requests_before_exit is None:
                    self._max_requests_before_exit = self._compute_max_requests_before_exit()
                if (
                    self._max_requests_before_exit is not None
                    and self.server_state.total_requests >= self._max_requests_before_exit
                ):
                    self.request_shutdown()
        except ValueError as exc:
            logger.warning("Bad request: %s", exc)
            error_response = HTTPResponse(status=400, headers=[(b"content-type", b"text/plain")])
            error_response.body_chunks = [b"Bad Request"]
            await self._write_response(writer, error_response, keep_alive=False)
        except Exception as exc:
            logger.exception("Connection handler failed: %s", exc)
            error_response = HTTPResponse(status=500, headers=[(b"content-type", b"text/plain")])
            error_response.body_chunks = [b"Internal Server Error"]
            await self._write_response(writer, error_response, keep_alive=False)
        finally:
            await stop_request_reader()
            self.server_state.connections.discard(tracked_connection)
            if current_task is not None:
                self.server_state.tasks.discard(current_task)
            writer.close()
            with contextlib.suppress(Exception):
                await writer.wait_closed()

    async def _handle_http2_connection(
        self,
        *,
        reader: asyncio.StreamReader,
        writer: asyncio.StreamWriter,
        context: ConnectionContext,
    ) -> None:
        """
        Handle one HTTP/2 connection and map each completed stream to the ASGI app.

        Args:
            reader (asyncio.StreamReader): Source stream reader.
            writer (asyncio.StreamWriter): Destination stream writer.
            context (ConnectionContext): Connection metadata for scope construction.
        """

        async def request_handler(request: HTTPRequest) -> HTTPResponse:
            """
            Bridge to handle incoming HTTP/2 requests via the ASGI application.

            Args:
                request (HTTPRequest): The parsed HTTP request.

            Returns:
                HTTPResponse: The response generated by the application.
            """
            if self._is_concurrency_limit_exceeded():
                return self._service_unavailable_response()

            acquired = self._enter_request_slot()
            if not acquired:
                return self._service_unavailable_response()

            try:
                response = await self._handle_http_request(request, context)
            except Exception as exc:
                logger.exception("HTTP/2 request handling failed: %s", exc)
                response = HTTPResponse(status=500, headers=[(b"content-type", b"text/plain")])
                response.body_chunks = [b"Internal Server Error"]
                append_default_response_headers(response, self.config)
            finally:
                self._leave_request_slot()

            self.server_state.total_requests += 1
            if self._max_requests_before_exit is None:
                self._max_requests_before_exit = self._compute_max_requests_before_exit()
            if (
                self._max_requests_before_exit is not None
                and self.server_state.total_requests >= self._max_requests_before_exit
            ):
                self.request_shutdown()

            return response

        await serve_http2_connection(
            reader=reader,
            writer=writer,
            request_handler=request_handler,
        )

    async def _queue_connection_requests(
        self,
        *,
        reader: asyncio.StreamReader,
        queue: asyncio.Queue[_QueuedRequest],
        keep_alive_timeout: float,
    ) -> None:
        """
        Background reader that parses requests from the stream and puts them in a queue.
        """
        first_request = True
        try:
            while True:
                request_coro = read_http_request(
                    reader,
                    max_head_size=self.config.h11_max_incomplete_event_size or 1_048_576,
                    parser_mode=self.config.effective_http,
                )
                try:
                    if first_request and keep_alive_timeout > 0:
                        request = await request_coro
                    else:
                        request = await asyncio.wait_for(request_coro, timeout=keep_alive_timeout)
                except asyncio.TimeoutError:
                    await self._queue_with_backpressure(reader, queue, _QueuedRequest(request=None))
                    return
                except Exception as exc:
                    await self._queue_with_backpressure(reader, queue, _QueuedRequest(error=exc))
                    return

                first_request = False
                await self._queue_with_backpressure(reader, queue, _QueuedRequest(request=request))
                if request is None:
                    return
        except asyncio.CancelledError:
            return

    async def _queue_with_backpressure(
        self,
        reader: asyncio.StreamReader,
        queue: asyncio.Queue[_QueuedRequest],
        item: _QueuedRequest,
    ) -> None:
        """
        Enqueues requests while applying backpressure to the transport when the queue is full.
        """
        paused = False
        if queue.full():
            # Pause socket reads to prevent CPU-wasting busy loops while app processes slow requests
            self._pause_stream_reader(reader)
            paused = True
        try:
            await queue.put(item)
        finally:
            if paused:
                self._resume_stream_reader(reader)

    @staticmethod
    def _pause_stream_reader(reader: asyncio.StreamReader) -> None:
        """
        Instructs the transport to stop reading from the socket.
        """
        transport = getattr(reader, "_transport", None)
        if transport is None:
            return
        with contextlib.suppress(Exception):
            transport.pause_reading()

    @staticmethod
    def _resume_stream_reader(reader: asyncio.StreamReader) -> None:
        """
        Instructs the transport to resume reading from the socket.
        """
        transport = getattr(reader, "_transport", None)
        if transport is None:
            return
        with contextlib.suppress(Exception):
            transport.resume_reading()

    async def _handle_http_request(
        self,
        request: HTTPRequest,
        context: ConnectionContext,
        writer: asyncio.StreamWriter | None = None,
    ) -> HTTPResponse:
        """
        Converts a parsed HTTP request into an ASGI scope and runs the application.
        """
        if self._resolved_app is None:
            raise RuntimeError("Application is not resolved.")

        scope = build_http_scope(
            request,
            client=context.client,
            server=context.server,
            root_path=self.config.root_path,
            is_tls=context.is_tls,
        )

        body_input: bytes | list[bytes] = (
            request.body_chunks if request.body_chunks else request.body
        )
        streamed_head_sent = False

        async def on_response_start(response: HTTPResponse) -> None:
            default_headers = self.server_state.default_headers or None
            append_default_response_headers(response, self.config, default_headers=default_headers)
            self._log_access(scope, response)

        async def on_response_body(
            response: HTTPResponse,
            body: bytes,
            more_body: bool,
        ) -> None:
            nonlocal streamed_head_sent
            if not streamed_head_sent:
                await self._write_response_head_and_body_chunk(
                    writer,
                    response,
                    body,
                    keep_alive=should_keep_alive(request, response),
                    more_body=more_body,
                )
                streamed_head_sent = True
                return
            await self._write_response_body_chunk(writer, response, body, more_body=more_body)

        response = await run_http_asgi(
            self._resolved_app.app,
            scope,
            body_input,
            expect_100_continue=requires_100_continue(request),
            on_100_continue=context.on_100_continue,
            on_response_start=on_response_start if writer is not None else None,
            on_response_body=on_response_body if writer is not None else None,
        )

        if not response.streamed:
            default_headers = self.server_state.default_headers or None
            append_default_response_headers(response, self.config, default_headers=default_headers)
            self._log_access(scope, response)

        return response

    def _log_access(self, scope: dict[str, Any], response: HTTPResponse) -> None:
        """
        Emits a configured access-log entry for one HTTP response.
        """
        if not self.config.access_log:
            return
        request_path = get_path_with_query_string(scope)
        access_logger.info(
            '%s - "%s %s HTTP/%s" %s',
            scope["client"][0],
            scope["method"],
            request_path,
            scope["http_version"],
            response.status,
        )

    async def _write_response_head_and_body_chunk(
        self,
        writer: asyncio.StreamWriter | None,
        response: HTTPResponse,
        body: bytes,
        *,
        keep_alive: bool,
        more_body: bool,
    ) -> None:
        """
        Writes response status, headers, and first body event together.
        """
        if writer is None:
            return
        payload = b"".join(
            (
                *encode_http_response_head(response, keep_alive=keep_alive),
                *encode_http_response_body_chunk(response, body, more_body=more_body),
            )
        )
        if payload:
            writer.write(payload)
        await writer.drain()

    async def _write_response_body_chunk(
        self,
        writer: asyncio.StreamWriter | None,
        response: HTTPResponse,
        body: bytes,
        *,
        more_body: bool,
    ) -> None:
        """
        Writes one ASGI body event immediately for streaming responses.
        """
        if writer is None:
            return
        payload = b"".join(encode_http_response_body_chunk(response, body, more_body=more_body))
        if payload:
            writer.write(payload)
        await writer.drain()

    async def _write_response(
        self,
        writer: asyncio.StreamWriter,
        response: HTTPResponse,
        *,
        keep_alive: bool,
    ) -> None:
        """
        Serializes and writes the HTTP response to the stream.
        """
        payload_chunks = encode_http_response_chunks(response, keep_alive=keep_alive)
        transport = getattr(writer, "transport", None) or getattr(writer, "_transport", None)
        get_write_buffer_size = (
            getattr(transport, "get_write_buffer_size", None) if transport is not None else None
        )
        high_watermark_bytes = 262_144
        if hasattr(transport, "get_write_buffer_limits"):
            try:
                high_watermark_bytes, _ = transport.get_write_buffer_limits()
            except (ValueError, TypeError):
                pass
        pending_bytes = 0

        async def drain_if_needed() -> None:
            """
            Checks the transport write buffer and drains if it exceeds the high watermark.
            """
            nonlocal pending_bytes

            if not callable(get_write_buffer_size) or pending_bytes < high_watermark_bytes:
                return

            if int(get_write_buffer_size()) >= high_watermark_bytes:
                await writer.drain()
                pending_bytes = 0

        writelines = getattr(writer, "writelines", None)
        if callable(writelines):
            batch: list[bytes] = []
            for chunk in payload_chunks:
                batch.append(chunk)
                pending_bytes += len(chunk)
                if pending_bytes >= high_watermark_bytes:
                    writelines(batch)
                    batch = []
                    await drain_if_needed()
            if batch:
                writelines(batch)
        else:
            for chunk in payload_chunks:
                writer.write(chunk)
                pending_bytes += len(chunk)
                await drain_if_needed()
        await writer.drain()

    def _service_unavailable_response(self) -> HTTPResponse:
        """
        Creates a standard 503 Service Unavailable response.
        """
        response = HTTPResponse(status=503, headers=[(b"content-type", b"text/plain")])
        response.body_chunks = [b"Service Unavailable"]
        append_default_response_headers(response, self.config)
        return response

    def _enter_request_slot(self) -> bool:
        """
        Decrements the available concurrency slot count.
        """
        limit = self.config.limit_concurrency
        if limit is None:
            return True

        if self._active_requests >= limit:
            return False

        self._active_requests += 1
        return True

    def _is_concurrency_limit_exceeded(self) -> bool:
        """
        Checks if current global resource usage exceeds configured limits.
        """
        limit = self.config.limit_concurrency
        if limit is None:
            return False
        return len(self.server_state.connections) >= limit or len(self.server_state.tasks) >= limit

    def _leave_request_slot(self) -> None:
        """
        Increments the available concurrency slot count.
        """
        limit = self.config.limit_concurrency
        if limit is None:
            return

        if self._active_requests > 0:
            self._active_requests -= 1

    @staticmethod
    def _normalize_address(
        value: Any,
        *,
        default_host: str,
        default_port: int,
    ) -> tuple[str, int]:
        """
        Ensures socket addresses are returned as a host/port tuple.
        """
        if isinstance(value, tuple) and len(value) >= 2:
            host = str(value[0])
            try:
                port = int(value[1])
            except (TypeError, ValueError):
                port = default_port
            return host, port
        return default_host, default_port

    def _build_ssl_context(self) -> ssl.SSLContext | None:
        """
        Constructs the SSLContext for encrypted connections.
        """
        if self.config.ssl_context is not None:
            return self.config.ssl_context
        if not self.config.is_ssl:
            return None

        assert self.config.ssl_certfile
        self.config.ssl_context = create_ssl_context(
            certfile=self.config.ssl_certfile,
            keyfile=self.config.ssl_keyfile,
            password=self.config.ssl_keyfile_password,
            ssl_version=self.config.ssl_version,
            cert_reqs=self.config.ssl_cert_reqs,
            ca_certs=self.config.ssl_ca_certs,
            ciphers=self.config.ssl_ciphers,
        )
        if self.config.effective_http == "h2":
            with contextlib.suppress(NotImplementedError, ValueError, AttributeError):
                self.config.ssl_context.set_alpn_protocols(["h2"])
        return self.config.ssl_context

    @staticmethod
    def _loop_backend_name(loop: asyncio.AbstractEventLoop) -> str:
        """
        Infer a stable loop backend name from the runtime loop type.

        Args:
            loop (asyncio.AbstractEventLoop): Running event loop instance.

        Returns:
            str: Friendly loop backend label suitable for startup logs.
        """
        module_name = loop.__class__.__module__
        class_name = loop.__class__.__name__

        if module_name.startswith("uvloop"):
            return "uvloop"
        if module_name.startswith("asyncio"):
            return "asyncio"
        return f"{module_name}.{class_name}"

    def _log_runtime_configuration(self, loop: asyncio.AbstractEventLoop) -> None:
        """
        Emit a concise startup summary for selected runtime backends.

        Args:
            loop (asyncio.AbstractEventLoop): Running event loop instance.
        """
        logger.info(
            "Runtime configuration: loop=%s, http=%s, ws=%s, lifespan=%s, interface=%s",
            self._loop_backend_name(loop),
            self.config.effective_http,
            self.config.effective_ws,
            self.config.lifespan,
            self.config.interface,
        )

    def _log_running_messages(self, sockets: list[socket.socket]) -> None:
        """
        Emit human-friendly startup messages for bound listeners.

        Args:
            sockets (list[socket.socket]): Socket list attached to running servers.
        """
        if not sockets:
            if self.config.uds:
                logger.info(
                    "Palfrey running on unix socket %s (Press CTRL+C to quit)",
                    self.config.uds,
                )
                return

            scheme = "https" if self.config.is_ssl else "http"
            host = self.config.host
            if ":" in host and not host.startswith("["):
                host = f"[{host}]"
            logger.info(
                "Palfrey running on %s://%s:%d (Press CTRL+C to quit)",
                scheme,
                host,
                self.config.port,
            )
            return

        seen_targets: set[str] = set()
        for sock in sockets:
            target = self._format_running_target(sock)
            if target in seen_targets:
                continue
            seen_targets.add(target)
            logger.info("Palfrey running on %s (Press CTRL+C to quit)", target)

    def _format_running_target(self, sock: socket.socket) -> str:
        """
        Convert a socket endpoint into a display string.

        Args:
            sock (socket.socket): Bound listener socket.

        Returns:
            str: URL-like or unix-socket endpoint representation.
        """
        try:
            sockname = sock.getsockname()
        except OSError:
            return "<unknown>"

        if isinstance(sockname, str):
            return f"unix socket {sockname}"

        if isinstance(sockname, tuple) and len(sockname) >= 2:
            host = str(sockname[0])
            port = int(sockname[1])
            if ":" in host and not host.startswith("["):
                host = f"[{host}]"
            scheme = "https" if self.config.is_ssl else "http"
            return f"{scheme}://{host}:{port}"

        return str(sockname)

    async def _serve_http3(self, *, sockets: list[socket.socket] | None) -> None:
        """
        Start QUIC/HTTP3 serving loop using aioquic.

        Args:
            sockets (list[socket.socket] | None): Pre-bound sockets. Not supported for HTTP/3.
        """
        if sockets is not None:
            raise RuntimeError("HTTP mode 'h3' does not support pre-bound sockets.")
        if self.config.fd is not None:
            raise RuntimeError("HTTP mode 'h3' does not support --fd.")
        if self.config.uds:
            raise RuntimeError("HTTP mode 'h3' does not support --uds.")
        if self._resolved_app is None:
            raise RuntimeError("Application is not resolved.")

        async def request_handler(
            request: HTTPRequest,
            client: ClientAddress,
            server: ServerAddress,
        ) -> HTTPResponse:
            """
            Bridge to handle incoming HTTP/3 requests via the ASGI application.

            Args:
                request (HTTPRequest): The parsed HTTP request.
                client (ClientAddress): The client address.
                server (ServerAddress): The server address.

            Returns:
                HTTPResponse: The response generated by the application.
            """
            context = ConnectionContext(client=client, server=server, is_tls=True)

            if self._is_concurrency_limit_exceeded():
                return self._service_unavailable_response()

            acquired = self._enter_request_slot()
            if not acquired:
                return self._service_unavailable_response()

            try:
                response = await self._handle_http_request(request, context)
            except Exception as exc:
                logger.exception("HTTP/3 request handling failed: %s", exc)
                response = HTTPResponse(status=500, headers=[(b"content-type", b"text/plain")])
                response.body_chunks = [b"Internal Server Error"]
                append_default_response_headers(response, self.config)
            finally:
                self._leave_request_slot()

            self.server_state.total_requests += 1
            if self._max_requests_before_exit is None:
                self._max_requests_before_exit = self._compute_max_requests_before_exit()
            if (
                self._max_requests_before_exit is not None
                and self.server_state.total_requests >= self._max_requests_before_exit
            ):
                self.request_shutdown()

            return response

        self._server = await create_http3_server(
            config=self.config,
            request_handler=request_handler,
        )
        self._servers = [self._server]

        host = self.config.host
        if ":" in host and not host.startswith("["):
            host = f"[{host}]"
        logger.info(
            "Palfrey running on https://%s:%d (HTTP/3 over QUIC) (Press CTRL+C to quit)",
            host,
            self.config.port,
        )
        self._started = True

        await self._main_loop()
        await self._shutdown()

    def _compute_max_requests_before_exit(self) -> int | None:
        """
        Computes the maximum request threshold with an optional random jitter.
        """
        if self.config.limit_max_requests is None:
            return None

        return self.config.limit_max_requests + random.randint(
            0,
            self.config.limit_max_requests_jitter,
        )

    def _build_static_default_headers(self) -> list[tuple[bytes, bytes]]:
        """
        Generates the static portion of default response headers.
        """
        encoded_headers = list(getattr(self.config, "encoded_headers", []))
        if encoded_headers:
            return encoded_headers

        configured_headers = [
            (name.lower().encode("latin-1"), value.encode("latin-1"))
            for name, value in self.config.normalized_headers
        ]
        configured_names = {name for name, _ in configured_headers}
        if self.config.server_header and b"server" not in configured_names:
            return [(b"server", b"palfrey"), *configured_headers]
        return configured_headers

    def _use_protocol_factory_mode(self) -> bool:
        """
        Detects if the configuration specifies a low-level asyncio.Protocol class.
        """
        protocol_class = self.config.http_protocol_class
        return isinstance(protocol_class, type) and issubclass(protocol_class, asyncio.Protocol)

    def _build_protocol_factory(
        self,
        loop: asyncio.AbstractEventLoop,
    ) -> Callable[[], asyncio.Protocol]:
        """
        Constructs a factory that instantiates custom HTTP protocol classes.
        """
        protocol_class = cast(type[asyncio.Protocol], self.config.http_protocol_class)
        protocol_constructor = cast("Callable[..., asyncio.Protocol]", protocol_class)
        app_state = getattr(self._lifespan, "state", {}) if self._lifespan is not None else {}

        def create_protocol(
            _loop: asyncio.AbstractEventLoop | None = None,
        ) -> asyncio.Protocol:
            """
            Factory function to instantiate the configured protocol class.

            Args:
                _loop (asyncio.AbstractEventLoop | None): The event loop to use.

            Returns:
                asyncio.Protocol: An instance of the configured protocol class.
            """
            return protocol_constructor(
                config=self.config,
                server_state=self.server_state,
                app_state=app_state,
                _loop=_loop or loop,
            )

        return create_protocol

    def _use_custom_ws_protocol_mode(self) -> bool:
        """
        Detects if the configuration specifies a low-level WebSocket protocol class.
        """
        protocol_class = self.config.ws_protocol_class
        return isinstance(protocol_class, type) and issubclass(protocol_class, asyncio.Protocol)

    async def _run_custom_ws_protocol(
        self,
        *,
        request: HTTPRequest,
        reader: asyncio.StreamReader,
        writer: asyncio.StreamWriter,
    ) -> None:
        """
        Hand-off mechanism for delegating WebSocket traffic to a custom protocol class.
        """
        protocol_class = cast(type[asyncio.Protocol], self.config.ws_protocol_class)
        protocol_constructor = cast("Callable[..., asyncio.Protocol]", protocol_class)

        loop = asyncio.get_running_loop()
        app_state = getattr(self._lifespan, "state", {}) if self._lifespan is not None else {}
        protocol = protocol_constructor(
            config=self.config,
            server_state=self.server_state,
            app_state=app_state,
            _loop=loop,
        )

        transport = getattr(writer, "transport", None)
        if transport is None:
            raise RuntimeError("Unable to access stream transport for custom websocket protocol.")

        protocol.connection_made(transport)
        protocol.data_received(self._serialize_http_request(request))

        try:
            while not writer.is_closing():
                chunk = await reader.read(65_536)
                if not chunk:
                    eof_received = getattr(protocol, "eof_received", None)
                    if callable(eof_received):
                        eof_received()
                    break
                protocol.data_received(chunk)
        finally:
            connection_lost = getattr(protocol, "connection_lost", None)
            if callable(connection_lost):
                connection_lost(None)

    @staticmethod
    def _serialize_http_request(request: HTTPRequest) -> bytes:
        """
        Re-serializes an HTTPRequest object into raw bytes for custom protocols.
        """
        parts: list[bytes] = [
            f"{request.method} {request.target} {request.http_version}\r\n".encode("latin-1")
        ]

        for name, value in request.headers:
            # Safely coerce to bytes in case parsers/middlewares provided raw bytes
            name_bytes = name if isinstance(name, bytes) else name.encode("latin-1")
            value_bytes = value if isinstance(value, bytes) else value.encode("latin-1")

            parts.append(name_bytes)
            parts.append(b": ")
            parts.append(value_bytes)
            parts.append(b"\r\n")

        parts.append(b"\r\n")
        parts.append(request.body)

        return b"".join(parts)

    def _validate_protocol_backends(self) -> None:
        """
        Ensures that dependencies for explicitly selected protocols are installed.
        """
        if self.config.http == "httptools" and find_spec("httptools") is None:
            raise RuntimeError("HTTP mode 'httptools' requires the 'httptools' package.")
        if self.config.http == "h2" and find_spec("h2") is None:
            raise RuntimeError("HTTP mode 'h2' requires the 'h2' package.")
        if self.config.http == "h3":
            if find_spec("aioquic") is None:
                raise RuntimeError("HTTP mode 'h3' requires the 'aioquic' package.")
            if not self.config.ssl_certfile or not self.config.ssl_keyfile:
                raise RuntimeError("HTTP mode 'h3' requires both --ssl-certfile and --ssl-keyfile.")
            if self.config.fd is not None or self.config.uds:
                raise RuntimeError("HTTP mode 'h3' does not support --fd or --uds.")

        selected_ws = self.config.effective_ws
        if selected_ws == "none":
            return

        if selected_ws in {"websockets", "websockets-sansio"} and find_spec("websockets") is None:
            raise RuntimeError(f"WebSocket mode '{selected_ws}' requires the 'websockets' package.")

        if selected_ws == "wsproto" and find_spec("wsproto") is None:
            raise RuntimeError("WebSocket mode 'wsproto' requires the 'wsproto' package.")

started property

Indicates if the server has successfully entered the listening state.

serve(sockets=None) async

Public entry point to start the server asynchronously.

PARAMETER DESCRIPTION
sockets

Optional list of pre-bound sockets to use for listening.

TYPE: list[socket] | None DEFAULT: None

Source code in palfrey/server.py
async def serve(self, sockets: list[socket.socket] | None = None) -> None:
    """
    Public entry point to start the server asynchronously.

    Args:
        sockets (list[socket.socket] | None): Optional list of pre-bound
            sockets to use for listening.
    """
    with self.capture_signals():
        await self._serve(sockets=sockets)

run(sockets=None)

Blocks while running the server in a new event loop.

Source code in palfrey/server.py
def run(self, sockets: list[socket.socket] | None = None) -> None:
    """
    Blocks while running the server in a new event loop.
    """
    asyncio.run(self.serve(sockets=sockets))

request_shutdown()

Signals the server to begin the graceful shutdown process.

Source code in palfrey/server.py
def request_shutdown(self) -> None:
    """
    Signals the server to begin the graceful shutdown process.
    """
    self._shutdown_event.set()

capture_signals()

Context manager to intercept OS signals for graceful termination.

Source code in palfrey/server.py
@contextlib.contextmanager
def capture_signals(self):
    """
    Context manager to intercept OS signals for graceful termination.
    """
    if threading.current_thread() is not threading.main_thread():
        yield
        return

    original_handlers = {sig: signal.signal(sig, self.handle_exit) for sig in HANDLED_SIGNALS}
    try:
        yield
    finally:
        for sig, handler in original_handlers.items():
            signal.signal(sig, handler)

    for captured_signal in reversed(self._captured_signals):
        signal.raise_signal(captured_signal)

handle_exit(sig, _frame)

Callback for signal.signal to register captured signals.

Source code in palfrey/server.py
def handle_exit(self, sig: int, _frame: FrameType | None) -> None:
    """
    Callback for signal.signal to register captured signals.
    """
    self._captured_signals.append(sig)
    self._handle_exit_signal(signal.Signals(sig))