Fix for log messages/dynamic UI teardown conflict (#10940)
Fix for log messages/dynamic UI teardown conflict (#10940)

### Problem

It's sometimes possible for the act of writing to console stderr (which pants does pervasively when writing log messages) to interfere with the terminal cleanup that the dynamic UI does when shutting down. When this happens, the characters representing the spinners in the dynamic UI sometimes are left on the screen, and multi-line log messages to stderr are sometimes unexpectedly truncated.

The problem seems to be with the delay between when the code in the `teardown` method sets `self.instance` to `None`, taking the `Instance` out of the `ConsoleUI` struct; and when the future that the `teardown` method returns actually completes, when we can be sure that the dynamic UI has fully shut down. Anything that attempts to call `write_stderr` during this time runs the risk of putting the console into an inconsistent state

### Solution

The solution here is to add a bool `teardown_in_progress` and a mpsc sender/receiver pair on the `ConsoleUI` struct. `teardown_in_progress` is normally false, but when `teardown` is called the first thing that method does is set it to true, then create the future that shuts down the dynamic UI, with an `.and_then` that uses the mpsc sender to send a message as the very last bit of work in the future.

If `teardown_in_progress` is true, when `write_stderr` is called it will block on receiving that send message, then set `teardown_in_progress` to false and print the message to stderr normally. This ensures that nothing tries to print to stderr while the dynamic UI is in the process of shutting down.
pantsbuild/pants,pantsbuild/pants,benjyw/pants,pantsbuild/pants,benjyw/pants,pantsbuild/pants,pantsbuild/pants,jsirois/pants,benjyw/pants,benjyw/pants,pantsbuild/pants,pantsbuild/pants,benjyw/pants,benjyw/pants,jsirois/pants,jsirois/pants,benjyw/pants