From 6232f76f6edd86ca44eef78aa61ee33e33e4e141 Mon Sep 17 00:00:00 2001 From: TalAloni Date: Sat, 1 Feb 2020 14:05:00 +0200 Subject: [PATCH] NTFileStoreTests: Reduce the possibility of a race condition --- SMBLibrary.Tests/NTFileStore/NTFileStoreTests.cs | 1 + SMBLibrary.Win32/NTFileStore/NTDirectoryFileSystem.cs | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/SMBLibrary.Tests/NTFileStore/NTFileStoreTests.cs b/SMBLibrary.Tests/NTFileStore/NTFileStoreTests.cs index 4502a4b..2eca2f6 100644 --- a/SMBLibrary.Tests/NTFileStore/NTFileStoreTests.cs +++ b/SMBLibrary.Tests/NTFileStore/NTFileStoreTests.cs @@ -38,6 +38,7 @@ namespace SMBLibrary.Tests NTStatus status = m_fileStore.NotifyChange(out ioRequest, handle, NotifyChangeFilter.FileName | NotifyChangeFilter.LastWrite | NotifyChangeFilter.DirName, false, 8192, OnNotifyChangeCompleted, null); Assert.IsTrue(status == NTStatus.STATUS_PENDING); + Thread.Sleep(1); m_fileStore.Cancel(ioRequest); m_fileStore.CloseFile(handle); while (m_notifyChangeStatus == null) diff --git a/SMBLibrary.Win32/NTFileStore/NTDirectoryFileSystem.cs b/SMBLibrary.Win32/NTFileStore/NTDirectoryFileSystem.cs index 34c9303..6d719f5 100644 --- a/SMBLibrary.Win32/NTFileStore/NTDirectoryFileSystem.cs +++ b/SMBLibrary.Win32/NTFileStore/NTDirectoryFileSystem.cs @@ -1,4 +1,4 @@ -/* Copyright (C) 2017-2019 Tal Aloni . All rights reserved. +/* Copyright (C) 2017-2020 Tal Aloni . All rights reserved. * * You can redistribute this program and/or modify it under the terms of * the GNU Lesser Public License as published by the Free Software Foundation, @@ -403,6 +403,7 @@ namespace SMBLibrary.Win32 m_pendingRequests.Add(request); // The request has been added, we can now return STATUS_PENDING. requestAddedEvent.Set(); + // There is a possibility of race condition if the caller will wait for STATUS_PENDING and then immediate call Cancel, but this scenario is very unlikely. NTStatus status = NtNotifyChangeDirectoryFile((IntPtr)handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, out request.IOStatusBlock, buffer, (uint)buffer.Length, completionFilter, watchTree); if (status == NTStatus.STATUS_SUCCESS) {