docs: the SWMR follow loop stops for a writer that died, not only one that closed
The README example looped while swmr_writer_active(), which never ends when the writer crashed or was killed: libhdf5 clears the SWMR-write flag only on close (the mid-write fixture keeps it set for good). The loop now also stops after a minute without growth, and the README, the swmr_writer_active docs (with the same loop as a compiled no_run doctest) and the design say why. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
@@ -518,17 +518,31 @@ races the writer (a checksum that fails mid-flush) is retried, up to 100
|
||||
attempts as in libhdf5, and never returned torn.
|
||||
|
||||
```rust
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
let file = clawhdf5::File::open_swmr("live.h5")?;
|
||||
let mut ds = file.dataset("samples")?;
|
||||
while file.swmr_writer_active()? {
|
||||
let (mut seen, mut last_growth) = (0, Instant::now());
|
||||
// Stop when the writer closes the file, or when the dataset has not grown
|
||||
// for a minute: a writer that crashed or was killed never clears the
|
||||
// SWMR-write flag, so `swmr_writer_active()` alone can stay true forever.
|
||||
while file.swmr_writer_active()? && last_growth.elapsed() < Duration::from_secs(60) {
|
||||
ds.refresh()?;
|
||||
let n = ds.shape()?[0];
|
||||
// read the new rows ...
|
||||
std::thread::sleep(std::time::Duration::from_millis(100));
|
||||
if n > seen {
|
||||
// read rows seen..n ...
|
||||
(seen, last_growth) = (n, Instant::now());
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
ds.refresh()?; // the final extent
|
||||
```
|
||||
|
||||
`swmr_writer_active()` reads the superblock's SWMR-write flag, which libhdf5
|
||||
clears only when the writer closes the file; a file whose writer died keeps
|
||||
it set (as the mid-write copy in `tests/fixtures/swmr_mid_write.h5` does),
|
||||
so a follower needs its own stop condition, like the idle timeout above.
|
||||
|
||||
Design and limits: [docs/design/swmr.md](docs/design/swmr.md).
|
||||
|
||||
### Python
|
||||
|
||||
Reference in New Issue
Block a user