Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BufReader hang #137307

Closed
BrandonMFong opened this issue Feb 20, 2025 · 2 comments
Closed

BufReader hang #137307

BrandonMFong opened this issue Feb 20, 2025 · 2 comments

Comments

@BrandonMFong
Copy link

I tried this code:

use std::fs::OpenOptions;
use std::io::{BufReader, BufRead};
use std::env;
fn main() -> Result<(), i32> {
    let args: Vec<String> = env::args().collect();
    let Ok(file) = OpenOptions::new().create(true).write(true).append(true).open(&args[1]) else {
        return Err(1);
    };
    for line in BufReader::new(&file).lines() {
        if let Ok(l) = line {
            println!("{}", l);
        }
    }
    Ok(())
}

Steps to reproduce

  1. compile above code in a main.rs file with rustc main.rs
  2. create a text file file.txt with the following contents:
hello
world
this
is
rust
  1. Call the compiled program from step 1 using ./main file.txt
  • Expectations: Each line is printed
  • Actual: Program hangs and doesn't terminate

Meta

rustc --version --verbose:

rustc 1.84.1 (e71f9a9a9 2025-01-27)
binary: rustc
commit-hash: e71f9a9a98b0faf423844bf0ba7438f29dc27d58
commit-date: 2025-01-27
host: aarch64-apple-darwin
release: 1.84.1
LLVM version: 19.1.5
Backtrace

$ lldb -- ./main file
(lldb) target create "./main"
Current executable set to '/Users/brandonmfong/brando/bench/rust-bufreader/main' (arm64).
(lldb) settings set -- target.run-args  "file"
(lldb) r
Process 46581 launched: '/Users/brandonmfong/brando/bench/rust-bufreader/main' (arm64)
Process 46581 stopped
* thread #1, name = 'main', queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
    frame #0: 0x0000000181c2d9b4 libsystem_kernel.dylib`read + 8
libsystem_kernel.dylib`read:
->  0x181c2d9b4 <+8>:  b.lo   0x181c2d9d4    ; <+40>
    0x181c2d9b8 <+12>: pacibsp 
    0x181c2d9bc <+16>: stp    x29, x30, [sp, #-0x10]!
    0x181c2d9c0 <+20>: mov    x29, sp
Target 0: (main) stopped.
(lldb) bt
* thread #1, name = 'main', queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
  * frame #0: 0x0000000181c2d9b4 libsystem_kernel.dylib`read + 8
    frame #1: 0x0000000100027d18 main`std::sys::pal::unix::fs::File::read_buf::h76fb18c0aff24553 [inlined] std::sys::pal::unix::fd::FileDesc::read_buf::h8e6f4b03bc473e99 at fd.rs:171:13 [opt]
    frame #2: 0x0000000100027cf4 main`std::sys::pal::unix::fs::File::read_buf::h76fb18c0aff24553 at fs.rs:1398:9 [opt]
    frame #3: 0x00000001000067b4 main`_$LT$$RF$std..fs..File$u20$as$u20$std..io..Read$GT$::read_buf::h179b3e59e0594d4a(self=0x000000016fdfef10, cursor=BorrowedCursor @ 0x000000016fdfe440) at fs.rs:1108:9
    frame #4: 0x000000010000457c main`std::io::impls::_$LT$impl$u20$std..io..Read$u20$for$u20$$RF$mut$u20$R$GT$::read_buf::hacaa1668b2a83f81(self=0x000000016fdfe4d0, cursor=BorrowedCursor @ 0x000000016fdfe470) at impls.rs:21:9
    frame #5: 0x00000001000047e0 main`std::io::buffered::bufreader::buffer::Buffer::fill_buf::hf544e3f72afb6f7c(self=0x000000016fdfeee8, reader=0x000000016fdfef10) at buffer.rs:146:26
    frame #6: 0x0000000100007dac main`_$LT$std..io..buffered..bufreader..BufReader$LT$R$GT$$u20$as$u20$std..io..BufRead$GT$::fill_buf::h93edbd40a8ded129(self=0x000000016fdfeee8) at bufreader.rs:450:9
    frame #7: 0x00000001000030f0 main`std::io::read_until::h14f65262709e2e4c(r=0x000000016fdfeee8, delim='\n', buf=0x000000016fdfeb80) at mod.rs:2149:35
    frame #8: 0x0000000100004618 main`std::io::BufRead::read_line::_$u7b$$u7b$closure$u7d$$u7d$::hae3b536ad8aee68b(b=0x000000016fdfeb80) at mod.rs:2526:44
    frame #9: 0x000000010000353c main`std::io::append_to_string::h3fe8e6471c731b93(buf=0x000000016fdfeb80, f={closure_env#0}<std::io::buffered::bufreader::BufReader<&std::fs::File>> @ 0x000000016fdfe9f8) at mod.rs:382:15
    frame #10: 0x00000001000045e4 main`std::io::BufRead::read_line::had1f20c26dcef599(self=0x000000016fdfeee8, buf=0x000000016fdfeb80) at mod.rs:2526:18
    frame #11: 0x0000000100007a38 main`_$LT$std..io..Lines$LT$B$GT$$u20$as$u20$core..iter..traits..iterator..Iterator$GT$::next::hf080d473d053abf7(self=0x000000016fdfeee8) at mod.rs:3230:15
    frame #12: 0x0000000100008044 main`main::main::hd98e703fd0168a84 at main.rs:9:17
    frame #13: 0x0000000100005340 main`core::ops::function::FnOnce::call_once::hdb395d4850040d33((null)=(main`main::main::hd98e703fd0168a84 at main.rs:4), (null)=<unavailable>) at function.rs:250:5
    frame #14: 0x0000000100004ae8 main`std::sys::backtrace::__rust_begin_short_backtrace::he7a2c3545445bca9(f=(main`main::main::hd98e703fd0168a84 at main.rs:4)) at backtrace.rs:154:18
    frame #15: 0x0000000100004948 main`std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::hb1f88eeafe7e5f7b at rt.rs:195:18
    frame #16: 0x0000000100021960 main`std::rt::lang_start_internal::hf1bc3d9041088441 [inlined] core::ops::function::impls::_$LT$impl$u20$core..ops..function..FnOnce$LT$A$GT$$u20$for$u20$$RF$F$GT$::call_once::h06c12913b4cd4f07 at function.rs:284:13 [opt]
    frame #17: 0x0000000100021958 main`std::rt::lang_start_internal::hf1bc3d9041088441 [inlined] std::panicking::try::do_call::h0ba61f435b854a36 at panicking.rs:557:40 [opt]
    frame #18: 0x0000000100021954 main`std::rt::lang_start_internal::hf1bc3d9041088441 [inlined] std::panicking::try::he3b4661fc127b5a5 at panicking.rs:520:19 [opt]
    frame #19: 0x0000000100021954 main`std::rt::lang_start_internal::hf1bc3d9041088441 [inlined] std::panic::catch_unwind::h507f39d1e9eab75a at panic.rs:358:14 [opt]
    frame #20: 0x0000000100021954 main`std::rt::lang_start_internal::hf1bc3d9041088441 [inlined] std::rt::lang_start_internal::_$u7b$$u7b$closure$u7d$$u7d$::hf0d9e7c35b7d4638 at rt.rs:174:48 [opt]
    frame #21: 0x0000000100021954 main`std::rt::lang_start_internal::hf1bc3d9041088441 [inlined] std::panicking::try::do_call::h30bd082554236df3 at panicking.rs:557:40 [opt]
    frame #22: 0x0000000100021954 main`std::rt::lang_start_internal::hf1bc3d9041088441 [inlined] std::panicking::try::hae7d311e23f98f93 at panicking.rs:520:19 [opt]
    frame #23: 0x0000000100021954 main`std::rt::lang_start_internal::hf1bc3d9041088441 [inlined] std::panic::catch_unwind::hc32211aeaf32befd at panic.rs:358:14 [opt]
    frame #24: 0x0000000100021954 main`std::rt::lang_start_internal::hf1bc3d9041088441 at rt.rs:174:20 [opt]
    frame #25: 0x0000000100004914 main`std::rt::lang_start::hfc5b2c2fcbe67028(main=(main`main::main::hd98e703fd0168a84 at main.rs:4), argc=2, argv=0x000000016fdff440, sigpipe='\0') at rt.rs:194:17
    frame #26: 0x0000000100008260 main`main + 36
    frame #27: 0x00000001818e3154 dyld`start + 2476
(lldb) thread list
Process 46581 stopped
* thread #1: tid = 0x43b0d62, 0x0000000181c2d9b4 libsystem_kernel.dylib`read + 8, name = 'main', queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
(lldb) 

@BrandonMFong BrandonMFong added the C-bug Category: This is a bug. label Feb 20, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 20, 2025
@theemathas
Copy link
Contributor

theemathas commented Feb 20, 2025

Not a bug in rust. You opened the file without saying .read(true). Therefore, you cannot read from the file. The lines() iterator is repeatedly giving you an error to inform you of failing of reading the file, but you repeatedly ignore this error in an infinite loop.

Also, please read the documentation of append(). https://doc.rust-lang.org/stable/std/fs/struct.OpenOptions.html#method.append

@BrandonMFong
Copy link
Author

rookie mistake on my part. Took this code excerpt out of context from an application I am writing. That's a bug on my part! Thank you!

BrandonMFong added a commit to BrandonMFong/goto that referenced this issue Feb 20, 2025
* The contributor on this [thread](rust-lang/rust#137307) helped me realize I didn't have a `read(true)`. This fixed the issue
@jieyouxu jieyouxu removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants