@@ -1205,6 +1205,10 @@ func (s *sessionRpcServer) AddAutopilotSession(ctx context.Context,
12051205 return nil , fmt .Errorf ("error creating new session: %v" , err )
12061206 }
12071207
1208+ fail := func (err error ) error {
1209+ return err
1210+ }
1211+
12081212 // If this session is being linked to a previous one, then we need to
12091213 // use the previous session's local private key to sign the new
12101214 // session's public key in order to prove to the Autopilot server that
@@ -1278,8 +1282,19 @@ func (s *sessionRpcServer) AddAutopilotSession(ctx context.Context,
12781282 privacyFlags .Serialize (),
12791283 )
12801284 if err != nil {
1281- return nil , fmt .Errorf ("error registering session with " +
1282- "autopilot server: %v" , err )
1285+ // If we tried to link to a previous session, we delete the
1286+ // newly created session in the case of errors to avoid having
1287+ // non-revoked sessions lying around.
1288+ if len (req .LinkedGroupId ) != 0 {
1289+ deleteErr := s .cfg .db .DeleteReservedSession (
1290+ ctx , sess .ID ,
1291+ )
1292+ log .Errorf ("error deleting session after failed " +
1293+ "linking attempt: %v" , deleteErr )
1294+ }
1295+
1296+ return nil , fail (fmt .Errorf ("error registering session with " +
1297+ "autopilot server: %v" , err ))
12831298 }
12841299
12851300 err = s .cfg .db .UpdateSessionRemotePubKey (ctx , sess .ID , remoteKey )
0 commit comments