Worker: Unify worker thread shutdown sequences.
This CL unifies worker thread shutdown sequences to fix a crash and simplify shutdown. <Problem> When termination is requested on the main thread before WorkerThread is initialized on the worker thread, shutdown sequence runs in a different way from regular shutdown sequence: initialization sequence seamlessly switches to shutdown sequence and asks the main thread to destroy WorkerThread (see WorkerThread::initializeOnWorkerThread). This causes a crash in a following scenario: 1) Request to start the worker thread from the main thread. 2) Post a task to the worker thread from the main thread. 3) Request to terminate the worker thread from the main thread. 4) Start initialziation sequence and switch to shutdown sequence on the worker thread. 5) WorkerThread is destroyed on the main thread. 6) The posted task runs on the worker thread and crashes. <Solution> This CL makes the initialization sequence complete regardless of termination request and defer to the regular shutdown task posted from the main thread. Other tasks also posted from the main thread(*) are guaranteed to be drained until the shutdown task runs. <Appendix> Regarding (*), you might wonder if tasks posted from/to the worker thread cannot be guaranteed to be drained until the shutdown tasks run. This case is covered by other mechanism. Let's consider it in following 2 cases: 1) An uninitialized worker thread does not post a task to itself, so there should be no tasks when termination happens before initialization. 2) Otherwise, WorkerBackingThread::shutdown drains all tasks. Therefore, we can ensure that tasks posted from the worker thread also never run after shutdown. BUG=632810, 640843 Review-Url: https://codereview.chromium.org/2280523002 Cr-Commit-Position: refs/heads/master@{#420592}
Showing
Please register or sign in to comment