ohm icon indicating copy to clipboard operation
ohm copied to clipboard

Update ohm.rb

Open ioquatix opened this issue 4 years ago • 5 comments

Don't lazy initialize mutex, it's not thread safe.

If you intended to have one unique mutex per class (e.g. derived classes have a unique mutex) I believe something like this is required...

class Model
	@mutex = Mutex.new
	
	def self.inherited(derived)
		derived.instance_variable_set(:@mutex, Mutex.new)
	end
	
	def self.mutex
		@mutex
	end
	
	def self.synchronize(&block)
		mutex.synchronize(&block)
	end
end

class MyModel < Model
end

# These are both different:
pp Model.mutex
pp MyModel.mutex

ioquatix avatar Mar 27 '20 00:03 ioquatix

I've tried the patch and I get errors when running the tests. Do you think it has to be an instance variable @mutex or it can be a class variable @@mutex?

soveran avatar Mar 27 '20 16:03 soveran

Do you have CI? e.g. GitHub Actions.

You could experiment with the alternative code I gave you which makes it per-class rather than shared.

ioquatix avatar Mar 27 '20 21:03 ioquatix

I tried it! But I get an error. You don't get errors?

soveran avatar Mar 27 '20 21:03 soveran

I tested the code in isolation to check it behaved the same, but not running tests for this project. Can you set up CI?

ioquatix avatar Mar 27 '20 21:03 ioquatix

Yes, I'll do it tomorrow as it's already late here. Thanks!

soveran avatar Mar 27 '20 21:03 soveran

Bump :)

ioquatix avatar Jan 18 '23 23:01 ioquatix