Skip to content

Fix race condition in IncExecution and IncError#810

Merged
javuto merged 1 commit into
jmpsec:mainfrom
kimjune01:fix/race-condition-inc-execution-error
May 12, 2026
Merged

Fix race condition in IncExecution and IncError#810
javuto merged 1 commit into
jmpsec:mainfrom
kimjune01:fix/race-condition-inc-execution-error

Conversation

@kimjune01
Copy link
Copy Markdown
Contributor

The previous implementation had a race condition where multiple goroutines could read the same counter value, increment it, and write back, causing lost updates under concurrent load.

The read-modify-write cycle allowed this scenario:

  1. Thread A reads: executions = 5
  2. Thread B reads: executions = 5 (same value)
  3. Thread A writes: executions = 6
  4. Thread B writes: executions = 6 (should be 7)

Fixed by using atomic database operations with gorm.Expr to perform the increment directly in SQL:

UPDATE distributed_queries SET executions = executions + 1 WHERE ...

This ensures thread-safe atomic increments even under concurrent load, matching the pattern used for IncExpected in the same file.

The previous implementation had a race condition where multiple
goroutines could read the same value, increment it, and write back,
causing lost updates.

Before:
1. Thread A reads: executions = 5
2. Thread B reads: executions = 5 (same value)
3. Thread A updates: executions = 6
4. Thread B updates: executions = 6 (should be 7, increment lost)

Fixed by using atomic database operations with gorm.Expr to perform
the increment directly in SQL, avoiding the read-modify-write cycle.
The new implementation uses:
  UPDATE distributed_queries SET executions = executions + 1 WHERE ...

This ensures thread-safe atomic increments even under concurrent load.

Fixes jmpsec#634
@javuto javuto self-assigned this May 12, 2026
@javuto javuto added osctrl-tls osctrl-tls related changes queries On-demand queries related issues 🚧 bugfix Fix for an existing bug labels May 12, 2026
@javuto
Copy link
Copy Markdown
Collaborator

javuto commented May 12, 2026

Fixes #634

Thank you!

@javuto javuto merged commit 49ea58b into jmpsec:main May 12, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🚧 bugfix Fix for an existing bug osctrl-tls osctrl-tls related changes queries On-demand queries related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants