Skip to content

Conversation

@benoithudson
Copy link
Contributor

@benoithudson benoithudson commented Aug 23, 2019

On most platforms, the environ symbol is accessible everywhere.

In a dylib on OSX, it's not easily accessible, you need to find it with
_NSGetEnviron.

The code was caching the value of environ. But a setenv can change the value,
leaving garbage at the old value. Fix: don't cache the value of environ, just
read it every time.

https://bugs.python.org/issue37931

On most platforms, the `environ` symbol is accessible everywhere.

In a dylib on OSX, it's not easily accessible, you need to find it with
_NSGetEnviron.

The code was caching the *value* of environ. But a setenv can change the value,
leaving garbage at the old value. Fix: don't cache the value of environ, just
read it every time.
@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA (this might be simply due to a missing "GitHub Name" entry in your b.p.o account settings). This is necessary for legal reasons before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for your contribution, we look forward to reviewing it!

@benoithudson benoithudson changed the title Fix issue 37931: crash on OSX re-initializing os.environ bpo-37931: crash on OSX re-initializing os.environ Aug 23, 2019
@ned-deily
Copy link
Member

@ronaldoussoren should review this

@ned-deily ned-deily removed their request for review October 13, 2019 03:02
Copy link
Contributor

@ronaldoussoren ronaldoussoren left a comment

Choose a reason for hiding this comment

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

The patch looks good, but its possible to centralise the macOS specific code a bit more, see my comments below.

@benoithudson
Copy link
Contributor Author

How does backporting work -- do I prepare a new PR for those or does someone else take care of it?

I'd ideally like to backport to 2.7 since that's where I actually hit the bug. This part of the codebase hasn't changed in a long time.

@ned-deily
Copy link
Member

@benoithudson, backporting is attempted automatically when a core developer adds the appropriate "needs backport to ..." labels to the PR, as @ronaldoussoren has done. It could be considered for 2.7.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM.

@benoithudson
Copy link
Contributor Author

I can't add labels; if someone can add needs backport to 2.7 that would be great.

Copy link
Contributor

@ronaldoussoren ronaldoussoren left a comment

Choose a reason for hiding this comment

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

LGTM

@ronaldoussoren
Copy link
Contributor

That's annoying: "squash and merge" fails for me at the moment, and twice in a row ("Merge attempt failed"). Will try again later.

@benoithudson
Copy link
Contributor Author

Any luck trying again?

@vstinner
Copy link
Member

vstinner commented Dec 6, 2019

The change looks perfectly safe to me. I cannot test it, but I trust @benoithudson managed to confirm that his change fix his crash.

@vstinner vstinner merged commit 723f71a into python:master Dec 6, 2019
@miss-islington
Copy link
Contributor

Thanks @benoithudson for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 2.7, 3.7, 3.8.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 6, 2019
On most platforms, the `environ` symbol is accessible everywhere.

In a dylib on OSX, it's not easily accessible, you need to find it with
_NSGetEnviron.

The code was caching the *value* of environ. But a setenv() can change the value,
leaving garbage at the old value. Fix: don't cache the value of environ, just
read it every time.
(cherry picked from commit 723f71a)

Co-authored-by: Benoit Hudson <benoit@imgspc.com>
@bedevere-bot
Copy link

GH-17488 is a backport of this pull request to the 3.8 branch.

@miss-islington
Copy link
Contributor

Sorry, @benoithudson and @vstinner, I could not cleanly backport this to 3.7 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 723f71abf7ab0a7be394f9f7b2daa9ecdf6fb1eb 3.7

@miss-islington
Copy link
Contributor

Sorry @benoithudson and @vstinner, I had trouble checking out the 2.7 backport branch.
Please backport using cherry_picker on command line.
cherry_picker 723f71abf7ab0a7be394f9f7b2daa9ecdf6fb1eb 2.7

@vstinner
Copy link
Member

vstinner commented Dec 6, 2019

@benoithudson: The automated backport to 2.7 and 3.7 failed. Would you mind to attempt to backport manually the change starting with "git cherry-pick -x 723f71a" or use cherry_picker, as explained in previous comments.

@benoithudson
Copy link
Contributor Author

Sure, I'll try to do that this weekend.

miss-islington added a commit that referenced this pull request Dec 6, 2019
On most platforms, the `environ` symbol is accessible everywhere.

In a dylib on OSX, it's not easily accessible, you need to find it with
_NSGetEnviron.

The code was caching the *value* of environ. But a setenv() can change the value,
leaving garbage at the old value. Fix: don't cache the value of environ, just
read it every time.
(cherry picked from commit 723f71a)

Co-authored-by: Benoit Hudson <benoit@imgspc.com>
shihai1991 pushed a commit to shihai1991/cpython that referenced this pull request Jan 31, 2020
On most platforms, the `environ` symbol is accessible everywhere.

In a dylib on OSX, it's not easily accessible, you need to find it with
_NSGetEnviron.

The code was caching the *value* of environ. But a setenv() can change the value,
leaving garbage at the old value. Fix: don't cache the value of environ, just
read it every time.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants