Error in compute resource model loading providers

http://projects.theforeman.org/issues/13694

This is the second time that I've encountered a situation where a class initializes a variable outside a method that later gets overwritten or itself overwrites the values. It is difficult to reproduce since it seems to be very dependent on sequence of API/UI/etc. This seemed to start happening w/ rails4. Is there a better/best-practice way for this pattern?

I'd expect something like this to be more common

def supported_providers()
unless _supported_providers && _supported_providers['Libvirt']
_supported_providers ||= {}
_supported_providers['Libvirt'] = 'Foreman::Model::Libvirt'
… etc. …
end

_supported_providers
end

Thoughts?

··· --

@thomasmckay

Sounds to me as a race condition. I know rails 4 added a bit more parallel
processing, so I would guess that it is possible.
Maybe we should move the initialization to a proper after_initialize block?

··· On Saturday, February 13, 2016 at 5:41:24 PM UTC+2, Tom McKay wrote: > > http://projects.theforeman.org/issues/13694 > > This is the second time that I've encountered a situation where a class > initializes a variable outside a method that later gets overwritten or > itself overwrites the values. It is difficult to reproduce since it seems > to be very dependent on sequence of API/UI/etc. This seemed to start > happening w/ rails4. Is there a better/best-practice way for this pattern? > > I'd expect something like this to be more common > > def supported_providers() > unless _supported_providers && _supported_providers['Libvirt'] > _supported_providers ||= {} > _supported_providers['Libvirt'] = 'Foreman::Model::Libvirt' > ... etc. ... > end > > _supported_providers > end > > Thoughts? > > -- > > @thomasmckay > >