Skip to content
This repository was archived by the owner on Mar 6, 2024. It is now read-only.

Fix race condition#9

Open
DarthMax wants to merge 1 commit intomadeintandem:masterfrom
DarthMax:fix_check-then-set-race-condition
Open

Fix race condition#9
DarthMax wants to merge 1 commit intomadeintandem:masterfrom
DarthMax:fix_check-then-set-race-condition

Conversation

@DarthMax
Copy link

There is a race condition in the following functions

  • Tabs#increment_counter
  • Tabs#record_value
  • Tabs#start_task

All function check if the requested metric exist. If not they try to create it, but in the mean time it is possible that an other thread has already created it. This results in the following code to fail repeatedly with a Tabs::DuplicateMetricError

5.times.map do 
  Thread.new do
    Tabs.increment_counter("test_counter")
  end
end.each(&:join)

Adding a get_or_create_metric function using a shared mutex prevents this race condition.

@DarthMax
Copy link
Author

I have noticed, that this might not be the best attempt to solve the issue, as it will not prevent the race condition it multiple distinct programs try to access the same metrics. Furthermore will the the mutex slow down all metric operations, although the race condition is only present when the metric is first accessed.

A better approach might be to prevent the increment_counter, record_value and start_task functions from trying to create a not existing metric.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant