-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
File instance dropped before leaving scope #54494
Comments
Hmmm, this one is definitely interesting. I don't see it in this code, but is there any chance that the NLL is enabled. The last time I had checked, this still required Does this same code work without issue on stable? |
From the
And you destroy the original File object in the line you get the file descriptor from: let fd = file_result.unwrap().as_raw_fd(); The let fd = {
let temp;
{
temp = file_result.unwrap();
temp.as_raw_fd();
}
} NLL isn't allowed to touch drop order (that's the domain of dropck, not borrowck), so if NLL make the original code work as the author thought it would, that itself would be a bug. The fix to this is add another let statement. let file = file_result.unwrap();
let fd = file.as_raw_fd(); I wonder if there's a way for clippy to catch this specific instance. cc @Manishearth I don't think it's a bug, but I'm not sure if I should be closing issues, so I leave it for somebody else to actually close it if they agree with my analysis. |
I’ll try stable and report back. Thanks for the explanation folks! I don’t remember much if of NLL (Non-lexical lifetimes) so I’ll go Googling in a bit. The point of this issue is: Should “This puts the value expression into a temporary that lasts as long as the let statement.” be a thing, and are there cases where we’ll run afoul? By and large I think no, but when you’re running through an FFI boundary with handles or other values pointing to low-level OS primitives it can get gnarly to figure out why it’s gone wrong. To gauge impact, I spent about 8 hours over about a week with an API that returns EBADF not only for bad file handles but also bad input data (when a device address is wrong). If I hadn’t thought to try |
Looks to still be a problem all the way back with "rustc 1.24.1 (d3ae9a9 2018-02-27)". |
@Havvy I completely missed that in my reading and quickly biased it with other thoughts I had related to how NLL changes might impact drop rules. If your statement about the NLL rules only affecting borrowck and not affecting dropck rules, then my concerns there might very well be unfounded. I agree that this issue should be closed. |
@RandomInsano wrote:
The semantics of how long r-value temporaries live is something that is a source of confusion at times, and has been discussed on various issues. You can see background discussion of this, as well as proposals for revising our semantics here, at links such as:
Having said that, the semantics is very unlikely to change from what it currently is. The rules weren't chosen randomly. The rules are an attempt to satisfy the needs of various use cases, while still being a relatively small set of uniform rules. |
I agree with the analysis by @Havvy above: #54494 (comment) Closing as notabug. |
(Also, I do sympathize that the rules can lead to mistakes at times. It would probably be a good idea to try to improve this section of the reference documentation, along the same lines as proposed here: rust-lang/reference#452 ) |
Thanks for closing. I forgot it was still around. |
It turns out that Rust will happily drop a File after you grab its RawFd if it doesn't have an explicit binding[1]. This causes a bunch of EBADF errors in basically all libpathrs methods which did this incorrectly. [1]: rust-lang/rust#54494 Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
It turns out that Rust will happily drop a File after you grab its RawFd if it doesn't have an explicit binding[1]. This causes a bunch of EBADF errors in basically all libpathrs methods which did this incorrectly. [1]: rust-lang/rust#54494 Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
This is tested on 1.26.0-nightly, but may affect other versions.
I found that if I don't
let
the File instance returned from OpenOptions::open() the File object is drop()ed and the handle is closed before we go out of scope.Here's the
strace
for the above:Should objects be dropped before the scope ends if they aren't used? While it makes a certain amount of sense, this was a hard paper cut for me doing some low-level work.
The text was updated successfully, but these errors were encountered: