Skip to content
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

[SR-6968] ProcessInfo.processInfo.environment only gets once #4257

swift-ci opened this issue Feb 9, 2018 · 2 comments

[SR-6968] ProcessInfo.processInfo.environment only gets once #4257

swift-ci opened this issue Feb 9, 2018 · 2 comments


Copy link

swift-ci commented Feb 9, 2018

Previous ID SR-6968
Radar None
Original Reporter (JIRA User)
Type Bug
Status Resolved
Resolution Done

Swift 4.0.3

MacOS Sierra 10.12.6

Ubuntu 16.04

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug, Linux
Assignee @spevans
Priority Medium

md5: 319865f696dd2175ab81093d8934b949

Issue Description:

Ran into the same bug as Youming Lin in SR-5076 where setenv() was not working during Linux tests.

This issue was condensed down to the following code:

import Foundation

let preset = ProcessInfo.processInfo.environment["test"]
setenv("test", "worked", 1)
let postset = ProcessInfo.processInfo.environment["test"]

on macOS preset == nil and postset == "worked"
However on Linux they are both equal to nil.

Furthermore, you do not call ProcessInfo before you setenv then it does work

import Foundation

setenv("test", "worked", 1)
let postset = ProcessInfo.processInfo.environment["test"]

and on both macOS and Linux postset == "worked"

Both examples will work if you use {{getenv("test") instead of ProcessInfo.processInfo.environment["test"].

}}We looked into ProcessInfo on foundation and at:

You can see that dispatch_once is used in __CFGetEnvironment().

This means that once you access ProcessInfo.processInfo.environment the first time it will cache the result and use that for any future accesses. This means that the environment variables updated by the setenv function are not reflected in the value returned by process info. I believe this is the cause of the difference in behaviour between Linux and macOS.

To fix:

Investigate why dispatch_once was chosen for this function

Look into threading requirements if dispatch_once is removed

Copy link

belkadan commented Feb 9, 2018

cc @millenomi

Copy link

spevans commented Feb 17, 2018


@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

3 participants