Skip to content

Commit 433ceb3

Browse files
committed
Remove unnecessary handling of ERROR_IO_PENDING
try_lock() and try_lock_shared() do not need to handle these per the discussion in #140718 (comment)
1 parent 2caf278 commit 433ceb3

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

library/std/src/fs/tests.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,28 @@ fn file_lock_blocking_async() {
358358
t.join().unwrap();
359359
}
360360

361+
#[test]
362+
#[cfg(windows)]
363+
fn file_try_lock_async() {
364+
const FILE_FLAG_OVERLAPPED: u32 = 0x40000000;
365+
366+
let tmpdir = tmpdir();
367+
let filename = &tmpdir.join("file_try_lock_async.txt");
368+
let f1 = check!(File::create(filename));
369+
let f2 =
370+
check!(OpenOptions::new().custom_flags(FILE_FLAG_OVERLAPPED).write(true).open(filename));
371+
372+
// Check that shared locks block exclusive locks
373+
check!(f1.lock_shared());
374+
assert_matches!(f2.try_lock().unwrap_err().kind(), ErrorKind::WouldBlock);
375+
check!(f1.unlock());
376+
377+
// Check that exclusive locks block all locks
378+
check!(f1.lock());
379+
assert_matches!(f2.try_lock().unwrap_err().kind(), ErrorKind::WouldBlock);
380+
assert_matches!(f2.try_lock_shared().unwrap_err().kind(), ErrorKind::WouldBlock);
381+
}
382+
361383
#[test]
362384
fn file_test_io_seek_shakedown() {
363385
// 01234567890123

library/std/src/sys/fs/windows.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,7 @@ impl File {
414414

415415
match result {
416416
Ok(_) => Ok(()),
417-
Err(err)
418-
if err.raw_os_error() == Some(c::ERROR_IO_PENDING as i32)
419-
|| err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) =>
420-
{
417+
Err(err) if err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) => {
421418
Err(io::ErrorKind::WouldBlock.into())
422419
}
423420
Err(err) => Err(err),
@@ -439,10 +436,7 @@ impl File {
439436

440437
match result {
441438
Ok(_) => Ok(()),
442-
Err(err)
443-
if err.raw_os_error() == Some(c::ERROR_IO_PENDING as i32)
444-
|| err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) =>
445-
{
439+
Err(err) if err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) => {
446440
Err(io::ErrorKind::WouldBlock.into())
447441
}
448442
Err(err) => Err(err),

0 commit comments

Comments
 (0)