@@ -9,6 +9,7 @@ mod windows_pipe {
99
1010 use anyhow:: Context as _;
1111 use tokio:: net:: windows:: named_pipe:: { NamedPipeServer , ServerOptions } ;
12+ use tokio:: sync:: Semaphore ;
1213 use tokio_util:: sync:: CancellationToken ;
1314 use tracing:: { error, info, warn} ;
1415 use win_api_wrappers:: identity:: sid:: Sid ;
@@ -25,13 +26,44 @@ mod windows_pipe {
2526 /// Default pipe name for the package broker.
2627 pub const DEFAULT_PIPE_NAME : & str = r"\\.\pipe\Devolutions.Now.PackageBroker.v1" ;
2728
29+ /// Maximum number of concurrently served pipe connections.
30+ ///
31+ /// Connection setup performs unauthenticated work (client process identity lookups)
32+ /// before any signature gate, so a connection flood could otherwise trigger unbounded
33+ /// work and task spawning. While all slots are taken, no pipe instance is listening and
34+ /// further clients fail to connect until a slot frees up.
35+ const MAX_CONCURRENT_CONNECTIONS : usize = 16 ;
36+
37+ /// Deadline for serving a single pipe connection, from accept to response completion.
38+ ///
39+ /// Each connection serves exactly one HTTP request (`keep_alive` is disabled) and all
40+ /// endpoints respond without blocking on package operations (execution is asynchronous,
41+ /// tracked via the operation tracker), so a healthy exchange completes well within this
42+ /// deadline. Without it, idle clients holding their connection open without sending a
43+ /// request would each pin a connection slot indefinitely and could exhaust the pool.
44+ const CONNECTION_DEADLINE : std:: time:: Duration = std:: time:: Duration :: from_secs ( 30 ) ;
45+
2846 /// Start the named pipe server and accept connections until shutdown.
2947 pub async fn run_pipe_server ( state : Arc < BrokerState > , shutdown : CancellationToken ) -> anyhow:: Result < ( ) > {
3048 let pipe_name = state. pipe_name . clone ( ) ;
3149 info ! ( %pipe_name, "Starting named pipe server" ) ;
3250
51+ let connection_permits = Arc :: new ( Semaphore :: new ( MAX_CONCURRENT_CONNECTIONS ) ) ;
52+
3353 let mut first_instance = true ;
3454 loop {
55+ // Wait for a free connection slot before exposing a new pipe instance,
56+ // bounding the number of concurrently served connections.
57+ let permit = tokio:: select! {
58+ permit = Arc :: clone( & connection_permits) . acquire_owned( ) => {
59+ permit. expect( "the semaphore is never closed" )
60+ }
61+ _ = shutdown. cancelled( ) => {
62+ info!( "Pipe server shutting down" ) ;
63+ return Ok ( ( ) ) ;
64+ }
65+ } ;
66+
3567 // Create a new pipe instance for each connection.
3668 let server = create_pipe_instance ( & pipe_name, first_instance) ?;
3769 first_instance = false ;
@@ -40,18 +72,32 @@ mod windows_pipe {
4072 result = server. connect( ) => {
4173 match result {
4274 Ok ( ( ) ) => {
43- let client = match PipeClient :: from_connected_pipe( & server) {
44- Ok ( client) => client,
45- Err ( error) => {
46- warn!( %error, "Rejected named pipe client" ) ;
47- continue ;
48- }
49- } ;
50- info!( "Client connected to named pipe" ) ;
51- let router = build_router_for_client( Arc :: clone( & state) , client) ;
75+ let state = Arc :: clone( & state) ;
5276 tokio:: spawn( async move {
53- serve_connection( server, router) . await ;
54- info!( "Client disconnected from named pipe" ) ;
77+ // The permit is held for the lifetime of the connection task.
78+ let _permit = permit;
79+
80+ let serve = async move {
81+ // Capture the client identity off the accept loop so a slow
82+ // lookup cannot stall accepting other connections.
83+ let client = match PipeClient :: from_connected_pipe( & server) {
84+ Ok ( client) => client,
85+ Err ( error) => {
86+ warn!( %error, "Rejected named pipe client" ) ;
87+ return ;
88+ }
89+ } ;
90+ info!( "Client connected to named pipe" ) ;
91+ let router = build_router_for_client( state, client) ;
92+ serve_connection( server, router) . await ;
93+ info!( "Client disconnected from named pipe" ) ;
94+ } ;
95+
96+ // Enforce a deadline so idle or slow clients cannot pin
97+ // a connection slot indefinitely.
98+ if tokio:: time:: timeout( CONNECTION_DEADLINE , serve) . await . is_err( ) {
99+ warn!( "Closed named pipe connection: deadline exceeded" ) ;
100+ }
55101 } ) ;
56102 }
57103 Err ( error) => {
0 commit comments