Skip to content

Commit 7e79f91

Browse files
authored
(#691): Flush the Backend buffer
Fixes #689. Normally the `BufWriter` in `LogBackend` is `flush`ed in `drop`, but since all of this runs from a static or a background thread, no destructors are run (reliably at least; not sure about the background thread case). Thus, we need to explicitly `flush` the buffer. I believe this may be the remaining cause of #613. This fix adds a `flush` method to `trait WriteEvent`, which is called after all `Event`s have been written so that we never lose any of the `Event`s. It's implemented by delegating to `Write::flush`, both for `BufWriter` in `LogBackend` and `stderr()` in `DebugBackend` (though that one is usually unbuffered). I discovered this while implementing #687, as the missing `flush` is reliably detected when running the `Backend` on the main thread.
2 parents 7d88216 + 27d7cd4 commit 7e79f91

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

analysis/runtime/src/runtime/backend.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use enum_dispatch::enum_dispatch;
22
use fs_err::{File, OpenOptions};
33
use std::fmt::Debug;
4-
use std::io::BufWriter;
4+
use std::io::{stderr, BufWriter, Write};
55
use std::sync::mpsc::Receiver;
66

77
use bincode;
@@ -14,6 +14,8 @@ use crate::parse::{self, AsStr, GetChoices};
1414
#[enum_dispatch]
1515
pub(super) trait WriteEvent {
1616
fn write(&mut self, event: Event);
17+
18+
fn flush(&mut self);
1719
}
1820

1921
pub(super) trait DetectBackend: Sized {
@@ -56,6 +58,10 @@ impl WriteEvent for DebugBackend {
5658
let mir_loc = self.metadata.get(event.mir_loc);
5759
eprintln!("{:?}: {:?}", mir_loc, event.kind);
5860
}
61+
62+
fn flush(&mut self) {
63+
stderr().flush().unwrap();
64+
}
5965
}
6066

6167
pub struct LogBackend {
@@ -66,6 +72,10 @@ impl WriteEvent for LogBackend {
6672
fn write(&mut self, event: Event) {
6773
bincode::serialize_into(&mut self.writer, &event).unwrap();
6874
}
75+
76+
fn flush(&mut self) {
77+
self.writer.flush().unwrap();
78+
}
6979
}
7080

7181
#[enum_dispatch(WriteEvent)]
@@ -80,9 +90,10 @@ impl Backend {
8090
let done = matches!(event.kind, EventKind::Done);
8191
self.write(event);
8292
if done {
83-
return;
93+
break;
8494
}
8595
}
96+
self.flush();
8697
}
8798

8899
pub fn run(&mut self, rx: Receiver<Event>) {

0 commit comments

Comments
 (0)