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

Art threads suspension causes deadlock #11

Open
canyie opened this issue Feb 13, 2021 · 3 comments
Open

Art threads suspension causes deadlock #11

canyie opened this issue Feb 13, 2021 · 3 comments
Labels
bug Something isn't working help wanted I'm powerless to fix this, extra help is needed

Comments

@canyie
Copy link
Owner

canyie commented Feb 13, 2021

(This problem is also described in the README.)
Background:

  1. In the bridge_jump_trampoline, we modified some registers to save our own values, so we have to save the original values from these registers; we can't allocate memory (stack and heap) here, so we use pre-allocated memory; but when multiple threads execute concurrently, because the memory is shared, its value may be erased by other threads, so we designed a spin lock, the thread executing here will block until it successfully acquires the lock.
  2. In some cases, art needs to suspend the execution of all threads (such as GC). When the thread executes to the checkpoint, it will be suspended. When all threads are suspended, the GC can begin.

Imagine this situation:

  1. Thread A and thread B acquire the lock at the same time, A acquires the lock and continues to execute, B blocks here until A releases the lock.
  2. At this time, art needs to suspend all threads. When A executes to checkpoint, it is blocked, waiting for B to execute checkpoint.
  3. A waits for B to execute the checkpoint and B waits for A to release the lock. They cannot continue to execute; and because other threads are also suspended, the runtime cannot continue to work.

We have three ways to solve it:

  1. When the thread fails to acquire the lock, explicitly check whether the thread needs to be suspended and actively enter the checkpoint. Tested and failed, the thread crashes at Thread::VerifyStack().
  2. Since the thread waiting for the lock has actually been suspended, we can hook certain system functions and make it ignore the thread; however, this method is hard to implement and may cause unknown problems.
  3. Prevent the thread holding the lock from being suspended until it releases the lock. This can be achieved by hooking certain system functions.

We will try to solve the problem, and suggestions are welcome!

@canyie canyie added the bug Something isn't working label Feb 13, 2021
@vegedreamgagnoa
Copy link

has this been solved yet? @canyie

@huigll
Copy link

huigll commented Jul 30, 2024

https://juejin.cn/post/7372126591215222835
这里有篇文章说了这个问题,不知道有没有帮助.

@canyie
Copy link
Owner Author

canyie commented Jul 30, 2024

https://juejin.cn/post/7372126591215222835 这里有篇文章说了这个问题,不知道有没有帮助.

这篇文章我看过的,但是要实现这种解决方法需要我把这部分逻辑几乎重写一遍,而且从 Android 4.4 ~ 15,每个版本的实现都需要重新追一遍以确保兼容性,我现在的心力不足以支撑我这样做

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted I'm powerless to fix this, extra help is needed
Projects
None yet
Development

No branches or pull requests

3 participants