Open
Description
Hi @javuto ,
We just see a clear race condition for the following two functions.
The race condition occurs when multiple goroutines call these functions concurrently for the same query.
Race Condition Scenario
- Thread A reads query data:
executions = 5
- Thread B reads query data:
executions = 5
(same value, before Thread A updates) - Thread A increments and updates:
executions = 6
- Thread B increments and updates:
executions = 6
(should be 7, but the increment was lost)
However, this does not impact the core functionality—it only affects the display of the executed query count. Rather than fixing the query function, I recommend completely removing these two functions and using an alternative approach to calculate the number.
// IncExecution to increase the execution count for this query
func (q *Queries) IncExecution(name string, envid uint) error {
query, err := q.Get(name, envid)
if err != nil {
return err
}
if err := q.DB.Model(&query).Update("executions", query.Executions+1).Error; err != nil {
return err
}
return nil
}
// IncError to increase the error count for this query
func (q *Queries) IncError(name string, envid uint) error {
query, err := q.Get(name, envid)
if err != nil {
return err
}
if err := q.DB.Model(&query).Update("errors", query.Errors+1).Error; err != nil {
return err
}
return nil
}