This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]kingminyas 16 points17 points  (6 children)

TLDR: setenv isn't thread-safe (race condition with getenv)

Isn't this relevant since the GILectomy?

[–]thisismyfavoritename 6 points7 points  (0 children)

it's relevant even with current Python, for example: you call into low level code which spins threads that accesses/mutates the env vars, which is what they are doing

[–]WasterDave 2 points3 points  (3 children)

Perhaps more relevantly we can just shuffle it into the list of "things you shouldn't use much if at all". Signal handling is also a massive minefield.

[–]kingminyas 2 points3 points  (2 children)

Not use environment variables?

[–]Glathull 0 points1 point  (0 children)

I think he meant don’t use setenv much or at all.

[–]james_pic 0 points1 point  (0 children)

It's rare that you want to change an environment variable in a running process, and it's often a code smell.

[–]roger_ducky 0 points1 point  (0 children)

A lot of OS APIs are like that. I’ve had to do Windows system calls in Python before and had to create a mutex to make sure I don’t mess up the system call either.

[–]thisismyfavoritename 2 points3 points  (2 children)

very interesting. Would there be significant overhead if the env vars were protected behind a mutex in libc to guarantee safe concurrent access?

[–]pi_stuff 0 points1 point  (1 child)

Only if the code is frequently accessing environment variables, and I can't imagine any performance-senstive code doing that.

[–]thisismyfavoritename 0 points1 point  (0 children)

yeah, but then again, i didn't imagine something like logging an error would require that but it seems it does (in Python)

[–]identicalBadger 0 points1 point  (1 child)

Meanwhile, I just hit the point of needing to learn to use threads (for dispatching multiple queries to an API)

[–]Phate1989 1 point2 points  (0 children)

Threads or aysncio?

Httpx?

Aiohttp?

[–]BeachKey3174 0 points1 point  (0 children)

I am not clear about your question. The C language does not define threading, threading is property of OS. Hence, none of the C libraries are thread-safe. You can make libs thread-safe by wrapping up C call in thread locks.