view_component: Compile deadlock with ActiveJob
I think the fix to this issue #1447 has created a regression because I am now experiencing deadlocks when using view_component and turbo streams.
After a lot of digging I found out that ViewComponent may reset the CompileCache while a request running in another thread is rendering some ViewComponents.
The line that refreshes the cache is this one and It seems to be triggered right after action cable streams a message (So probably after the BoradcastJob is performed). https://github.com/github/view_component/blob/6397e87fcd9e606212493f31127943e13e3e336b/lib/view_component/engine.rb#L136
The fact that the cache is reset makes the rendering on the other thread to recompile the view component event though it is already compiled. Because of that it tries to get a write lock while still holding a read lock thus resulting in a deadlock. (If it already holds a read lock that is because my components are nested)
To sum it up
In thread #1
Send turbo stream message
Finish BroadcastJob
Reset CompileCache
In thread #2
Render view component A <-- (Compile and is now cached)
Render view component A <-- (Knows it is compiled so renders with read lock)
-- Thread #1 has reset cache --
Render view component A <-- (Thinks it needs to compile and try to get write lock)
deadlock because we already have a read lock.
Steps to reproduce
Unfortunately that issue is extremely hard to reproduce because of its nature
Expected behavior
No deadlock
Actual behavior
Deadlock 😦
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 3
- Comments: 16 (7 by maintainers)
Hey everyone! Thanks for all the discussion around the current threadsafety issues. I just submitted a pull request that removes the locking mechanisms entirely (see the PR description for my reasoning). I’d be grateful if folks could try it in their apps and report back.
v2.74.1 has been released, please try it out!
Well in my case I have been using my fork since then and have had no issue whatsoever so maybe we should work on trying to get this one merged 😃
FWIW the hanging server issues I’m seeing also seem to be resolved by downgrading ViewComponent to <= 2.63.0. As 2.64.0 was the first version that included the fix to #1447 I suspect that it is indeed related to that (as @paulhenri-l suggested above).
@spone yes it’s been driving me a little bit crazy trying to get to the root cause of it so I was glad to come across this issue too! Hopefully I haven’t been too hasty in assuming this is the cause of the problems in Lookbook but it certainly seems likely to be related.