We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
env_mach_pes.xml not being locked properly
I ran into this problem while doing testing on cheyenne.  I was able to make changes in env_mach_pes.xml and submit to the queue without any error messages.  

I was also able to change env_mach_pes.xml, then do a rebuild and submit without redoing case.setup.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `scripts/lib/CIME/check_lockedfiles.py`
Content:
```
1 """
2 API for checking locked files
3 """
4 
5 from CIME.XML.standard_module_setup import *
6 from CIME.XML.env_build import EnvBuild
7 from CIME.XML.env_case import EnvCase
8 from CIME.XML.env_mach_pes import EnvMachPes
9 from CIME.XML.env_batch import EnvBatch
10 from CIME.utils import run_cmd_no_fail
11 
12 logger = logging.getLogger(__name__)
13 
14 import glob, shutil
15 
16 LOCKED_DIR = "LockedFiles"
17 
18 def lock_file(filename, caseroot=None, newname=None):
19     expect("/" not in filename, "Please just provide basename of locked file")
20     caseroot = os.getcwd() if caseroot is None else caseroot
21     newname = filename if newname is None else newname
22     fulllockdir = os.path.join(caseroot, LOCKED_DIR)
23     if not os.path.exists(fulllockdir):
24         os.mkdir(fulllockdir)
25     logging.debug("Locking file %s to %s"%(filename, newname))
26     shutil.copyfile(os.path.join(caseroot, filename), os.path.join(fulllockdir, newname))
27 
28 def unlock_file(filename, caseroot=None):
29     expect("/" not in filename, "Please just provide basename of locked file")
30     caseroot = os.getcwd() if caseroot is None else caseroot
31     locked_path = os.path.join(caseroot, LOCKED_DIR, filename)
32     if os.path.exists(locked_path):
33         os.remove(locked_path)
34 
35 def is_locked(filename, caseroot=None):
36     expect("/" not in filename, "Please just provide basename of locked file")
37     caseroot = os.getcwd() if caseroot is None else caseroot
38     return os.path.exists(os.path.join(caseroot, LOCKED_DIR, filename))
39 
40 def restore(filename, caseroot=None, newname=None):
41     """
42     Restore the locked version of filename into main case dir
43     """
44     expect("/" not in filename, "Please just provide basename of locked file")
45     caseroot = os.getcwd() if caseroot is None else caseroot
46     newname = filename if newname is None else newname
47     shutil.copyfile(os.path.join(caseroot, LOCKED_DIR, filename), os.path.join(caseroot, newname))
48     # relock the restored file if names diffs
49     if newname != filename:
50         lock_file(newname, caseroot)
51 
52 def check_pelayouts_require_rebuild(case, models):
53     """
54     Create if we require a rebuild, expects cwd is caseroot
55     """
56     locked_pes = os.path.join(LOCKED_DIR, "env_mach_pes.xml")
57     if os.path.exists(locked_pes):
58         # Look to see if $comp_PE_CHANGE_REQUIRES_REBUILD is defined
59         # for any component
60         env_mach_pes_locked = EnvMachPes(infile=locked_pes, components=case.get_values("COMP_CLASSES"))
61         for comp in models:
62             if case.get_value("%s_PE_CHANGE_REQUIRES_REBUILD" % comp):
63                 # Changing these values in env_mach_pes.xml will force
64                 # you to clean the corresponding component
65                 old_tasks   = env_mach_pes_locked.get_value("NTASKS_%s" % comp)
66                 old_threads = env_mach_pes_locked.get_value("NTHRDS_%s" % comp)
67                 old_inst    = env_mach_pes_locked.get_value("NINST_%s" % comp)
68 
69                 new_tasks   = case.get_value("NTASKS_%s" % comp)
70                 new_threads = case.get_value("NTHRDS_%s" % comp)
71                 new_inst    = case.get_value("NINST_%s" % comp)
72 
73                 if old_tasks != new_tasks or old_threads != new_threads or old_inst != new_inst:
74                     logging.warn("%s pe change requires clean build %s %s" % (comp, old_tasks, new_tasks))
75                     cleanflag = comp.lower()
76                     run_cmd_no_fail("./case.build --clean %s" % cleanflag)
77 
78         unlock_file("env_mach_pes.xml", case.get_value("CASEROOT"))
79 
80 def check_lockedfiles(caseroot=None):
81     """
82     Check that all lockedfiles match what's in case
83 
84     If caseroot is not specified, it is set to the current working directory
85     """
86     caseroot = os.getcwd() if caseroot is None else caseroot
87     lockedfiles = glob.glob(os.path.join(caseroot, "LockedFiles", "*.xml"))
88     for lfile in lockedfiles:
89         fpart = os.path.basename(lfile)
90         # ignore files used for tests such as env_mach_pes.ERP1.xml by looking for extra dots in the name
91         if lfile.count('.') > 1:
92             continue
93         cfile = os.path.join(caseroot, fpart)
94         if os.path.isfile(cfile):
95             objname = fpart.split('.')[0]
96             if objname == "env_build":
97                 f1obj = EnvBuild(caseroot, cfile)
98                 f2obj = EnvBuild(caseroot, lfile)
99             elif objname == "env_mach_pes":
100                 f1obj = EnvMachPes(caseroot, cfile)
101                 f2obj = EnvMachPes(caseroot, lfile)
102             elif objname == "env_case":
103                 f1obj = EnvCase(caseroot, cfile)
104                 f2obj = EnvCase(caseroot, lfile)
105             elif objname == "env_batch":
106                 f1obj = EnvBatch(caseroot, cfile)
107                 f2obj = EnvBatch(caseroot, lfile)
108             else:
109                 logging.warn("Locked XML file '%s' is not current being handled" % fpart)
110                 continue
111             diffs = f1obj.compare_xml(f2obj)
112             if diffs:
113                 logging.warn("File %s has been modified"%lfile)
114                 for key in diffs.keys():
115                     print("  found difference in %s : case %s locked %s" %
116                           (key, repr(diffs[key][0]), repr(diffs[key][1])))
117 
118                 if objname == "env_mach_pes":
119                     expect(False, "Invoke case.setup --reset ")
120                 elif objname == "env_case":
121                     expect(False, "Cannot change file env_case.xml, please"
122                            " recover the original copy from LockedFiles")
123                 elif objname == "env_build":
124                     logging.warn("Setting build complete to False")
125                     f1obj.set_value("BUILD_COMPLETE", False)
126                     if "PIO_VERSION" in diffs.keys():
127                         f1obj.set_value("BUILD_STATUS", 2)
128                         f1obj.write()
129                         logging.critical("Changing PIO_VERSION requires running "
130                                          "case.build --clean-all and rebuilding")
131                     else:
132                         f1obj.set_value("BUILD_STATUS", 1)
133                         f1obj.write()
134                 elif objname == "env_batch":
135                     expect(False, "Batch configuration has changed, please run case.setup --reset")
136                 else:
137                     expect(False, "'%s' diff was not handled" % objname)
138
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/scripts/lib/CIME/check_lockedfiles.py b/scripts/lib/CIME/check_lockedfiles.py
--- a/scripts/lib/CIME/check_lockedfiles.py
+++ b/scripts/lib/CIME/check_lockedfiles.py
@@ -88,7 +88,7 @@
     for lfile in lockedfiles:
         fpart = os.path.basename(lfile)
         # ignore files used for tests such as env_mach_pes.ERP1.xml by looking for extra dots in the name
-        if lfile.count('.') > 1:
+        if fpart.count('.') > 1:
             continue
         cfile = os.path.join(caseroot, fpart)
         if os.path.isfile(cfile):

{"golden_diff": "diff --git a/scripts/lib/CIME/check_lockedfiles.py b/scripts/lib/CIME/check_lockedfiles.py\n--- a/scripts/lib/CIME/check_lockedfiles.py\n+++ b/scripts/lib/CIME/check_lockedfiles.py\n@@ -88,7 +88,7 @@\n     for lfile in lockedfiles:\n         fpart = os.path.basename(lfile)\n         # ignore files used for tests such as env_mach_pes.ERP1.xml by looking for extra dots in the name\n-        if lfile.count('.') > 1:\n+        if fpart.count('.') > 1:\n             continue\n         cfile = os.path.join(caseroot, fpart)\n         if os.path.isfile(cfile):\n", "issue": "env_mach_pes.xml not being locked properly\nI ran into this problem while doing testing on cheyenne.  I was able to make changes in env_mach_pes.xml and submit to the queue without any error messages.  \r\n\r\nI was also able to change env_mach_pes.xml, then do a rebuild and submit without redoing case.setup.\n", "before_files": [{"content": "\"\"\"\nAPI for checking locked files\n\"\"\"\n\nfrom CIME.XML.standard_module_setup import *\nfrom CIME.XML.env_build import EnvBuild\nfrom CIME.XML.env_case import EnvCase\nfrom CIME.XML.env_mach_pes import EnvMachPes\nfrom CIME.XML.env_batch import EnvBatch\nfrom CIME.utils import run_cmd_no_fail\n\nlogger = logging.getLogger(__name__)\n\nimport glob, shutil\n\nLOCKED_DIR = \"LockedFiles\"\n\ndef lock_file(filename, caseroot=None, newname=None):\n    expect(\"/\" not in filename, \"Please just provide basename of locked file\")\n    caseroot = os.getcwd() if caseroot is None else caseroot\n    newname = filename if newname is None else newname\n    fulllockdir = os.path.join(caseroot, LOCKED_DIR)\n    if not os.path.exists(fulllockdir):\n        os.mkdir(fulllockdir)\n    logging.debug(\"Locking file %s to %s\"%(filename, newname))\n    shutil.copyfile(os.path.join(caseroot, filename), os.path.join(fulllockdir, newname))\n\ndef unlock_file(filename, caseroot=None):\n    expect(\"/\" not in filename, \"Please just provide basename of locked file\")\n    caseroot = os.getcwd() if caseroot is None else caseroot\n    locked_path = os.path.join(caseroot, LOCKED_DIR, filename)\n    if os.path.exists(locked_path):\n        os.remove(locked_path)\n\ndef is_locked(filename, caseroot=None):\n    expect(\"/\" not in filename, \"Please just provide basename of locked file\")\n    caseroot = os.getcwd() if caseroot is None else caseroot\n    return os.path.exists(os.path.join(caseroot, LOCKED_DIR, filename))\n\ndef restore(filename, caseroot=None, newname=None):\n    \"\"\"\n    Restore the locked version of filename into main case dir\n    \"\"\"\n    expect(\"/\" not in filename, \"Please just provide basename of locked file\")\n    caseroot = os.getcwd() if caseroot is None else caseroot\n    newname = filename if newname is None else newname\n    shutil.copyfile(os.path.join(caseroot, LOCKED_DIR, filename), os.path.join(caseroot, newname))\n    # relock the restored file if names diffs\n    if newname != filename:\n        lock_file(newname, caseroot)\n\ndef check_pelayouts_require_rebuild(case, models):\n    \"\"\"\n    Create if we require a rebuild, expects cwd is caseroot\n    \"\"\"\n    locked_pes = os.path.join(LOCKED_DIR, \"env_mach_pes.xml\")\n    if os.path.exists(locked_pes):\n        # Look to see if $comp_PE_CHANGE_REQUIRES_REBUILD is defined\n        # for any component\n        env_mach_pes_locked = EnvMachPes(infile=locked_pes, components=case.get_values(\"COMP_CLASSES\"))\n        for comp in models:\n            if case.get_value(\"%s_PE_CHANGE_REQUIRES_REBUILD\" % comp):\n                # Changing these values in env_mach_pes.xml will force\n                # you to clean the corresponding component\n                old_tasks   = env_mach_pes_locked.get_value(\"NTASKS_%s\" % comp)\n                old_threads = env_mach_pes_locked.get_value(\"NTHRDS_%s\" % comp)\n                old_inst    = env_mach_pes_locked.get_value(\"NINST_%s\" % comp)\n\n                new_tasks   = case.get_value(\"NTASKS_%s\" % comp)\n                new_threads = case.get_value(\"NTHRDS_%s\" % comp)\n                new_inst    = case.get_value(\"NINST_%s\" % comp)\n\n                if old_tasks != new_tasks or old_threads != new_threads or old_inst != new_inst:\n                    logging.warn(\"%s pe change requires clean build %s %s\" % (comp, old_tasks, new_tasks))\n                    cleanflag = comp.lower()\n                    run_cmd_no_fail(\"./case.build --clean %s\" % cleanflag)\n\n        unlock_file(\"env_mach_pes.xml\", case.get_value(\"CASEROOT\"))\n\ndef check_lockedfiles(caseroot=None):\n    \"\"\"\n    Check that all lockedfiles match what's in case\n\n    If caseroot is not specified, it is set to the current working directory\n    \"\"\"\n    caseroot = os.getcwd() if caseroot is None else caseroot\n    lockedfiles = glob.glob(os.path.join(caseroot, \"LockedFiles\", \"*.xml\"))\n    for lfile in lockedfiles:\n        fpart = os.path.basename(lfile)\n        # ignore files used for tests such as env_mach_pes.ERP1.xml by looking for extra dots in the name\n        if lfile.count('.') > 1:\n            continue\n        cfile = os.path.join(caseroot, fpart)\n        if os.path.isfile(cfile):\n            objname = fpart.split('.')[0]\n            if objname == \"env_build\":\n                f1obj = EnvBuild(caseroot, cfile)\n                f2obj = EnvBuild(caseroot, lfile)\n            elif objname == \"env_mach_pes\":\n                f1obj = EnvMachPes(caseroot, cfile)\n                f2obj = EnvMachPes(caseroot, lfile)\n            elif objname == \"env_case\":\n                f1obj = EnvCase(caseroot, cfile)\n                f2obj = EnvCase(caseroot, lfile)\n            elif objname == \"env_batch\":\n                f1obj = EnvBatch(caseroot, cfile)\n                f2obj = EnvBatch(caseroot, lfile)\n            else:\n                logging.warn(\"Locked XML file '%s' is not current being handled\" % fpart)\n                continue\n            diffs = f1obj.compare_xml(f2obj)\n            if diffs:\n                logging.warn(\"File %s has been modified\"%lfile)\n                for key in diffs.keys():\n                    print(\"  found difference in %s : case %s locked %s\" %\n                          (key, repr(diffs[key][0]), repr(diffs[key][1])))\n\n                if objname == \"env_mach_pes\":\n                    expect(False, \"Invoke case.setup --reset \")\n                elif objname == \"env_case\":\n                    expect(False, \"Cannot change file env_case.xml, please\"\n                           \" recover the original copy from LockedFiles\")\n                elif objname == \"env_build\":\n                    logging.warn(\"Setting build complete to False\")\n                    f1obj.set_value(\"BUILD_COMPLETE\", False)\n                    if \"PIO_VERSION\" in diffs.keys():\n                        f1obj.set_value(\"BUILD_STATUS\", 2)\n                        f1obj.write()\n                        logging.critical(\"Changing PIO_VERSION requires running \"\n                                         \"case.build --clean-all and rebuilding\")\n                    else:\n                        f1obj.set_value(\"BUILD_STATUS\", 1)\n                        f1obj.write()\n                elif objname == \"env_batch\":\n                    expect(False, \"Batch configuration has changed, please run case.setup --reset\")\n                else:\n                    expect(False, \"'%s' diff was not handled\" % objname)\n", "path": "scripts/lib/CIME/check_lockedfiles.py"}], "after_files": [{"content": "\"\"\"\nAPI for checking locked files\n\"\"\"\n\nfrom CIME.XML.standard_module_setup import *\nfrom CIME.XML.env_build import EnvBuild\nfrom CIME.XML.env_case import EnvCase\nfrom CIME.XML.env_mach_pes import EnvMachPes\nfrom CIME.XML.env_batch import EnvBatch\nfrom CIME.utils import run_cmd_no_fail\n\nlogger = logging.getLogger(__name__)\n\nimport glob, shutil\n\nLOCKED_DIR = \"LockedFiles\"\n\ndef lock_file(filename, caseroot=None, newname=None):\n    expect(\"/\" not in filename, \"Please just provide basename of locked file\")\n    caseroot = os.getcwd() if caseroot is None else caseroot\n    newname = filename if newname is None else newname\n    fulllockdir = os.path.join(caseroot, LOCKED_DIR)\n    if not os.path.exists(fulllockdir):\n        os.mkdir(fulllockdir)\n    logging.debug(\"Locking file %s to %s\"%(filename, newname))\n    shutil.copyfile(os.path.join(caseroot, filename), os.path.join(fulllockdir, newname))\n\ndef unlock_file(filename, caseroot=None):\n    expect(\"/\" not in filename, \"Please just provide basename of locked file\")\n    caseroot = os.getcwd() if caseroot is None else caseroot\n    locked_path = os.path.join(caseroot, LOCKED_DIR, filename)\n    if os.path.exists(locked_path):\n        os.remove(locked_path)\n\ndef is_locked(filename, caseroot=None):\n    expect(\"/\" not in filename, \"Please just provide basename of locked file\")\n    caseroot = os.getcwd() if caseroot is None else caseroot\n    return os.path.exists(os.path.join(caseroot, LOCKED_DIR, filename))\n\ndef restore(filename, caseroot=None, newname=None):\n    \"\"\"\n    Restore the locked version of filename into main case dir\n    \"\"\"\n    expect(\"/\" not in filename, \"Please just provide basename of locked file\")\n    caseroot = os.getcwd() if caseroot is None else caseroot\n    newname = filename if newname is None else newname\n    shutil.copyfile(os.path.join(caseroot, LOCKED_DIR, filename), os.path.join(caseroot, newname))\n    # relock the restored file if names diffs\n    if newname != filename:\n        lock_file(newname, caseroot)\n\ndef check_pelayouts_require_rebuild(case, models):\n    \"\"\"\n    Create if we require a rebuild, expects cwd is caseroot\n    \"\"\"\n    locked_pes = os.path.join(LOCKED_DIR, \"env_mach_pes.xml\")\n    if os.path.exists(locked_pes):\n        # Look to see if $comp_PE_CHANGE_REQUIRES_REBUILD is defined\n        # for any component\n        env_mach_pes_locked = EnvMachPes(infile=locked_pes, components=case.get_values(\"COMP_CLASSES\"))\n        for comp in models:\n            if case.get_value(\"%s_PE_CHANGE_REQUIRES_REBUILD\" % comp):\n                # Changing these values in env_mach_pes.xml will force\n                # you to clean the corresponding component\n                old_tasks   = env_mach_pes_locked.get_value(\"NTASKS_%s\" % comp)\n                old_threads = env_mach_pes_locked.get_value(\"NTHRDS_%s\" % comp)\n                old_inst    = env_mach_pes_locked.get_value(\"NINST_%s\" % comp)\n\n                new_tasks   = case.get_value(\"NTASKS_%s\" % comp)\n                new_threads = case.get_value(\"NTHRDS_%s\" % comp)\n                new_inst    = case.get_value(\"NINST_%s\" % comp)\n\n                if old_tasks != new_tasks or old_threads != new_threads or old_inst != new_inst:\n                    logging.warn(\"%s pe change requires clean build %s %s\" % (comp, old_tasks, new_tasks))\n                    cleanflag = comp.lower()\n                    run_cmd_no_fail(\"./case.build --clean %s\" % cleanflag)\n\n        unlock_file(\"env_mach_pes.xml\", case.get_value(\"CASEROOT\"))\n\ndef check_lockedfiles(caseroot=None):\n    \"\"\"\n    Check that all lockedfiles match what's in case\n\n    If caseroot is not specified, it is set to the current working directory\n    \"\"\"\n    caseroot = os.getcwd() if caseroot is None else caseroot\n    lockedfiles = glob.glob(os.path.join(caseroot, \"LockedFiles\", \"*.xml\"))\n    for lfile in lockedfiles:\n        fpart = os.path.basename(lfile)\n        # ignore files used for tests such as env_mach_pes.ERP1.xml by looking for extra dots in the name\n        if fpart.count('.') > 1:\n            continue\n        cfile = os.path.join(caseroot, fpart)\n        if os.path.isfile(cfile):\n            objname = fpart.split('.')[0]\n            if objname == \"env_build\":\n                f1obj = EnvBuild(caseroot, cfile)\n                f2obj = EnvBuild(caseroot, lfile)\n            elif objname == \"env_mach_pes\":\n                f1obj = EnvMachPes(caseroot, cfile)\n                f2obj = EnvMachPes(caseroot, lfile)\n            elif objname == \"env_case\":\n                f1obj = EnvCase(caseroot, cfile)\n                f2obj = EnvCase(caseroot, lfile)\n            elif objname == \"env_batch\":\n                f1obj = EnvBatch(caseroot, cfile)\n                f2obj = EnvBatch(caseroot, lfile)\n            else:\n                logging.warn(\"Locked XML file '%s' is not current being handled\" % fpart)\n                continue\n            diffs = f1obj.compare_xml(f2obj)\n            if diffs:\n                logging.warn(\"File %s has been modified\"%lfile)\n                for key in diffs.keys():\n                    print(\"  found difference in %s : case %s locked %s\" %\n                          (key, repr(diffs[key][0]), repr(diffs[key][1])))\n\n                if objname == \"env_mach_pes\":\n                    expect(False, \"Invoke case.setup --reset \")\n                elif objname == \"env_case\":\n                    expect(False, \"Cannot change file env_case.xml, please\"\n                           \" recover the original copy from LockedFiles\")\n                elif objname == \"env_build\":\n                    logging.warn(\"Setting build complete to False\")\n                    f1obj.set_value(\"BUILD_COMPLETE\", False)\n                    if \"PIO_VERSION\" in diffs.keys():\n                        f1obj.set_value(\"BUILD_STATUS\", 2)\n                        f1obj.write()\n                        logging.critical(\"Changing PIO_VERSION requires running \"\n                                         \"case.build --clean-all and rebuilding\")\n                    else:\n                        f1obj.set_value(\"BUILD_STATUS\", 1)\n                        f1obj.write()\n                elif objname == \"env_batch\":\n                    expect(False, \"Batch configuration has changed, please run case.setup --reset\")\n                else:\n                    expect(False, \"'%s' diff was not handled\" % objname)\n", "path": "scripts/lib/CIME/check_lockedfiles.py"}]}