@@ -283,6 +283,9 @@ func (a *Agent) Run(
283283 response .SessionRequest = req .Request
284284 if err := stream .Send (response ); err != nil {
285285 log .Printf ("Failed to send session request %s to agent %s" , req .Request .SessionId , a .id )
286+ a .delegate .RevokeAccessToAgent (stream .Context (), a , req )
287+ delete (a .mySessions , req .Key ())
288+ // If this error happens, the stream will likely be broken. Not registering back.
286289 req .agentResponse <- AgentSessionResponse {
287290 Error : err ,
288291 }
@@ -314,7 +317,7 @@ func (a *Agent) RequestKill(ctx context.Context, instanceId int64, sessionId str
314317func (a * Agent ) handleSessionUpdate (ctx context.Context , resp * proto.AgentUpdateRequest ) error {
315318 // TODO: Handle multi-error.
316319 if resp .SessionInitResponse != nil {
317- if err := a .handleSessionInitResponse (resp .SessionInitResponse ); err != nil {
320+ if err := a .handleSessionInitResponse (ctx , resp .SessionInitResponse ); err != nil {
318321 return err
319322 }
320323 }
@@ -326,7 +329,7 @@ func (a *Agent) handleSessionUpdate(ctx context.Context, resp *proto.AgentUpdate
326329 return nil
327330}
328331
329- func (a * Agent ) handleSessionInitResponse (resp * proto.SessionInitResponse ) error {
332+ func (a * Agent ) handleSessionInitResponse (ctx context. Context , resp * proto.SessionInitResponse ) error {
330333 sessionKey := SessionKey {
331334 InstanceId : resp .InstanceId ,
332335 SessionId : resp .SessionId ,
@@ -342,6 +345,7 @@ func (a *Agent) handleSessionInitResponse(resp *proto.SessionInitResponse) error
342345 Error : fmt .Errorf ("%w: %s" , ErrorFailedToStartSession , a .id ),
343346 }
344347 session .Complete (proto .SessionExecutionFinalState_SESSION_EXECUTION_FINAL_STATE_STARTUP_FAILURE )
348+ a .delegate .RevokeAccessToAgent (ctx , a , session )
345349 a .scheduler .AddAgent (a )
346350 a .scheduler .PoolManager .MarkIdle (a .id , a .slots - len (a .mySessions ))
347351 a .receiving = true
0 commit comments