Skip to content

Race condition for function IncExecution and IncError #634

Open
@zhuoyuan-liu

Description

@zhuoyuan-liu

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

  1. Thread A reads query data: executions = 5
  2. Thread B reads query data: executions = 5 (same value, before Thread A updates)
  3. Thread A increments and updates: executions = 6
  4. 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
}

Metadata

Metadata

Assignees

Labels

queriesOn-demand queries related issues🐛 bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions