Repository: dmwm/wmcore
Issue: Issue creating /tmp/cmst0/.wmcore_cache file 

**Impact of the bug**
T0 Agent not able to create/run any job/workflow using WMCore agent 1.5.2

**Describe the bug**
T0 use the Tier0Feeder component to create the sandbox. When Tier0 started to test a new release based con WMCore 1.5.2, Tier0Feeder component was unable to start properly given the following error:
```
2021-09-13 06:55:27,603:140108589012736:WARNING:Service:The cachefile /tmp/cmst0/.wmcore_cache_xlpsm5ls/cmssdt.cern.ch/-3895757188456107843_GET__SDT_cgi-bin_ReleasesXML_ does not exi
st and the service at https://cmssdt.cern.ch/SDT/cgi-bin/ReleasesXML/ raised a FileNotFoundError(2, 'No such file or directory') when accessed
2021-09-13 06:55:27,673:140108589012736:ERROR:StdBase:About to raise exception <@========== WMException Start ==========@>
Exception Class: WMSpecFactoryException
Message: [Errno 2] No such file or directory: '/tmp/cmst0/.wmcore_cache_xlpsm5ls/cmssdt.cern.ch/-3895757188456107843_GET__SDT_cgi-bin_ReleasesXML_'
        ClassName : None
        ModuleName : WMCore.WMSpec.WMWorkloadTools
        MethodName : _validateArgFunction
        ClassInstance : None
        FileName : /data/tier0/srv/wmagent/3.0.0/sw.jamadova/slc7_amd64_gcc630/cms/t0/3.0.0/lib/python3.8/site-packages/WMCore/WMSpec/WMWorkloadTools.py
        LineNumber : 105
        ErrorNr : 0
```

**How to reproduce it**
Deploying the T0 (3.0.0) agent and start all the components of the agent or only Tier0Feeder.

**Expected behavior**
The /tmp/cmst0/.wmcore_cache file must be correctly created and finally Tier0Feeder will be able to create the sandbox

**Additional context and error message**
Issue is **only** affecting when T0 use WMCore 1.5.2.


Changed files:
src/python/WMCore/Services/Requests.py

diff --git a/src/python/WMCore/Services/Requests.py b/src/python/WMCore/Services/Requests.py
index 58f5e32d7..89d582756 100644
--- a/src/python/WMCore/Services/Requests.py
+++ b/src/python/WMCore/Services/Requests.py
@@ -7,8 +7,19 @@ A set of classes to handle making http and https requests to a remote server and
 deserialising the response.
 
 The response from the remote server is cached if expires/etags are set.
+Note that the cache can have two different behaviors:
+1. when WMCORE_CACHE_DIR is defined: it defines one specific path to be used for the
+   cache files. Cache files are never automatically cleaned up and it's up to the user/
+   maintainer to do so. Note that cache directories are named after the base class.
+2. otherwise, the system will use the Operating System temp dir to store the cache files,
+   which uses `/tmp` as default for Linux systems. When using these temporary areas, the
+   cache files are automatically cleaned up when the object using it is destroyed and
+   garbage collected. Cache directories carry a random name.
+
+By default, all of the WMCore central services define the CACHE_DIR (to use /data/srv/state).
 """
 from __future__ import division, print_function
+
 from future import standard_library
 standard_library.install_aliases()
 
@@ -51,6 +62,7 @@ except ImportError:
     class ServerNotFoundError(Exception):
         pass
 
+
 def check_server_url(srvurl):
     """Check given url for correctness"""
     good_name = srvurl.startswith('http://') or srvurl.startswith('https://')
@@ -216,7 +228,7 @@ class Requests(dict):
             setattr(e, 'status', 503)
             setattr(e, 'reason', 'Service Unavailable')
             setattr(e, 'result', str(ex))
-            raise e
+            raise e from None
         except (socket.error, AttributeError):
             self['logger'].warn("Http request failed, retrying once again..")
             # AttributeError implies initial connection error - need to close
@@ -230,10 +242,10 @@ class Requests(dict):
             try:
                 response, result = conn.request(uri, method=verb, body=data, headers=headers)
             except AttributeError:
-                msg = traceback.format_exc()
+                msg = 'Error contacting: {}: {}'.format(self.getDomainName(), traceback.format_exc())
                 # socket/httplib really screwed up - nuclear option
                 conn.connections = {}
-                raise socket.error('Error contacting: %s: %s' % (self.getDomainName(), msg))
+                raise socket.error(msg) from None
         if response.status >= 400:
             e = HTTPException()
             setattr(e, 'req_data', data)
@@ -367,6 +379,8 @@ class Requests(dict):
                 break
         else:
             idir = tempfile.mkdtemp(prefix='.wmcore_cache_')
+            # Alan Malta in 29 Mar 2022: this seems to prematurely remove the cache
+            # directory. For details, see: https://github.com/dmwm/WMCore/pull/10915
             self['deleteCacheOnExit'] = TempDirectory(idir)
             return idir
 
@@ -412,13 +426,9 @@ class Requests(dict):
 
     def addBasicAuth(self, username, password):
         """Add basic auth headers to request"""
-        ## TODO: base64.encodestring is deprecated
-        # https://docs.python.org/3.8/library/base64.html#base64.encodestring
-        # change to base64.encodebytes after we drop python2
         username = encodeUnicodeToBytes(username)
         password = encodeUnicodeToBytes(password)
-        encodedauth = base64.encodestring(b'%s:%s' % (
-            username, password)).strip()
+        encodedauth = base64.encodebytes(b'%s:%s' % (username, password)).strip()
         if PY3:
             encodedauth = decodeBytesToUnicode(encodedauth)
         auth_string = "Basic %s" % encodedauth