-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix timing issue of Thread#kill and sleep
In some cases, a thread isn't correctly killed by Thread#kill. If that is the case, Thread#join on that thread leads to the deadlock. Consider the following example: thread = Thread.new do sleep end thread.kill # actually, don't kill thread.join # thread lives forever; deadlock Thread#kill does 2 things to kill a thread: 1. prepares the thread to be terminated and 2. wakes up the thread. Most of time, the wake-up is executed after the sleep in the killed thread. However, it is possible the wake-up is executed BEFORE the sleep in some rare cases. If that is the case, the wake-up does nothing because the thread is still waking up at that time. Then, the thread begins to sleep forever. As a result, Thread#join never returns. To avoid such a deadlock, Thread#sleep must check to terminate or not before actually sleeping. In addition, it must be asssured that the wake-up isn't executed AFTER the check BEFORE the sleep. The follwing diagrams illustrate the good and bad timings. Good timings: Time ------------------------------------------------------> main thread prepare - wakeup (no-op) | new thread | check - no sleep - terminate Time ------------------------------------------------------> main thread | prepare - wake-up | new thread check - sleep | | terminate Time ------------------------------------------------------> main thread | prepare | | wake-up | new thread check | | sleep | | terminate Bad timing: Time ------------------------------------------------------> main thread | prepare - wake-up (no-op) | new thread check | | sleep forever To fix the deadlock, this commit makes check_async be called before sleeping inside the park mutex.
- Loading branch information
Showing
5 changed files
with
18 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters