Skip to content

Commit

Permalink
Prevent deadlock during command deletion in MySQL (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin Lee authored Dec 22, 2022
1 parent 60072b4 commit c90f186
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion storage/mysql/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,17 @@ func (m *MySQLStorage) EnqueueCommand(ctx context.Context, ids []string, cmd *md
}

func (s *MySQLStorage) deleteCommand(ctx context.Context, tx *sql.Tx, id, uuid string) error {
// delete command result (i.e. NotNows) and this queued command
// first, place a record lock on the command so that multiple devices
// trying to each delete it do not race
_, err := tx.ExecContext(
ctx, `
SELECT command_uuid FROM commands WHERE command_uuid = ? FOR UPDATE;
`,
uuid,
)
// delete command result (i.e. NotNows) and this queued command
_, err = tx.ExecContext(
ctx, `
DELETE
q, r
FROM
Expand Down

0 comments on commit c90f186

Please sign in to comment.