Eric Snow | ee536b2 | 2019-09-11 19:49:45 +0100 | [diff] [blame] | 1 | ####################################### |
| 2 | # C Globals and CPython Runtime State. |
| 3 | |
| 4 | CPython's C code makes extensive use of global variables (whether static |
| 5 | globals or static locals). Each such variable falls into one of several |
| 6 | categories: |
| 7 | |
| 8 | * strictly const data |
| 9 | * used exclusively in main or in the REPL |
| 10 | * process-global state (e.g. managing process-level resources |
| 11 | like signals and file descriptors) |
| 12 | * Python "global" runtime state |
| 13 | * per-interpreter runtime state |
| 14 | |
| 15 | The last one can be a problem as soon as anyone creates a second |
| 16 | interpreter (AKA "subinterpreter") in a process. It is definitely a |
| 17 | problem under subinterpreters if they are no longer sharing the GIL, |
| 18 | since the GIL protects us from a lot of race conditions. Keep in mind |
| 19 | that ultimately *all* objects (PyObject) should be treated as |
| 20 | per-interpreter state. This includes "static types", freelists, |
| 21 | _PyIdentifier, and singletons. Take that in for a second. It has |
| 22 | significant implications on where we use static variables! |
| 23 | |
| 24 | Be aware that module-global state (stored in C statics) is a kind of |
| 25 | per-interpreter state. There have been efforts across many years, and |
| 26 | still going, to provide extension module authors mechanisms to store |
| 27 | that state safely (see PEPs 3121, 489, etc.). |
| 28 | |
| 29 | (Note that there has been discussion around support for running multiple |
| 30 | Python runtimes in the same process. That would ends up with the same |
| 31 | problems, relative to static variables, that subinterpreters have.) |
| 32 | |
| 33 | Historically we have been bad at keeping per-interpreter state out of |
| 34 | static variables, mostly because until recently subinterpreters were |
| 35 | not widely used nor even factored in to solutions. However, the |
| 36 | feature is growing in popularity and use in the community. |
| 37 | |
| 38 | Mandate: "Eliminate use of static variables for per-interpreter state." |
| 39 | |
| 40 | The "c-statics.py" script in this directory, along with its accompanying |
| 41 | data files, are part of the effort to resolve existing problems with |
| 42 | our use of static variables and to prevent future problems. |
| 43 | |
| 44 | #------------------------- |
| 45 | ## statics for actually-global state (and runtime state consolidation) |
| 46 | |
| 47 | In general, holding any kind of state in static variables |
| 48 | increases maintenance burden and increases the complexity of code (e.g. |
| 49 | we use TSS to identify the active thread state). So it is a good idea |
| 50 | to avoid using statics for state even if for the "global" runtime or |
| 51 | for process-global state. |
| 52 | |
| 53 | Relative to maintenance burden, one problem is where the runtime |
| 54 | state is spread throughout the codebase in dozens of individual |
| 55 | globals. Unlike the other globals, the runtime state represents a set |
| 56 | of values that are constantly shifting in a complex way. When they are |
| 57 | spread out it's harder to get a clear picture of what the runtime |
| 58 | involves. Furthermore, when they are spread out it complicates efforts |
| 59 | that change the runtime. |
| 60 | |
| 61 | Consequently, the globals for Python's runtime state have been |
| 62 | consolidated under a single top-level _PyRuntime global. No new globals |
| 63 | should be added for runtime state. Instead, they should be added to |
| 64 | _PyRuntimeState or one of its sub-structs. The tools in this directory |
| 65 | are run as part of the test suite to ensure that no new globals have |
| 66 | been added. The script can be run manually as well: |
| 67 | |
| 68 | ./python Lib/test/test_c_statics/c-statics.py check |
| 69 | |
| 70 | If it reports any globals then they should be resolved. If the globals |
| 71 | are runtime state then they should be folded into _PyRuntimeState. |
| 72 | Otherwise they should be marked as ignored. |