@@ -15,7 +15,7 @@ use crate::types::*;
1515pub struct TaskManager {
1616 task_timeout : Duration ,
1717 running_tasks : Arc < RwLock < HashMap < AgentId , TaskHandle > > > ,
18- task_sender : mpsc:: UnboundedSender < TaskCommand > ,
18+ task_sender : mpsc:: Sender < TaskCommand > ,
1919 #[ allow( dead_code) ]
2020 system_info : Arc < RwLock < System > > ,
2121}
@@ -24,7 +24,9 @@ impl TaskManager {
2424 /// Create a new task manager
2525 pub fn new ( task_timeout : Duration ) -> Self {
2626 let running_tasks = Arc :: new ( RwLock :: new ( HashMap :: new ( ) ) ) ;
27- let ( task_sender, task_receiver) = mpsc:: unbounded_channel ( ) ;
27+ // Bounded channel prevents unbounded memory growth under load.
28+ // 1024 is sufficient for most workloads; submitters block when full.
29+ let ( task_sender, task_receiver) = mpsc:: channel ( 1024 ) ;
2830
2931 let manager = Self {
3032 task_timeout,
@@ -47,15 +49,16 @@ impl TaskManager {
4749 // Store the handle
4850 self . running_tasks . write ( ) . insert ( agent_id, handle. clone ( ) ) ;
4951
50- // Send command to start the task
52+ // Send command to start the task (try_send avoids blocking the caller;
53+ // returns an error if the bounded channel is full).
5154 self . task_sender
52- . send ( TaskCommand :: Start {
55+ . try_send ( TaskCommand :: Start {
5356 task : Box :: new ( task) ,
5457 handle,
5558 } )
5659 . map_err ( |_| SchedulerError :: SchedulingFailed {
5760 agent_id,
58- reason : "Failed to send start command " . into ( ) ,
61+ reason : "Task channel full — backpressure applied " . into ( ) ,
5962 } ) ?;
6063
6164 Ok ( ( ) )
@@ -67,12 +70,12 @@ impl TaskManager {
6770 let handle = self . running_tasks . write ( ) . remove ( & agent_id) ;
6871
6972 if let Some ( handle) = handle {
70- // Send termination command
73+ // Send termination command (try_send to avoid blocking).
7174 self . task_sender
72- . send ( TaskCommand :: Terminate { agent_id, handle } )
75+ . try_send ( TaskCommand :: Terminate { agent_id, handle } )
7376 . map_err ( |_| SchedulerError :: SchedulingFailed {
7477 agent_id,
75- reason : "Failed to send terminate command" . into ( ) ,
78+ reason : "Task channel full — cannot send terminate command" . into ( ) ,
7679 } ) ?;
7780 }
7881
@@ -131,7 +134,7 @@ impl TaskManager {
131134 }
132135
133136 /// Start the task execution loop
134- fn start_task_loop ( & self , mut task_receiver : mpsc:: UnboundedReceiver < TaskCommand > ) {
137+ fn start_task_loop ( & self , mut task_receiver : mpsc:: Receiver < TaskCommand > ) {
135138 let task_timeout = self . task_timeout ;
136139
137140 tokio:: spawn ( async move {
0 commit comments