@@ -189,7 +189,8 @@ func (cmd *WorkerCMD) Run(ctx *cliContext.Context) error {
189189// subscribeFileStaging subscribes to NATS file staging subjects for this node.
190190func (cmd * WorkerCMD ) subscribeFileStaging (natsClient messaging.MessagingClient , nodeID string ) error {
191191 // Create FileManager with same S3 config as the frontend
192- s3Store , err := storage .NewS3Store (storage.S3Config {
192+ // TODO: propagate a caller-provided context once WorkerCMD carries one
193+ s3Store , err := storage .NewS3Store (context .Background (), storage.S3Config {
193194 Endpoint : cmd .StorageURL ,
194195 Region : cmd .StorageRegion ,
195196 Bucket : cmd .StorageBucket ,
@@ -383,9 +384,10 @@ func (s *backendSupervisor) startBackend(backend, backendPath string) (string, e
383384 port = s .nextPort
384385 s .nextPort ++
385386 }
386- addr := fmt .Sprintf ("0.0.0.0:%d" , port )
387+ bindAddr := fmt .Sprintf ("0.0.0.0:%d" , port )
388+ clientAddr := fmt .Sprintf ("127.0.0.1:%d" , port )
387389
388- proc , err := s .ml .StartProcess (backendPath , backend , addr )
390+ proc , err := s .ml .StartProcess (backendPath , backend , bindAddr )
389391 if err != nil {
390392 s .mu .Unlock ()
391393 return "" , fmt .Errorf ("starting backend process: %w" , err )
@@ -394,17 +396,17 @@ func (s *backendSupervisor) startBackend(backend, backendPath string) (string, e
394396 s .processes [backend ] = & backendProcess {
395397 proc : proc ,
396398 backend : backend ,
397- addr : addr ,
399+ addr : clientAddr ,
398400 }
399- xlog .Info ("Backend process started" , "backend" , backend , "addr" , addr )
401+ xlog .Info ("Backend process started" , "backend" , backend , "addr" , clientAddr )
400402
401403 // Capture reference before unlocking for race-safe health check.
402404 // Another goroutine could stopBackend and recycle the port while we poll.
403405 bp := s .processes [backend ]
404406 s .mu .Unlock ()
405407
406408 // Wait for the gRPC server to be ready
407- client := grpc .NewClientWithToken (addr , false , nil , false , s .cmd .RegistrationToken )
409+ client := grpc .NewClientWithToken (clientAddr , false , nil , false , s .cmd .RegistrationToken )
408410 for range 20 {
409411 time .Sleep (200 * time .Millisecond )
410412 ctx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
@@ -417,27 +419,34 @@ func (s *backendSupervisor) startBackend(backend, backendPath string) (string, e
417419 if ! exists || currentBP != bp {
418420 return "" , fmt .Errorf ("backend %s was stopped during startup" , backend )
419421 }
420- xlog .Debug ("Backend gRPC server is ready" , "backend" , backend , "addr" , addr )
421- return addr , nil
422+ xlog .Debug ("Backend gRPC server is ready" , "backend" , backend , "addr" , clientAddr )
423+ return clientAddr , nil
422424 }
423425 cancel ()
424426 }
425427
426- xlog .Warn ("Backend gRPC server not ready after waiting, proceeding anyway" , "backend" , backend , "addr" , addr )
427- return addr , nil
428+ xlog .Warn ("Backend gRPC server not ready after waiting, proceeding anyway" , "backend" , backend , "addr" , clientAddr )
429+ return clientAddr , nil
428430}
429431
430432// stopBackend stops a specific backend's gRPC process.
431433func (s * backendSupervisor ) stopBackend (backend string ) {
432434 s .mu .Lock ()
433- defer s .mu .Unlock ()
434-
435435 bp , ok := s .processes [backend ]
436436 if ! ok || bp .proc == nil {
437+ s .mu .Unlock ()
437438 return
438439 }
440+ // Clean up map and recycle port while holding lock
441+ delete (s .processes , backend )
442+ if _ , portStr , err := net .SplitHostPort (bp .addr ); err == nil {
443+ if p , err := strconv .Atoi (portStr ); err == nil {
444+ s .freePorts = append (s .freePorts , p )
445+ }
446+ }
447+ s .mu .Unlock ()
439448
440- // Best-effort Free() to release GPU memory
449+ // Network I/O outside the lock
441450 client := grpc .NewClientWithToken (bp .addr , false , nil , false , s .cmd .RegistrationToken )
442451 if freeFunc , ok := client .(interface { Free () error }); ok {
443452 xlog .Debug ("Calling Free() before stopping backend" , "backend" , backend )
@@ -450,13 +459,6 @@ func (s *backendSupervisor) stopBackend(backend string) {
450459 if err := bp .proc .Stop (); err != nil {
451460 xlog .Error ("Error stopping backend process" , "backend" , backend , "error" , err )
452461 }
453- // Recycle the port for future backends
454- if _ , portStr , err := net .SplitHostPort (bp .addr ); err == nil {
455- if p , err := strconv .Atoi (portStr ); err == nil {
456- s .freePorts = append (s .freePorts , p )
457- }
458- }
459- delete (s .processes , backend )
460462}
461463
462464// stopAllBackends stops all running backend processes.
0 commit comments