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

Multithread error #109

Closed
myxaluch opened this issue Nov 24, 2016 · 2 comments
Closed

Multithread error #109

myxaluch opened this issue Nov 24, 2016 · 2 comments

Comments

@myxaluch
Copy link
Contributor

Hello!

We use your gems in high-load application and faced with a problem :
NoMethodError in function .find_by_unicode/.find_by_alias.After some review, we found problem: when we use Gemoji in multithreading, variable @all defined in first thread, but not fill-in yet. Second thread use .find_by_unicode, pass check on @all and return not defined @unicode_index => NoMethodError.

I create a RSpec test for you:(it don't looks great, but I have small xp in testing yet:) )

require 'emoji'

RSpec.describe "Multithread error" do

  it "raises" do
    Thread.new { 10000.times { Emoji.find_by_unicode 't' }}
    expect { Emoji.find_by_unicode 't' }.to raise_error {NoMethodError}
  end
end

and propose to use Mutex in this functions to avoid this problem.

private
@mutex = Mutex.new
...

def find_by_unicode(unicode)
   @mutex.syncronize do
      unicodes_index[unicode]
  end
end

def find_by_alias(unicode)
   @mutex.syncronize do
      names_index[names]
  end
end

Maybe, you have another ideas for this error :)

@mislav
Copy link
Contributor

mislav commented Nov 24, 2016

Thanks for reporting! Would you submit a pull request that adds a mutex around building the index?

@myxaluch
Copy link
Contributor Author

myxaluch commented Nov 24, 2016

@mislav Of course, but we fix this problem in version 2.1.0. In 3.0.0rc1, you use find_by_unicode inside parse_data_file, and this fix crash all :(. I will think about more useful fix for new version

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

No branches or pull requests

2 participants