Skip to content

Commit dde0052

Browse files
lafrikslunny
authored andcommitted
Fix key usage time update if the key is used in parallel for multiple operations (#2185)
1 parent 3702dac commit dde0052

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

models/ssh_key.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,16 +496,21 @@ func UpdatePublicKey(key *PublicKey) error {
496496
// UpdatePublicKeyUpdated updates public key use time.
497497
func UpdatePublicKeyUpdated(id int64) error {
498498
now := time.Now()
499-
cnt, err := x.ID(id).Cols("updated_unix").Update(&PublicKey{
499+
// Check if key exists before update as affected rows count is unreliable
500+
// and will return 0 affected rows if two updates are made at the same time
501+
if cnt, err := x.ID(id).Count(&PublicKey{}); err != nil {
502+
return err
503+
} else if cnt != 1 {
504+
return ErrKeyNotExist{id}
505+
}
506+
507+
_, err := x.ID(id).Cols("updated_unix").Update(&PublicKey{
500508
Updated: now,
501509
UpdatedUnix: now.Unix(),
502510
})
503511
if err != nil {
504512
return err
505513
}
506-
if cnt != 1 {
507-
return ErrKeyNotExist{id}
508-
}
509514
return nil
510515
}
511516

0 commit comments

Comments
 (0)