Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Avoid lazy-initialized lock #1577

Merged
merged 2 commits into from
Jul 31, 2024
Merged

fix: Avoid lazy-initialized lock #1577

merged 2 commits into from
Jul 31, 2024

Conversation

olleolleolle
Copy link
Member

@olleolleolle olleolleolle commented Jul 30, 2024

Description

2.10.0 introduced a lock, to synchronize changes to default options.

Issue

The mechanism to obtain the lock tried to wait as long as possible, with a lazy-initialized assignment.

This becomes a race between clients: "Who locks the lock?".

Solution

We avoid this by creating the lock beforehand.

In addition: this changes the lock to be a Mutex, not a Monitor.

A mutex should be fine, the only reason to use a monitor is if you are going to re-enter the same block on the same thread, e.g.

def do_something
 MONITOR.synchronize do
   ...
   yield
   ...
  end
end

def do_something_else
 MONITOR.synchronize do ...

In the above example, the user-supplied block might call another method with the lock.

Generally, "Recursive locks are bad": https://blog.stephencleary.com/2013/04/recursive-re-entrant-locks.html

@olleolleolle olleolleolle merged commit 051a635 into main Jul 31, 2024
7 checks passed
@olleolleolle olleolleolle deleted the avoid-lazy-init branch July 31, 2024 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant