From 98bc4c37ef92368cb446dfe0be4e917ac90d8b16 Mon Sep 17 00:00:00 2001 From: "gordon%netscape.com" Date: Tue, 6 Mar 2001 03:26:13 +0000 Subject: [PATCH] [not part of build] Fix race condition in nsCacheRequest WaitForValidation(). Force !streamBased to set storagePolicy to nsICache:STORE_IN_MEMORY when creating nsCacheSessions. Fix ProcessRequest() to refrain from creating a descriptor for ACCESS_READ requests when no entry is found (found by pavlov). --- netwerk/cache/src/nsCacheRequest.h | 14 +++++++++++--- netwerk/cache/src/nsCacheService.cpp | 6 ++++-- netwerk/cache/src/nsCacheSession.cpp | 1 + 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/netwerk/cache/src/nsCacheRequest.h b/netwerk/cache/src/nsCacheRequest.h index abfa6f3c2ef..c5c0b19e6bf 100644 --- a/netwerk/cache/src/nsCacheRequest.h +++ b/netwerk/cache/src/nsCacheRequest.h @@ -54,6 +54,7 @@ private: SetAccessRequested(accessRequested); if (streamBased) MarkStreamBased(); SetStoragePolicy(storagePolicy); + MarkWaitingForValidation(); } ~nsCacheRequest() @@ -111,6 +112,11 @@ private: nsresult WaitForValidation(void) { + if (!WaitingForValidation()) { // flag already cleared + MarkWaitingForValidation(); // set up for next time + return NS_OK; // early exit; + } + if (!mLock) { mLock = PR_NewLock(); if (!mLock) return NS_ERROR_OUT_OF_MEMORY; @@ -122,14 +128,14 @@ private: return NS_ERROR_OUT_OF_MEMORY; } } - PRStatus status; PR_Lock(mLock); while (WaitingForValidation() && (status == PR_SUCCESS) ) { status = PR_WaitCondVar(mCondVar, PR_INTERVAL_NO_TIMEOUT); } + MarkWaitingForValidation(); // set up for next time PR_Unlock(mLock); - + NS_ASSERTION(status == PR_SUCCESS, "PR_WaitCondVar() returned PR_FAILURE?"); if (status == PR_FAILURE) return NS_ERROR_UNEXPECTED; @@ -138,10 +144,12 @@ private: } void WakeUp(void) { - PR_Lock(mLock); DoneWaitingForValidation(); + if (mLock) { + PR_Lock(mLock); PR_NotifyCondVar(mCondVar); PR_Unlock(mLock); + } } /** diff --git a/netwerk/cache/src/nsCacheService.cpp b/netwerk/cache/src/nsCacheService.cpp index 9d7c9e3a6c0..dcd6684601d 100644 --- a/netwerk/cache/src/nsCacheService.cpp +++ b/netwerk/cache/src/nsCacheService.cpp @@ -282,6 +282,7 @@ nsCacheService::ProcessRequest(nsCacheRequest * request, if (request->mListener) // async exits - validate, doom, or close will resume return rv; + // XXX allocate condvar for request if necessary PR_Unlock(mCacheServiceLock); rv = request->WaitForValidation(); PR_Lock(mCacheServiceLock); @@ -300,8 +301,9 @@ nsCacheService::ProcessRequest(nsCacheRequest * request, } nsCOMPtr descriptor; - - rv = entry->CreateDescriptor(request, accessGranted, getter_AddRefs(descriptor)); + + if (NS_SUCCEEDED(rv)) + rv = entry->CreateDescriptor(request, accessGranted, getter_AddRefs(descriptor)); if (request->mListener) { // Asynchronous // call listener to report error or descriptor diff --git a/netwerk/cache/src/nsCacheSession.cpp b/netwerk/cache/src/nsCacheSession.cpp index 5b16e959378..ab49d1ba815 100644 --- a/netwerk/cache/src/nsCacheSession.cpp +++ b/netwerk/cache/src/nsCacheSession.cpp @@ -38,6 +38,7 @@ nsCacheSession::nsCacheSession(const char * clientID, mStreamBased(streamBased) { NS_INIT_ISUPPORTS(); + if (!streamBased) mStoragePolicy = nsICache::STORE_IN_MEMORY; } nsCacheSession::~nsCacheSession()