Fix an occasional CHECK fail in ApiTest.RunDeadTask
Fix an occasional CHECK fail in ApiTest.RunDeadTask

third_party/ghost/lib/ghost.cc:117(675352) CHECK FAILED: can_free() != 0
[0 == 0]
errno: 116 [Stale file handle]
PID 675352 Backtrace:
[0] 0x562c7dcff5e7 : ghost::Exit()
[1] 0x562c7dcfa73b : ghost::LocalStatusWord::Free()
[2] 0x562c7dc8f4e0 : ghost::Task<>::~Task()
[3] 0x562c7dc8c9aa : ghost::(anonymous
namespace)::DeadAgent::AgentThread()
[4] 0x562c7dcc7aaf : ghost::LocalAgent::ThreadBody()
[5] 0x562c7dcc83ba : std::__u::__thread_proxy<>()
[6] 0x7efcd084b7d9 : start_thread
[7] 0x7efcd07c369f : clone

This is an unintentional side-effect of the previous change  where we
started exiting from the agent loop immediately after observing Finished().

This fixed the hang during agent termination but introduced a race
between the exiting task and the destructor. If it happens that the
destructor runs before the task has made its way to the 'task_dead'
callback then we will encounter a CHECK fail because the status_word
cannot be freed.

Tested: 10000 iterations of ApiTest.RunDeadTask

google/ghost-userspace,google/ghost-userspace,google/ghost-userspace,google/ghost-userspace