From c9911694ff0e22ca0c87fcabdfd85167e2c01dc3 Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 8 Jan 2011 23:27:33 +0000 Subject: [PATCH] corrected a bug that caused file descriptors to never get closed. Adding some tracing comments. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3958 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/ft/ftdatamultiplex.cc | 44 +++++++++++++++++++------ libretroshare/src/ft/ftfileprovider.cc | 21 ++++++++++-- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/libretroshare/src/ft/ftdatamultiplex.cc b/libretroshare/src/ft/ftdatamultiplex.cc index 2c820b4ef..7f688b391 100644 --- a/libretroshare/src/ft/ftdatamultiplex.cc +++ b/libretroshare/src/ft/ftdatamultiplex.cc @@ -755,12 +755,21 @@ void ftDataMultiplex::deleteUnusedServers() for(std::map::iterator sit(mServers.begin());sit != mServers.end();) if(sit->second->purgeOldPeers(now,10)) { -#ifdef SERVER_DEBUG - std::cout << "info.lastTS = " << info.lastTS << ", now=" << now << std::endl ; +#ifdef MPLEX_DEBUG + std::cerr << "ftDataMultiplex::deleteUnusedServers(): provider " << (void*)sit->second << " has no active peers. Removing. Now=" << now << std::endl ; #endif // We don't delete servers that are clients at the same time ! if(dynamic_cast(sit->second) == NULL) + { +#ifdef MPLEX_DEBUG + std::cerr << "ftDataMultiplex::deleteUnusedServers(): deleting file provider " << (void*)sit->second << std::endl ; +#endif delete sit->second; + } +#ifdef MPLEX_DEBUG + else + std::cerr << "ftDataMultiplex::deleteUnusedServers(): " << (void*)sit->second << " was not deleted because it's also a file creator." << std::endl ; +#endif std::map::iterator tmp(sit); ++tmp ; @@ -823,18 +832,33 @@ bool ftDataMultiplex::handleSearchRequest(const std::string& peerId, const std:: if (mSearch->search(hash, hintflags, info)) { -#ifdef MPLEX_DEBUG - std::cerr << "ftDataMultiplex::handleSearchRequest("; - std::cerr << " Found Local File, sharing..."; - std::cerr << std::endl; -#endif - /* setup a new provider */ RsStackMutex stack(dataMtx); /******* LOCK MUTEX ******/ - ftFileProvider *provider = new ftFileProvider(info.path, info.size, hash); + // We might already have a file provider, if two requests have got stacked in the request queue. So let's + // check that before. - mServers[hash] = provider; +#ifdef MPLEX_DEBUG + std::cerr << "ftDataMultiplex::handleSearchRequest("; + std::cerr << " Found Local File, sharing..."; +#endif + std::map::const_iterator it = mServers.find(hash) ; + ftFileProvider *provider ; + + if(it == mServers.end()) + { + provider = new ftFileProvider(info.path, info.size, hash); + mServers[hash] = provider; +#ifdef MPLEX_DEBUG + std::cerr << " created new file provider " << (void*)provider << std::endl; +#endif + } + else + { +#ifdef MPLEX_DEBUG + std::cerr << " re-using existing file provider " << (void*)it->second << std::endl; +#endif + } return true; } diff --git a/libretroshare/src/ft/ftfileprovider.cc b/libretroshare/src/ft/ftfileprovider.cc index f91688ba5..5252d3918 100644 --- a/libretroshare/src/ft/ftfileprovider.cc +++ b/libretroshare/src/ft/ftfileprovider.cc @@ -14,7 +14,6 @@ * #define DEBUG_TRANSFERS 1 // TO GET TIMESTAMPS of DATA READING ********/ - #ifdef DEBUG_TRANSFERS #include "util/rsprint.h" #include @@ -34,13 +33,17 @@ ftFileProvider::ftFileProvider(std::string path, uint64_t size, std::string hash #endif } -ftFileProvider::~ftFileProvider(){ +ftFileProvider::~ftFileProvider() +{ #ifdef DEBUG_FT_FILE_PROVIDER - std::cout << "Destroying file provider for " << hash << std::endl ; + std::cout << "ftFileProvider::~ftFileProvider(): Destroying file provider for " << hash << std::endl ; #endif if (fd!=NULL) { fclose(fd); fd = NULL ; +#ifdef DEBUG_FT_FILE_PROVIDER + std::cout << "ftFileProvider::~ftFileProvider(): closed file: " << hash << std::endl ; +#endif } } @@ -102,10 +105,16 @@ bool ftFileProvider::purgeOldPeers(time_t now,uint32_t max_duration) { RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/ +#ifdef DEBUG_FT_FILE_PROVIDER + std::cerr << "ftFileProvider::purgeOldPeers(): " << (void*)this << ": examining peers." << std::endl ; +#endif bool ret = true ; for(std::map::iterator it(uploading_peers.begin());it!=uploading_peers.end();) if( (*it).second.lastTS+max_duration < (uint32_t)now) { +#ifdef DEBUG_FT_FILE_PROVIDER + std::cerr << "ftFileProvider::purgeOldPeers(): " << (void*)this << ": peer " << it->first << " is too old. Removing." << std::endl ; +#endif std::map::iterator tmp = it ; ++tmp ; uploading_peers.erase(it) ; @@ -113,6 +122,9 @@ bool ftFileProvider::purgeOldPeers(time_t now,uint32_t max_duration) } else { +#ifdef DEBUG_FT_FILE_PROVIDER + std::cerr << "ftFileProvider::purgeOldPeers(): " << (void*)this << ": peer " << it->first << " will be kept." << std::endl ; +#endif ret = false ; ++it ; } @@ -320,6 +332,9 @@ int ftFileProvider::initializeFileAttrs() return 0; } } +#ifdef DEBUG_FT_FILE_PROVIDER + std::cerr << "ftFileProvider:: openned file " << file_name << std::endl ; +#endif return 1; }