Skip to content

Commit b8ac392

Browse files
committed
Library - Get the latest UserContext during Close
Close is called when all events are done and we get a kernel close request. The kernel can issue an event (GetFileInfo) but actually do not wait for its completion and directly send a close. We will have a race condition in ReleaseDokanOpenInfo where the DokanFileInfo might have the GetFileInfo UserContext and not the Cleanup UserContext that is expected to happen before Close. This change enforce to get the latest UserContext we can get from DokanOpenInfo. Yes, it does not prevent the GetFileInfo to be the latest to set UserContext in EventCompletion but that's the best we can do.
1 parent ed61a3e commit b8ac392

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

dokan/dokan.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -963,26 +963,26 @@ VOID CreateDispatchCommon(PDOKAN_IO_EVENT IoEvent, ULONG SizeOfEventInfo, BOOL U
963963
}
964964

965965
VOID ReleaseDokanOpenInfo(PDOKAN_IO_EVENT IoEvent) {
966-
LPWSTR fileNameForClose = NULL;
967-
968966
EnterCriticalSection(&IoEvent->DokanOpenInfo->CriticalSection);
969967
IoEvent->DokanOpenInfo->OpenCount--;
970-
if (IoEvent->DokanOpenInfo->OpenCount < 1) {
971-
if (IoEvent->DokanOpenInfo->FileName) {
972-
fileNameForClose = IoEvent->DokanOpenInfo->FileName;
973-
IoEvent->DokanOpenInfo->FileName = NULL;
974-
}
975-
LeaveCriticalSection(&IoEvent->DokanOpenInfo->CriticalSection);
976-
PushFileOpenInfo(IoEvent->DokanOpenInfo);
977-
IoEvent->DokanOpenInfo = NULL;
978-
if (IoEvent->EventResult) {
979-
// Reset the Kernel UserContext if we can. Close events do not have one.
980-
IoEvent->EventResult->Context = 0;
981-
}
982-
} else {
968+
if (IoEvent->DokanOpenInfo->OpenCount > 0) {
983969
LeaveCriticalSection(&IoEvent->DokanOpenInfo->CriticalSection);
970+
return;
984971
}
985972

973+
LPWSTR fileNameForClose = NULL;
974+
if (IoEvent->DokanOpenInfo->FileName) {
975+
fileNameForClose = IoEvent->DokanOpenInfo->FileName;
976+
IoEvent->DokanOpenInfo->FileName = NULL;
977+
}
978+
IoEvent->DokanFileInfo.Context =IoEvent->DokanOpenInfo->UserContext;
979+
LeaveCriticalSection(&IoEvent->DokanOpenInfo->CriticalSection);
980+
PushFileOpenInfo(IoEvent->DokanOpenInfo);
981+
IoEvent->DokanOpenInfo = NULL;
982+
if (IoEvent->EventResult) {
983+
// Reset the Kernel UserContext if we can. Close events do not have one.
984+
IoEvent->EventResult->Context = 0;
985+
}
986986
if (fileNameForClose) {
987987
if (IoEvent->DokanInstance->DokanOperations->CloseFile) {
988988
IoEvent->DokanInstance->DokanOperations->CloseFile(

0 commit comments

Comments
 (0)