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

Thread safe? #8

Closed
mkdynamic opened this issue Jun 22, 2023 · 7 comments
Closed

Thread safe? #8

mkdynamic opened this issue Jun 22, 2023 · 7 comments

Comments

@mkdynamic
Copy link
Contributor

Is it possible that Tiktoken.encoding_for_model is not thread safe?

@IAPark
Copy link
Owner

IAPark commented Sep 21, 2023

I'm not really sure how I would check for that. I think it should be? No unsafe rust features were used and I'm aware of anything wrong. Did you see something specific

@mkdynamic
Copy link
Contributor Author

I haven’t dug in deeply, but in a fairly heavily multi threaded app calling that method a lot results in some kind of deadlock for me (verified this with gdb, thread was stuck on that call).

My fix was to synchronize that call in a Mutex. And this solves the issue.

@IAPark
Copy link
Owner

IAPark commented Sep 22, 2023

Interesting, this is a very low priority for me, but maybe I'll try digging into it one of these weekends

@viamin
Copy link

viamin commented Feb 18, 2024

It looks like it's this usage of memoization inside a class method in lib/tiktoken_ruby/encoding.rb:

  def self.for_name_cached(encoding)
    @encodings ||= {}
    @encodings[encoding.to_sym] ||= Tiktoken::Encoding.for_name(encoding)
  end

Memoization inside a class method is not thread-safe. The easy fix might be to use a thread-safe memoization library like memo_wise

@bdegomme
Copy link

Hi @IAPark, would you consider adding a warning in the readme saying that the library is not thread safe? This leads to tricky deadlocks (I had the same as this one sidekiq/sidekiq#6084, took my 2-3 days to understand).
Unfortunately I don't think I could fix this myself 😬
Thank you for the great gem!

@mkdynamic
Copy link
Contributor Author

FWIW I think the fix is fairly simple, if the memoization is indeed the source of the thread safety issue.

Untested patch if it helps:
#30

@IAPark
Copy link
Owner

IAPark commented Apr 5, 2024

This should now be fixed in the latest version

@IAPark IAPark closed this as completed Apr 5, 2024
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

4 participants