fs4-rs icon indicating copy to clipboard operation
fs4-rs copied to clipboard

Documentation clarification request: How do locks behave in Err scenarios?

Open schneems opened this issue 1 year ago • 1 comments

Hello, thanks for taking this crate on. I'm curious about the error behavior when an Err or Unwrap happens. For example in this code:

    let mut file = fs::OpenOptions::new()
        .append(true)
        .create(true)
        .open(path)
        .unwrap();

    file.lock_exclusive().unwrap();

  // What happens when there's an error thrown here?
  // Is the lock unlocked automatically?

  file.unlock().unwrap()

If the lock is successfully claimed, but an Err is returned via ? is returned before the file.unlock I would assume that Rust would close the file and the lock would therefore be dropped, but I want to confirm that my understanding is correct.

I want to make sure I don't have to do something like make a custom FileUnlockGuard<&File> type or something to ensure that the file is never left in a bad "locked" state by accident.

schneems avatar Jul 23 '24 21:07 schneems