Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MAINT: mark evil_global_disable_warn_O4O8_flag as thread-local #26676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2024

Conversation

ngoldbaum
Copy link
Member

I also looked at getting rid of the flag entirely but unfortunately the pickle format encodes dtype="O" as "O8" on 64 bit systems, coming from this line:

obj = PyUnicode_FromFormat("%c%d",self->kind, elsize);

Unfortunately we didn't change that to writing just "O" instead of "O4" or "O8" when we deprecated this and added the global flag back in 2012, so we need to keep the evil global flag. I have no idea if changing how object dtypes get written to pickle files would break other things or subtly break compatibility across numpy versions.

However, we can at least make the evil global flag thread safe by marking it as a thread-local variable, just like the promotion state.

If we ever wanted to support subinterpreters, we'd need to move this state into the module state and it would no longer need to be thread-local.

You could test this by having one thread load a pickle file and another thread create e.g. an "O4" dtype simultaneously. In practice it's tricky to actually write such a test because pytest isn't thread-safe, so I can't use e.g. pytest.warns inside a thread.

@ngoldbaum ngoldbaum changed the title MNT: mark evil_global_disable_warn_O4O8_flag as thread-local MAINT: mark evil_global_disable_warn_O4O8_flag as thread-local Jun 12, 2024
@ngoldbaum ngoldbaum added the 39 - free-threading PRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703) label Jun 12, 2024
@charris
Copy link
Member

charris commented Jun 12, 2024

The Clang problem is fixed in main.

Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name actually starts with evil :)

Seems simple enough, leaving open for a bit in case someone with more context about this flag wants to take a look.

@seberg
Copy link
Member

seberg commented Jun 13, 2024

Thanks LGTM.

If we ever wanted to support subinterpreters, we'd need to move this state into the module state and it would no longer need to be thread-local.

Subinterpreters can be free-threaded, no? So it would have to be thread-local there also, I think?
OTOH, I suspect that thread-local is sufficient (enough?) there, since I am not sure it is possible for someone to pause one subinterpreter and then run a different one in the same thread while we are inside the unpickling (C-API) function.

@seberg seberg merged commit 787ff0f into numpy:main Jun 13, 2024
68 of 69 checks passed
@ngoldbaum
Copy link
Member Author

Subinterpreters can be free-threaded, no? So it would have to be thread-local there also, I think?

You're right, it would need to be a thread-local variable in the interpreter state struct. I don't even know if such a thing is possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
03 - Maintenance 39 - free-threading PRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants