We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Target Hitting Recursion Limit During Pants Setup (with workaround)
# Description of Problem
We’re in the process of migrating from 1.25 to 2.1.0., and hit an issue trying to run a test on specific target. The target is large and results in a max recursion limit exceeded.

I tried hacking on `sys.setrecursionlimit` and found for our use case 1021 was the min that would allow the test to succeed.

We can try breaking that target up, but the app it is testing is kind of a monolith so i don’t know how successful that would be.

Can you make a runtime limit in pants to handle?

This error happens in the pants setup before our pytest is run.

# Workaround
In one of our plugin's `register.py` we added `sys.setrecursionlimit(1021)` and this resolved our problem.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `src/python/pants/bin/pants_loader.py`
Content:
```
1 # Copyright 2017 Pants project contributors (see CONTRIBUTORS.md).
2 # Licensed under the Apache License, Version 2.0 (see LICENSE).
3 
4 import importlib
5 import locale
6 import os
7 import warnings
8 from textwrap import dedent
9 
10 
11 class PantsLoader:
12     """Loads and executes entrypoints."""
13 
14     ENTRYPOINT_ENV_VAR = "PANTS_ENTRYPOINT"
15     DEFAULT_ENTRYPOINT = "pants.bin.pants_exe:main"
16 
17     ENCODING_IGNORE_ENV_VAR = "PANTS_IGNORE_UNRECOGNIZED_ENCODING"
18 
19     class InvalidLocaleError(Exception):
20         """Raised when a valid locale can't be found."""
21 
22     @staticmethod
23     def setup_warnings():
24         # We want to present warnings to the user, set this up before importing any of our own code,
25         # to ensure all deprecation warnings are seen, including module deprecations.
26         # The "default" action displays a warning for a particular file and line number exactly once.
27         # See https://docs.python.org/3/library/warnings.html#the-warnings-filter for the complete list.
28         #
29         # However, we do turn off deprecation warnings for libraries that Pants uses for which we do
30         # not have a fixed upstream version, typically because the library is no longer maintained.
31         warnings.simplefilter("default", category=DeprecationWarning)
32         # TODO: Eric-Arellano has emailed the author to see if he is willing to accept a PR fixing the
33         # deprecation warnings and to release the fix. If he says yes, remove this once fixed.
34         warnings.filterwarnings("ignore", category=DeprecationWarning, module="ansicolors")
35         # Silence this ubiquitous warning. Several of our 3rd party deps incur this.
36         warnings.filterwarnings(
37             "ignore",
38             category=DeprecationWarning,
39             message="Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated",
40         )
41 
42     @classmethod
43     def ensure_locale(cls):
44         # Sanity check for locale, See https://github.com/pantsbuild/pants/issues/2465.
45         # This check is done early to give good feedback to user on how to fix the problem. Other
46         # libraries called by Pants may fail with more obscure errors.
47         encoding = locale.getpreferredencoding()
48         if (
49             encoding.lower() != "utf-8"
50             and os.environ.get(cls.ENCODING_IGNORE_ENV_VAR, None) is None
51         ):
52             raise cls.InvalidLocaleError(
53                 dedent(
54                     """
55                     Your system's preferred encoding is `{}`, but Pants requires `UTF-8`.
56                     Specifically, Python's `locale.getpreferredencoding()` must resolve to `UTF-8`.
57 
58                     Fix it by setting the LC_* and LANG environment settings. Example:
59                       LC_ALL=en_US.UTF-8
60                       LANG=en_US.UTF-8
61                     Or, bypass it by setting the below environment variable.
62                       {}=1
63                     Note: we cannot guarantee consistent behavior with this bypass enabled.
64                     """.format(
65                         encoding, cls.ENCODING_IGNORE_ENV_VAR
66                     )
67                 )
68             )
69 
70     @staticmethod
71     def determine_entrypoint(env_var, default):
72         return os.environ.pop(env_var, default)
73 
74     @staticmethod
75     def load_and_execute(entrypoint):
76         assert ":" in entrypoint, "ERROR: entrypoint must be of the form `module.path:callable`"
77         module_path, func_name = entrypoint.split(":", 1)
78         module = importlib.import_module(module_path)
79         entrypoint_main = getattr(module, func_name)
80         assert callable(entrypoint_main), "ERROR: entrypoint `{}` is not callable".format(
81             entrypoint
82         )
83         entrypoint_main()
84 
85     @classmethod
86     def run(cls):
87         cls.setup_warnings()
88         cls.ensure_locale()
89         entrypoint = cls.determine_entrypoint(cls.ENTRYPOINT_ENV_VAR, cls.DEFAULT_ENTRYPOINT)
90         cls.load_and_execute(entrypoint)
91 
92 
93 def main():
94     PantsLoader.run()
95 
96 
97 if __name__ == "__main__":
98     main()
99
```
--- 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/src/python/pants/bin/pants_loader.py b/src/python/pants/bin/pants_loader.py
--- a/src/python/pants/bin/pants_loader.py
+++ b/src/python/pants/bin/pants_loader.py
@@ -4,6 +4,7 @@
 import importlib
 import locale
 import os
+import sys
 import warnings
 from textwrap import dedent
 
@@ -14,6 +15,8 @@
     ENTRYPOINT_ENV_VAR = "PANTS_ENTRYPOINT"
     DEFAULT_ENTRYPOINT = "pants.bin.pants_exe:main"
 
+    RECURSION_LIMIT_ENV_VAR = "PANTS_RECURSION_LIMIT"
+
     ENCODING_IGNORE_ENV_VAR = "PANTS_IGNORE_UNRECOGNIZED_ENCODING"
 
     class InvalidLocaleError(Exception):
@@ -67,6 +70,10 @@
                 )
             )
 
+    @classmethod
+    def set_recursion_limit(cls):
+        sys.setrecursionlimit(int(os.environ.get(cls.RECURSION_LIMIT_ENV_VAR, "10000")))
+
     @staticmethod
     def determine_entrypoint(env_var, default):
         return os.environ.pop(env_var, default)
@@ -86,6 +93,7 @@
     def run(cls):
         cls.setup_warnings()
         cls.ensure_locale()
+        cls.set_recursion_limit()
         entrypoint = cls.determine_entrypoint(cls.ENTRYPOINT_ENV_VAR, cls.DEFAULT_ENTRYPOINT)
         cls.load_and_execute(entrypoint)

{"golden_diff": "diff --git a/src/python/pants/bin/pants_loader.py b/src/python/pants/bin/pants_loader.py\n--- a/src/python/pants/bin/pants_loader.py\n+++ b/src/python/pants/bin/pants_loader.py\n@@ -4,6 +4,7 @@\n import importlib\n import locale\n import os\n+import sys\n import warnings\n from textwrap import dedent\n \n@@ -14,6 +15,8 @@\n     ENTRYPOINT_ENV_VAR = \"PANTS_ENTRYPOINT\"\n     DEFAULT_ENTRYPOINT = \"pants.bin.pants_exe:main\"\n \n+    RECURSION_LIMIT_ENV_VAR = \"PANTS_RECURSION_LIMIT\"\n+\n     ENCODING_IGNORE_ENV_VAR = \"PANTS_IGNORE_UNRECOGNIZED_ENCODING\"\n \n     class InvalidLocaleError(Exception):\n@@ -67,6 +70,10 @@\n                 )\n             )\n \n+    @classmethod\n+    def set_recursion_limit(cls):\n+        sys.setrecursionlimit(int(os.environ.get(cls.RECURSION_LIMIT_ENV_VAR, \"10000\")))\n+\n     @staticmethod\n     def determine_entrypoint(env_var, default):\n         return os.environ.pop(env_var, default)\n@@ -86,6 +93,7 @@\n     def run(cls):\n         cls.setup_warnings()\n         cls.ensure_locale()\n+        cls.set_recursion_limit()\n         entrypoint = cls.determine_entrypoint(cls.ENTRYPOINT_ENV_VAR, cls.DEFAULT_ENTRYPOINT)\n         cls.load_and_execute(entrypoint)\n", "issue": "Target Hitting Recursion Limit During Pants Setup (with workaround)\n# Description of Problem\r\nWe\u2019re in the process of migrating from 1.25 to 2.1.0., and hit an issue trying to run a test on specific target. The target is large and results in a max recursion limit exceeded.\r\n\r\nI tried hacking on `sys.setrecursionlimit` and found for our use case 1021 was the min that would allow the test to succeed.\r\n\r\nWe can try breaking that target up, but the app it is testing is kind of a monolith so i don\u2019t know how successful that would be.\r\n\r\nCan you make a runtime limit in pants to handle?\r\n\r\nThis error happens in the pants setup before our pytest is run.\r\n\r\n# Workaround\r\nIn one of our plugin's `register.py` we added `sys.setrecursionlimit(1021)` and this resolved our problem.\n", "before_files": [{"content": "# Copyright 2017 Pants project contributors (see CONTRIBUTORS.md).\n# Licensed under the Apache License, Version 2.0 (see LICENSE).\n\nimport importlib\nimport locale\nimport os\nimport warnings\nfrom textwrap import dedent\n\n\nclass PantsLoader:\n    \"\"\"Loads and executes entrypoints.\"\"\"\n\n    ENTRYPOINT_ENV_VAR = \"PANTS_ENTRYPOINT\"\n    DEFAULT_ENTRYPOINT = \"pants.bin.pants_exe:main\"\n\n    ENCODING_IGNORE_ENV_VAR = \"PANTS_IGNORE_UNRECOGNIZED_ENCODING\"\n\n    class InvalidLocaleError(Exception):\n        \"\"\"Raised when a valid locale can't be found.\"\"\"\n\n    @staticmethod\n    def setup_warnings():\n        # We want to present warnings to the user, set this up before importing any of our own code,\n        # to ensure all deprecation warnings are seen, including module deprecations.\n        # The \"default\" action displays a warning for a particular file and line number exactly once.\n        # See https://docs.python.org/3/library/warnings.html#the-warnings-filter for the complete list.\n        #\n        # However, we do turn off deprecation warnings for libraries that Pants uses for which we do\n        # not have a fixed upstream version, typically because the library is no longer maintained.\n        warnings.simplefilter(\"default\", category=DeprecationWarning)\n        # TODO: Eric-Arellano has emailed the author to see if he is willing to accept a PR fixing the\n        # deprecation warnings and to release the fix. If he says yes, remove this once fixed.\n        warnings.filterwarnings(\"ignore\", category=DeprecationWarning, module=\"ansicolors\")\n        # Silence this ubiquitous warning. Several of our 3rd party deps incur this.\n        warnings.filterwarnings(\n            \"ignore\",\n            category=DeprecationWarning,\n            message=\"Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated\",\n        )\n\n    @classmethod\n    def ensure_locale(cls):\n        # Sanity check for locale, See https://github.com/pantsbuild/pants/issues/2465.\n        # This check is done early to give good feedback to user on how to fix the problem. Other\n        # libraries called by Pants may fail with more obscure errors.\n        encoding = locale.getpreferredencoding()\n        if (\n            encoding.lower() != \"utf-8\"\n            and os.environ.get(cls.ENCODING_IGNORE_ENV_VAR, None) is None\n        ):\n            raise cls.InvalidLocaleError(\n                dedent(\n                    \"\"\"\n                    Your system's preferred encoding is `{}`, but Pants requires `UTF-8`.\n                    Specifically, Python's `locale.getpreferredencoding()` must resolve to `UTF-8`.\n\n                    Fix it by setting the LC_* and LANG environment settings. Example:\n                      LC_ALL=en_US.UTF-8\n                      LANG=en_US.UTF-8\n                    Or, bypass it by setting the below environment variable.\n                      {}=1\n                    Note: we cannot guarantee consistent behavior with this bypass enabled.\n                    \"\"\".format(\n                        encoding, cls.ENCODING_IGNORE_ENV_VAR\n                    )\n                )\n            )\n\n    @staticmethod\n    def determine_entrypoint(env_var, default):\n        return os.environ.pop(env_var, default)\n\n    @staticmethod\n    def load_and_execute(entrypoint):\n        assert \":\" in entrypoint, \"ERROR: entrypoint must be of the form `module.path:callable`\"\n        module_path, func_name = entrypoint.split(\":\", 1)\n        module = importlib.import_module(module_path)\n        entrypoint_main = getattr(module, func_name)\n        assert callable(entrypoint_main), \"ERROR: entrypoint `{}` is not callable\".format(\n            entrypoint\n        )\n        entrypoint_main()\n\n    @classmethod\n    def run(cls):\n        cls.setup_warnings()\n        cls.ensure_locale()\n        entrypoint = cls.determine_entrypoint(cls.ENTRYPOINT_ENV_VAR, cls.DEFAULT_ENTRYPOINT)\n        cls.load_and_execute(entrypoint)\n\n\ndef main():\n    PantsLoader.run()\n\n\nif __name__ == \"__main__\":\n    main()\n", "path": "src/python/pants/bin/pants_loader.py"}], "after_files": [{"content": "# Copyright 2017 Pants project contributors (see CONTRIBUTORS.md).\n# Licensed under the Apache License, Version 2.0 (see LICENSE).\n\nimport importlib\nimport locale\nimport os\nimport sys\nimport warnings\nfrom textwrap import dedent\n\n\nclass PantsLoader:\n    \"\"\"Loads and executes entrypoints.\"\"\"\n\n    ENTRYPOINT_ENV_VAR = \"PANTS_ENTRYPOINT\"\n    DEFAULT_ENTRYPOINT = \"pants.bin.pants_exe:main\"\n\n    RECURSION_LIMIT_ENV_VAR = \"PANTS_RECURSION_LIMIT\"\n\n    ENCODING_IGNORE_ENV_VAR = \"PANTS_IGNORE_UNRECOGNIZED_ENCODING\"\n\n    class InvalidLocaleError(Exception):\n        \"\"\"Raised when a valid locale can't be found.\"\"\"\n\n    @staticmethod\n    def setup_warnings():\n        # We want to present warnings to the user, set this up before importing any of our own code,\n        # to ensure all deprecation warnings are seen, including module deprecations.\n        # The \"default\" action displays a warning for a particular file and line number exactly once.\n        # See https://docs.python.org/3/library/warnings.html#the-warnings-filter for the complete list.\n        #\n        # However, we do turn off deprecation warnings for libraries that Pants uses for which we do\n        # not have a fixed upstream version, typically because the library is no longer maintained.\n        warnings.simplefilter(\"default\", category=DeprecationWarning)\n        # TODO: Eric-Arellano has emailed the author to see if he is willing to accept a PR fixing the\n        # deprecation warnings and to release the fix. If he says yes, remove this once fixed.\n        warnings.filterwarnings(\"ignore\", category=DeprecationWarning, module=\"ansicolors\")\n        # Silence this ubiquitous warning. Several of our 3rd party deps incur this.\n        warnings.filterwarnings(\n            \"ignore\",\n            category=DeprecationWarning,\n            message=\"Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated\",\n        )\n\n    @classmethod\n    def ensure_locale(cls):\n        # Sanity check for locale, See https://github.com/pantsbuild/pants/issues/2465.\n        # This check is done early to give good feedback to user on how to fix the problem. Other\n        # libraries called by Pants may fail with more obscure errors.\n        encoding = locale.getpreferredencoding()\n        if (\n            encoding.lower() != \"utf-8\"\n            and os.environ.get(cls.ENCODING_IGNORE_ENV_VAR, None) is None\n        ):\n            raise cls.InvalidLocaleError(\n                dedent(\n                    \"\"\"\n                    Your system's preferred encoding is `{}`, but Pants requires `UTF-8`.\n                    Specifically, Python's `locale.getpreferredencoding()` must resolve to `UTF-8`.\n\n                    Fix it by setting the LC_* and LANG environment settings. Example:\n                      LC_ALL=en_US.UTF-8\n                      LANG=en_US.UTF-8\n                    Or, bypass it by setting the below environment variable.\n                      {}=1\n                    Note: we cannot guarantee consistent behavior with this bypass enabled.\n                    \"\"\".format(\n                        encoding, cls.ENCODING_IGNORE_ENV_VAR\n                    )\n                )\n            )\n\n    @classmethod\n    def set_recursion_limit(cls):\n        sys.setrecursionlimit(int(os.environ.get(cls.RECURSION_LIMIT_ENV_VAR, \"10000\")))\n\n    @staticmethod\n    def determine_entrypoint(env_var, default):\n        return os.environ.pop(env_var, default)\n\n    @staticmethod\n    def load_and_execute(entrypoint):\n        assert \":\" in entrypoint, \"ERROR: entrypoint must be of the form `module.path:callable`\"\n        module_path, func_name = entrypoint.split(\":\", 1)\n        module = importlib.import_module(module_path)\n        entrypoint_main = getattr(module, func_name)\n        assert callable(entrypoint_main), \"ERROR: entrypoint `{}` is not callable\".format(\n            entrypoint\n        )\n        entrypoint_main()\n\n    @classmethod\n    def run(cls):\n        cls.setup_warnings()\n        cls.ensure_locale()\n        cls.set_recursion_limit()\n        entrypoint = cls.determine_entrypoint(cls.ENTRYPOINT_ENV_VAR, cls.DEFAULT_ENTRYPOINT)\n        cls.load_and_execute(entrypoint)\n\n\ndef main():\n    PantsLoader.run()\n\n\nif __name__ == \"__main__\":\n    main()\n", "path": "src/python/pants/bin/pants_loader.py"}]}