We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Cron job that cleans hf-datasets cache often fails
> Description: Job datasets-server/prod-datasets-server-job-clean-hf-datasets-cache-28426140 failed to complete. Removing failed job after investigation should clear this alert.

https://huggingface.slack.com/archives/C05UE1R5ZBJ/p1705571880300879



--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `jobs/cache_maintenance/src/cache_maintenance/clean_directory.py`
Content:
```
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2023 The HuggingFace Authors.
3 
4 import glob
5 import logging
6 import os
7 import shutil
8 from datetime import datetime, timedelta
9 from typing import Any
10 
11 
12 def clean_directory(pattern: str, expired_time_interval_seconds: int) -> None:
13     """
14     Delete temporary cache directories that match the given pattern.
15     """
16     # sanity check
17     if len(str(pattern)) < 10:
18         raise RuntimeError(f"Sanity check on pattern failed: len('{pattern}') < 10.")
19     logging.info(f"looking for all files and directories with pattern {pattern}")
20     now = datetime.now().replace(tzinfo=None)
21     errors = 0
22     total_dirs = 0
23     total_files = 0
24 
25     def rmtree_on_error(function: Any, path: str, excinfo: Any) -> None:  # noqa: ARG001, args needed for onerror=
26         logging.error(f"failed to delete directory {path=}")
27 
28     for path in glob.glob(pattern):
29         last_access_time_value = os.path.getatime(path)
30         last_access_datetime = datetime.fromtimestamp(last_access_time_value).replace(tzinfo=None)
31         if last_access_datetime + timedelta(seconds=expired_time_interval_seconds) <= now:
32             if os.path.isfile(path):
33                 logging.info(f"deleting file {path=} {last_access_datetime=}")
34                 os.remove(path)
35                 total_files += 1
36             elif os.path.isdir(path):
37                 logging.info(f"deleting directory {path=} {last_access_datetime=}")
38                 shutil.rmtree(path, onerror=rmtree_on_error)
39                 total_dirs += 1
40                 errors += os.path.isdir(path)
41     if errors:
42         logging.error(f"clean_directory failed to remove {errors} directories at the root of the cache directory.")
43     if total_files:
44         logging.info(f"clean_directory removed {total_files} files at the root of the cache directory.")
45 
46     logging.info(f"clean_directory removed {total_dirs - errors} directories at the root of the cache directory.")
47
```
--- 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/jobs/cache_maintenance/src/cache_maintenance/clean_directory.py b/jobs/cache_maintenance/src/cache_maintenance/clean_directory.py
--- a/jobs/cache_maintenance/src/cache_maintenance/clean_directory.py
+++ b/jobs/cache_maintenance/src/cache_maintenance/clean_directory.py
@@ -21,26 +21,35 @@
     errors = 0
     total_dirs = 0
     total_files = 0
+    total_disappeared_paths = 0
 
     def rmtree_on_error(function: Any, path: str, excinfo: Any) -> None:  # noqa: ARG001, args needed for onerror=
         logging.error(f"failed to delete directory {path=}")
 
     for path in glob.glob(pattern):
-        last_access_time_value = os.path.getatime(path)
-        last_access_datetime = datetime.fromtimestamp(last_access_time_value).replace(tzinfo=None)
-        if last_access_datetime + timedelta(seconds=expired_time_interval_seconds) <= now:
-            if os.path.isfile(path):
-                logging.info(f"deleting file {path=} {last_access_datetime=}")
-                os.remove(path)
-                total_files += 1
-            elif os.path.isdir(path):
-                logging.info(f"deleting directory {path=} {last_access_datetime=}")
-                shutil.rmtree(path, onerror=rmtree_on_error)
-                total_dirs += 1
-                errors += os.path.isdir(path)
+        try:
+            last_access_time_value = os.path.getatime(path)
+            last_access_datetime = datetime.fromtimestamp(last_access_time_value).replace(tzinfo=None)
+            if last_access_datetime + timedelta(seconds=expired_time_interval_seconds) <= now:
+                if os.path.isfile(path):
+                    logging.info(f"deleting file {path=} {last_access_datetime=}")
+                    os.remove(path)
+                    total_files += 1
+                elif os.path.isdir(path):
+                    logging.info(f"deleting directory {path=} {last_access_datetime=}")
+                    shutil.rmtree(path, onerror=rmtree_on_error)
+                    total_dirs += 1
+                    errors += os.path.isdir(path)
+        except FileNotFoundError:
+            logging.error(f"failed to delete {path=} because it has disappeared during the loop")
+            total_disappeared_paths += 1
     if errors:
         logging.error(f"clean_directory failed to remove {errors} directories at the root of the cache directory.")
     if total_files:
         logging.info(f"clean_directory removed {total_files} files at the root of the cache directory.")
+    if total_disappeared_paths:
+        logging.info(
+            f"clean_directory failed to delete {total_disappeared_paths} paths because they disappeared during the loop."
+        )
 
     logging.info(f"clean_directory removed {total_dirs - errors} directories at the root of the cache directory.")

{"golden_diff": "diff --git a/jobs/cache_maintenance/src/cache_maintenance/clean_directory.py b/jobs/cache_maintenance/src/cache_maintenance/clean_directory.py\n--- a/jobs/cache_maintenance/src/cache_maintenance/clean_directory.py\n+++ b/jobs/cache_maintenance/src/cache_maintenance/clean_directory.py\n@@ -21,26 +21,35 @@\n     errors = 0\n     total_dirs = 0\n     total_files = 0\n+    total_disappeared_paths = 0\n \n     def rmtree_on_error(function: Any, path: str, excinfo: Any) -> None:  # noqa: ARG001, args needed for onerror=\n         logging.error(f\"failed to delete directory {path=}\")\n \n     for path in glob.glob(pattern):\n-        last_access_time_value = os.path.getatime(path)\n-        last_access_datetime = datetime.fromtimestamp(last_access_time_value).replace(tzinfo=None)\n-        if last_access_datetime + timedelta(seconds=expired_time_interval_seconds) <= now:\n-            if os.path.isfile(path):\n-                logging.info(f\"deleting file {path=} {last_access_datetime=}\")\n-                os.remove(path)\n-                total_files += 1\n-            elif os.path.isdir(path):\n-                logging.info(f\"deleting directory {path=} {last_access_datetime=}\")\n-                shutil.rmtree(path, onerror=rmtree_on_error)\n-                total_dirs += 1\n-                errors += os.path.isdir(path)\n+        try:\n+            last_access_time_value = os.path.getatime(path)\n+            last_access_datetime = datetime.fromtimestamp(last_access_time_value).replace(tzinfo=None)\n+            if last_access_datetime + timedelta(seconds=expired_time_interval_seconds) <= now:\n+                if os.path.isfile(path):\n+                    logging.info(f\"deleting file {path=} {last_access_datetime=}\")\n+                    os.remove(path)\n+                    total_files += 1\n+                elif os.path.isdir(path):\n+                    logging.info(f\"deleting directory {path=} {last_access_datetime=}\")\n+                    shutil.rmtree(path, onerror=rmtree_on_error)\n+                    total_dirs += 1\n+                    errors += os.path.isdir(path)\n+        except FileNotFoundError:\n+            logging.error(f\"failed to delete {path=} because it has disappeared during the loop\")\n+            total_disappeared_paths += 1\n     if errors:\n         logging.error(f\"clean_directory failed to remove {errors} directories at the root of the cache directory.\")\n     if total_files:\n         logging.info(f\"clean_directory removed {total_files} files at the root of the cache directory.\")\n+    if total_disappeared_paths:\n+        logging.info(\n+            f\"clean_directory failed to delete {total_disappeared_paths} paths because they disappeared during the loop.\"\n+        )\n \n     logging.info(f\"clean_directory removed {total_dirs - errors} directories at the root of the cache directory.\")\n", "issue": "Cron job that cleans hf-datasets cache often fails\n> Description: Job datasets-server/prod-datasets-server-job-clean-hf-datasets-cache-28426140 failed to complete. Removing failed job after investigation should clear this alert.\r\n\r\nhttps://huggingface.slack.com/archives/C05UE1R5ZBJ/p1705571880300879\r\n\r\n\n", "before_files": [{"content": "# SPDX-License-Identifier: Apache-2.0\n# Copyright 2023 The HuggingFace Authors.\n\nimport glob\nimport logging\nimport os\nimport shutil\nfrom datetime import datetime, timedelta\nfrom typing import Any\n\n\ndef clean_directory(pattern: str, expired_time_interval_seconds: int) -> None:\n    \"\"\"\n    Delete temporary cache directories that match the given pattern.\n    \"\"\"\n    # sanity check\n    if len(str(pattern)) < 10:\n        raise RuntimeError(f\"Sanity check on pattern failed: len('{pattern}') < 10.\")\n    logging.info(f\"looking for all files and directories with pattern {pattern}\")\n    now = datetime.now().replace(tzinfo=None)\n    errors = 0\n    total_dirs = 0\n    total_files = 0\n\n    def rmtree_on_error(function: Any, path: str, excinfo: Any) -> None:  # noqa: ARG001, args needed for onerror=\n        logging.error(f\"failed to delete directory {path=}\")\n\n    for path in glob.glob(pattern):\n        last_access_time_value = os.path.getatime(path)\n        last_access_datetime = datetime.fromtimestamp(last_access_time_value).replace(tzinfo=None)\n        if last_access_datetime + timedelta(seconds=expired_time_interval_seconds) <= now:\n            if os.path.isfile(path):\n                logging.info(f\"deleting file {path=} {last_access_datetime=}\")\n                os.remove(path)\n                total_files += 1\n            elif os.path.isdir(path):\n                logging.info(f\"deleting directory {path=} {last_access_datetime=}\")\n                shutil.rmtree(path, onerror=rmtree_on_error)\n                total_dirs += 1\n                errors += os.path.isdir(path)\n    if errors:\n        logging.error(f\"clean_directory failed to remove {errors} directories at the root of the cache directory.\")\n    if total_files:\n        logging.info(f\"clean_directory removed {total_files} files at the root of the cache directory.\")\n\n    logging.info(f\"clean_directory removed {total_dirs - errors} directories at the root of the cache directory.\")\n", "path": "jobs/cache_maintenance/src/cache_maintenance/clean_directory.py"}], "after_files": [{"content": "# SPDX-License-Identifier: Apache-2.0\n# Copyright 2023 The HuggingFace Authors.\n\nimport glob\nimport logging\nimport os\nimport shutil\nfrom datetime import datetime, timedelta\nfrom typing import Any\n\n\ndef clean_directory(pattern: str, expired_time_interval_seconds: int) -> None:\n    \"\"\"\n    Delete temporary cache directories that match the given pattern.\n    \"\"\"\n    # sanity check\n    if len(str(pattern)) < 10:\n        raise RuntimeError(f\"Sanity check on pattern failed: len('{pattern}') < 10.\")\n    logging.info(f\"looking for all files and directories with pattern {pattern}\")\n    now = datetime.now().replace(tzinfo=None)\n    errors = 0\n    total_dirs = 0\n    total_files = 0\n    total_disappeared_paths = 0\n\n    def rmtree_on_error(function: Any, path: str, excinfo: Any) -> None:  # noqa: ARG001, args needed for onerror=\n        logging.error(f\"failed to delete directory {path=}\")\n\n    for path in glob.glob(pattern):\n        try:\n            last_access_time_value = os.path.getatime(path)\n            last_access_datetime = datetime.fromtimestamp(last_access_time_value).replace(tzinfo=None)\n            if last_access_datetime + timedelta(seconds=expired_time_interval_seconds) <= now:\n                if os.path.isfile(path):\n                    logging.info(f\"deleting file {path=} {last_access_datetime=}\")\n                    os.remove(path)\n                    total_files += 1\n                elif os.path.isdir(path):\n                    logging.info(f\"deleting directory {path=} {last_access_datetime=}\")\n                    shutil.rmtree(path, onerror=rmtree_on_error)\n                    total_dirs += 1\n                    errors += os.path.isdir(path)\n        except FileNotFoundError:\n            logging.error(f\"failed to delete {path=} because it has disappeared during the loop\")\n            total_disappeared_paths += 1\n    if errors:\n        logging.error(f\"clean_directory failed to remove {errors} directories at the root of the cache directory.\")\n    if total_files:\n        logging.info(f\"clean_directory removed {total_files} files at the root of the cache directory.\")\n    if total_disappeared_paths:\n        logging.info(\n            f\"clean_directory failed to delete {total_disappeared_paths} paths because they disappeared during the loop.\"\n        )\n\n    logging.info(f\"clean_directory removed {total_dirs - errors} directories at the root of the cache directory.\")\n", "path": "jobs/cache_maintenance/src/cache_maintenance/clean_directory.py"}]}