alicorn-systems/v8-adapter

Issues with multiple threads and V8Runtime cache

Closed this issue · 3 comments

When using this adapter with multiple threads, and therefore multiple V8 instances, there are two problems I see with the design.

  1. There is no way to remove the V8 item from the cache, so this will grow indefinitely if used in a multi-threaded server installation.
  2. Once a V8 object is released, the equals function throws an exception. Most of the time, this isn't a problem, because there isn't a hash collision. However, with enough repetition there will be a hash collision and the equals method will be called causing a problem.

Seems like we need a way to release things from this cache either automatically or manually when releasing the V8 object.

caer commented

Good catch! I've modified the cache to use a WeakHashMap, causing a V8 instance's corresponding cache to be automatically evicted once the V8 instance is garbage collected.

Note that this will not detect when a V8 object is released; however, I have a suspicion that this may be sufficient for your use case (since I'm assuming your V8 objects end up being garbage collected shortly after they're released).

As a side note, I'm not sure what your exact use case is, but you might take a look at the ConcurrentV8 class and see if it would help you with multithreaded JS runtimes. ;D

Thanks for the quick response, and explanation!
Thanks for the tip on ConcurrentV8. I don't think it's the right application in our particular use case though. We are actually executing multiple threads concurrently, but we don't want them to share state.

caer commented

No problem; thank you for pointing out this issue! And you're right; in your case, a ConcurrentV8 wouldn't be any help at all.