Skip to content

Commit 1574ac7

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 rust-lang#140718 (comment)
1 parent 1a95cc6 commit 1574ac7

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
@@ -366,6 +366,28 @@ fn file_lock_blocking_async() {
366366
t.join().unwrap();
367367
}
368368

369+
#[test]
370+
#[cfg(windows)]
371+
fn file_try_lock_async() {
372+
const FILE_FLAG_OVERLAPPED: u32 = 0x40000000;
373+
374+
let tmpdir = tmpdir();
375+
let filename = &tmpdir.join("file_try_lock_async.txt");
376+
let f1 = check!(File::create(filename));
377+
let f2 =
378+
check!(OpenOptions::new().custom_flags(FILE_FLAG_OVERLAPPED).write(true).open(filename));
379+
380+
// Check that shared locks block exclusive locks
381+
check!(f1.lock_shared());
382+
assert_matches!(f2.try_lock().unwrap_err().kind(), ErrorKind::WouldBlock);
383+
check!(f1.unlock());
384+
385+
// Check that exclusive locks block all locks
386+
check!(f1.lock());
387+
assert_matches!(f2.try_lock().unwrap_err().kind(), ErrorKind::WouldBlock);
388+
assert_matches!(f2.try_lock_shared().unwrap_err().kind(), ErrorKind::WouldBlock);
389+
}
390+
369391
#[test]
370392
fn file_test_io_seek_shakedown() {
371393
// 01234567890123

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

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

416416
match result {
417417
Ok(_) => Ok(()),
418-
Err(err)
419-
if err.raw_os_error() == Some(c::ERROR_IO_PENDING as i32)
420-
|| err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) =>
421-
{
418+
Err(err) if err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) => {
422419
Err(TryLockError::WouldBlock)
423420
}
424421
Err(err) => Err(TryLockError::Error(err)),
@@ -440,10 +437,7 @@ impl File {
440437

441438
match result {
442439
Ok(_) => Ok(()),
443-
Err(err)
444-
if err.raw_os_error() == Some(c::ERROR_IO_PENDING as i32)
445-
|| err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) =>
446-
{
440+
Err(err) if err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) => {
447441
Err(TryLockError::WouldBlock)
448442
}
449443
Err(err) => Err(TryLockError::Error(err)),

0 commit comments

Comments
 (0)