microdroid_manager: wait for failure reason to transmit

Writing only 16 bytes at a time makes the race condition less likely to
happen, but, theoretically, it isn't a complete fix. Instead, use
`tcdrain` to block until the serial port driver knows all the data has
been trasmitted.

In the very unlikely case that the failure reason is larger than the
pipe capacity, this could cause a deadlock, so the pipe read is moved
into a new thread.

I was not able to reproduce the race condition either before or after
this change.

Bug: 220071963
Test: packages/modules/Virtualization/android/vm/vm_shell.sh start-microdroid
Change-Id: I3626e942f4a4ec86422c40befb28ee34f2682f22
diff --git a/guest/microdroid_manager/src/main.rs b/guest/microdroid_manager/src/main.rs
index fa089fa..2a77c81 100644
--- a/guest/microdroid_manager/src/main.rs
+++ b/guest/microdroid_manager/src/main.rs
@@ -140,10 +140,10 @@
         Owned(format!("MICRODROID_UNKNOWN_RUNTIME_ERROR|{:?}", err))
     };
 
-    for chunk in death_reason.as_bytes().chunks(16) {
-        // TODO(b/220071963): Sometimes, sending more than 16 bytes at once makes MM hang.
-        OpenOptions::new().read(false).write(true).open(FAILURE_SERIAL_DEVICE)?.write_all(chunk)?;
-    }
+    let mut serial_file = OpenOptions::new().read(false).write(true).open(FAILURE_SERIAL_DEVICE)?;
+    serial_file.write_all(death_reason.as_bytes()).context("serial device write_all failed")?;
+    // Block until the serial port trasmits all the data to the host.
+    nix::sys::termios::tcdrain(&serial_file).context("tcdrain failed")?;
 
     Ok(())
 }