codeplant / simple-navigation

A ruby gem for creating navigations (with multiple levels) for your Rails, Sinatra or Padrino applications. Render your navigation as html list, link list or breadcrumbs.
http://simple-navigation-demo.codeplant.ch/
MIT License
885 stars 136 forks source link

Non-deterministic behaviour when running in multiple threads #206

Closed mkarklins closed 7 years ago

mkarklins commented 7 years ago

I have a rails app running in multiple threads. When the workload is high enough, the rendered navigation produces strange results - as if it had skipped few navigation elements.

I created a gist with reproduction steps here.

An example result is this:

Books, Music <...> Books, Music Books, Music Books, Music, records, record_1, record_2 Books, Music Books, Music Books, Music Books, Music Books, Music

Using simple-navigation 4.0.5

Could someone provide an insight on where lies the problem?

andi commented 7 years ago

Thanks for the gist... simple-navigation has probably never been tested in a multithreaded high workload environment and it has never been an issue so far. Due to time constraints I would appreciate if you could investigate and contribute your findings. Thanks!

mkarklins commented 7 years ago

The way I got around the problem was to simply synchronise the execution across threads:

# initializer
class SimpleNavigationPatch
  def self.mutex
    @mutex ||= Mutex.new
  end
end

# layout.html
SimpleNavigationPatch.mutex.synchronize { render_navigation }

This is not pretty and has performance penalties, but renders the navigation correctly. Since this is working now and doesn't seem like such a major problem, I'll probably won't have the motivation to dig deeper.

Guess we can close this now.

andi commented 7 years ago

Ok, thanks for your feedback.