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

Windows 7: std::io::last_os_error is set after printing to stdout #53155

Closed
nicokoch opened this issue Aug 7, 2018 · 11 comments · Fixed by #91460
Closed

Windows 7: std::io::last_os_error is set after printing to stdout #53155

nicokoch opened this issue Aug 7, 2018 · 11 comments · Fixed by #91460
Labels
C-bug Category: This is a bug. O-windows Operating system: Windows T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@nicokoch
Copy link
Contributor

nicokoch commented Aug 7, 2018

This happens on Windows 7, cannot test on newer Windows.
Basically, something wrong happens with file handles when using the println! macro, not sure what exactly.

Minimal repro:

use std::io;
fn main() {
    println!("{:?}", io::Error::last_os_error());
    println!("{:?}", io::Error::last_os_error());
}

Actual output:

Os { code: 0, kind: Other, message: "Der Vorgang wurde erfolgreich beendet." }
Os { code: 6, kind: Other, message: "Das Handle ist ungültig." }

Expected output:

Os { code: 0, kind: Other, message: "Der Vorgang wurde erfolgreich beendet." }
Os { code: 0, kind: Other, message: "Der Vorgang wurde erfolgreich beendet." }

Sorry for German localization in the error messages.

I noticed this because of some obscure behaviour in the windows API (a function returned != 0, but LastError wasn't set accordingly).

cc @retep998
cc retep998/winapi-rs#658

@nicokoch
Copy link
Contributor Author

nicokoch commented Aug 7, 2018

Additional info: I am using stable rust 1.28

@hellow554
Copy link
Contributor

Works for me on Windows 10:

  • 1.27.2
  • 1.28
  • beta
  • nightly

Can't test Windows 7 :( sorry

@hellow554
Copy link
Contributor

Does it occur more than once? (e.g. can you reproduce it?)

@nicokoch
Copy link
Contributor Author

nicokoch commented Aug 7, 2018

Yes, this happens every time I execute the program

@kennytm
Copy link
Member

kennytm commented Aug 7, 2018

("Das Handle ist ungültig" means "The handle is invalid" in English)

@kennytm kennytm added O-windows Operating system: Windows T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Aug 7, 2018
@sfackler
Copy link
Member

sfackler commented Aug 7, 2018

@retep998
Copy link
Member

retep998 commented Aug 7, 2018

Windows system functions generally guarantee that they will set the last error when they fail, but offer no guarantees as to the state of the last error when they succeed (some may clear the error, some may leave it alone, some may change it).

The only action that Rust could take here is for any libstd function that calls system functions to clear the last error when it returns success.

@nicokoch
Copy link
Contributor Author

nicokoch commented Aug 8, 2018

@sfackler I'm using git bashon windows 7 (which is just the mingw terminal if I'm not mistaken).
Probably that resolves to Pipe in the call you mentioned.

Maybe we should SetLastError(0) in that branch?

I understand this is not really a bug (if the function errors, we have a pipe, otherwise a terminal), but perhaps we can reduce the confusion if we reset the OS error in the former case.

@retep998
Copy link
Member

retep998 commented Aug 8, 2018

Personally I'd rather that Rust offer no guarantees about the result of Error::last_os_error() when called after any libstd function that calls some system function. It should only be used directly after calling a system function yourself and only when that function is documented to have set the last error.

There is a case to be made here about documenting this though.

@steveklabnik
Copy link
Member

Triage:

There is a case to be made here about documenting this though.

I agree, though we should get @rust-lang/libs signoff before re-categorizing this bug.

@Zymlex
Copy link

Zymlex commented Jan 14, 2021

Same problem.

As I see line break affects.

Normal:

let mut stdout = stdout();
stdout.write_fmt(format_args!("{}", unsafe { winapi::um::errhandlingapi::GetLastError() }));
stdout.write_fmt(format_args!("{}", unsafe { winapi::um::errhandlingapi::GetLastError() }));

Normal:

print!("{}", Error::last_os_error());
print!("{}", Error::last_os_error());

Bug:

let mut stdout = stdout();
stdout.write_fmt(format_args!("{}\n", unsafe { winapi::um::errhandlingapi::GetLastError() }));
stdout.write_fmt(format_args!("{}", unsafe { winapi::um::errhandlingapi::GetLastError() }));

Bug:

println!("{}", Error::last_os_error());
println!("{}", Error::last_os_error());

Windows 10
1.51.0-nightly
1.49.0-stable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. O-windows Operating system: Windows T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants