@@ -874,6 +874,10 @@ func (g *Gateway) handleIncomingChannel(newChannel ssh.NewChannel) {
874874 }
875875 sessionCtx , sessionCancel := context .WithCancel (g .ctx )
876876 touchSession := g .RegisterPAMSession (forwardConfig .PAMConfig .SessionId , sessionCancel , tlsConn )
877+ defer func () {
878+ sessionCancel ()
879+ g .DeregisterPAMSession (forwardConfig .PAMConfig .SessionId , tlsConn )
880+ }()
877881 forwardConfig .PAMConfig .OnActivity = touchSession
878882 if err := pam .HandlePAMProxy (sessionCtx , tlsConn , & forwardConfig .PAMConfig , g .httpClient ); err != nil {
879883 if err .Error () == "unexpected EOF" {
@@ -882,18 +886,6 @@ func (g *Gateway) handleIncomingChannel(newChannel ssh.NewChannel) {
882886 log .Error ().Err (err ).Msg ("PAM proxy handler ended with error" )
883887 }
884888 }
885- sessionCancel ()
886- // RDP reconnects via a stable .rdp file within the session's validity
887- // window; terminating on disconnect would break that. Idle reaper /
888- // expiry / explicit cancel still end the session normally.
889- isRDP := forwardConfig .PAMConfig .ResourceType == session .ResourceTypeWindows
890- if lastConn := g .DeregisterPAMSession (forwardConfig .PAMConfig .SessionId , tlsConn ); lastConn && ! isRDP {
891- if err := forwardConfig .PAMConfig .SessionUploader .CleanupPAMSession (
892- forwardConfig .PAMConfig .SessionId , "connection_closed" ,
893- ); err != nil {
894- log .Error ().Err (err ).Str ("sessionId" , forwardConfig .PAMConfig .SessionId ).Msg ("Failed to cleanup PAM session" )
895- }
896- }
897889 return
898890 } else if forwardConfig .Mode == ForwardModePAMCancellation {
899891 if err := pam .HandlePAMCancellation (g .ctx , tlsConn , & forwardConfig .PAMConfig , g .httpClient , g .CancelPAMSession ); err != nil {
0 commit comments