You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If we File::open() a directory, then wrap it in BufReader, call lines() and then filter_map() only successful reads, the programs never finishes:
use std::fs::File;use std::io::{BufRead,BufReader};fnmain(){let file = File::open(".").unwrap();let reader = BufReader::new(file);let lines = reader.lines();// Comment to make it output infinite errorslet lines = lines.filter_map(|s| s.ok());for line in lines {println!("Line: {:?}", line);}println!("Finished");}
"Finished" is never printed. No "Line: ..." lines are printed. Instead 100% CPU is consumed.
Commenting the filter_map() line makes it output infinite Line: Err(Error { repr: Os { code: 21, message: "Is a directory" } }).
Looks similar to #34703, but not sure it's because of same reasons.
Not sure what would a proper fix would be, but I think I'd prefer lines() to output a single item with error instead of spamming it.
The text was updated successfully, but these errors were encountered:
mkpankov
changed the title
Trying to read directory with BufReader::lines while filtering errors hangs
Trying to read directory with BufReader::lines while filtering errors consumes 100% CPU
Jul 27, 2017
This is the expected behavior of filter_map. It keeps on trying (to read from the directory) until it would succeed (aka never). You might want to use .take_while(|x| x.is_ok()).map(|x| x.unwrap()), or just a for-loop, to stop after the first error.
If we
File::open()
a directory, then wrap it inBufReader
, calllines()
and thenfilter_map()
only successful reads, the programs never finishes:"Finished" is never printed. No "Line: ..." lines are printed. Instead 100% CPU is consumed.
Commenting the
filter_map()
line makes it output infiniteLine: Err(Error { repr: Os { code: 21, message: "Is a directory" } })
.https://play.rust-lang.org/?gist=f6f0fdbcc83b9630b493c025e3432d31&version=stable
Looks similar to #34703, but not sure it's because of same reasons.
Not sure what would a proper fix would be, but I think I'd prefer
lines()
to output a single item with error instead of spamming it.The text was updated successfully, but these errors were encountered: