codeplant/simple-navigation

Non-deterministic behaviour when running in multiple threads

mkarklins opened this issue · 3 comments

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

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!

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

Ok, thanks for your feedback.