Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0004499OCamlplatform support (windows, cross-compilation, etc)public2008-02-05 18:102018-04-05 23:17
Assigned Tonojebar 
PlatformOSWindowsOS Version
Product Version 
Target VersionlaterFixed in Version 
Summary0004499: Changes to the environment are invisible to Sys.getenv
DescriptionWindows has functions like SetEnvironmentVariable/GetEnvironmentVariable to access the process environment. The MS C runtime library also has getenv/putenv functions, but they operate on a snapshot of the environment taken when the process started. putenv also push modifications to the real environment through SetEnvironmentVariable.

So, if we mix in a single process OCaml code and other native code that set environment variables directly (from .Net, we only have SetEnvironmentVariable, not putenv), these changes are not seen from OCaml, which uses getenv.

A fix would be to use the "native" win32 functions for OCaml (not only for implementing Sys.getenv, but also for all the calls to getenv in the runtime system; and some care is needed for execve).

(At LexiFi, we use a temporary solution: we have a function to "refresh" the local copy of the environment in the libc by fetching all the real environment variable and putenv'ing them.)
TagsNo tags attached.
Attached Filesc file icon getenv_test.c [^] (479 bytes) 2017-06-18 09:52 [Show Content]

- Relationships
related to 0006903resolved Unix.execvpe doesn't change environment on Cygwin and maybe Windows 

-  Notes
doligez (administrator)
2008-02-19 14:49

I could easily argue that this is a bug in the getenv of the MS C runtime library.
frisch (developer)
2012-06-20 11:25

> I could easily argue that this is a bug in the getenv of the MS C runtime library.

Probably, but it's easier (for us) to work-around the problem on the OCaml side than to convince Microsoft to fix their C runtime. As long as the effort remains reasonable, I think it's good to provide a coherent semantics between all the supported OCaml ports, even if this requires to work around bugs created by third parties.
doligez (administrator)
2012-09-15 11:57

The right solution is probably to define our own functions sys_getenv and sys_putenv once with system-dependent code, and then use them everywhere.

In fact, from your description it seems we only need to redefine getenv.
xleroy (administrator)
2013-07-29 16:56

Pushing this for later, as I don't see the issue fixed in time for 4.01.

adrien (reporter)
2015-06-15 22:42

What would be the functions involved? From a very very quick glance at MSDN, I don't see links to similar functions.
xleroy (administrator)
2015-11-20 18:31

I don't see this issue actively worked on, so I'm pushing it for after 4.03.

In the meantime it would be good to check whether the issue is still there in more recent versions of the MSVC runtime library.
nojebar (developer)
2017-06-18 09:52

Just to report that the issue persists in the MSVC bundled with the latest Visual Studio 2017.

Surprisingly enough, the Unicode version of getenv (_wgetenv) does not seem to have this problem! So if we manage to get [^] merged, we will also be fixing this issue.

I uploaded getenv_test.c that exhibits both points. To compile, simply do cl getenv_test.c in a Developer Command Prompt.
frisch (developer)
2017-09-25 15:19

Fixed as part of GPR#1200.
nojebar (developer)
2017-09-25 15:45

Unfortunately, I was too hasty about this. It turns out that the snapshot of the environment is taken lazily at the first call to _wgetenv, but not updated after that, so the problem remains depending on the precise sequence of calls.

The solution is probably to just switch to the native Win32 API {Get,Set}EnvironmentVariable.
dra (developer)
2017-09-27 11:02

@nojb - it should be the case that _wenviron is also initialised at program startup now, that's part of the reason for using wmain.

FTR, here's the full link to the Microsoft docs on how _environ and _wenviron work: [^]
gasche (administrator)
2018-04-05 22:26

Can this be marked as resolved now that [^]

was merged?
nojebar (developer)
2018-04-05 23:17

Yes, done.

- Issue History
Date Modified Username Field Change
2008-02-05 18:10 frisch New Issue
2008-02-19 14:49 doligez Note Added: 0004452
2008-02-19 14:49 doligez Status new => acknowledged
2012-06-20 11:20 frisch Category OCaml general => OCaml windows
2012-06-20 11:25 frisch Note Added: 0007582
2012-07-11 15:58 doligez Target Version => 4.01.0+dev
2012-07-31 13:37 doligez Target Version 4.01.0+dev => 4.00.1+dev
2012-09-15 11:57 doligez Note Added: 0008078
2012-09-15 11:57 doligez Target Version 4.00.1+dev => 4.00.2+dev
2013-07-09 16:09 doligez Target Version 4.00.2+dev => 4.01.0+dev
2013-07-29 16:56 xleroy Note Added: 0009989
2013-07-29 16:56 xleroy Target Version 4.01.0+dev => 4.01.1+dev
2014-05-25 20:20 doligez Target Version 4.01.1+dev => 4.02.0+dev
2014-07-30 23:21 doligez Target Version 4.02.0+dev => 4.02.1+dev
2014-09-04 00:25 doligez Target Version 4.02.1+dev => undecided
2014-09-26 16:53 doligez Target Version undecided => 4.02.2+dev / +rc1
2015-03-18 17:52 frisch Target Version 4.02.2+dev / +rc1 => 4.02.3+dev
2015-06-15 10:27 frisch Relationship added related to 0006903
2015-06-15 22:42 adrien Note Added: 0014086
2015-07-15 16:20 doligez Target Version 4.02.3+dev => 4.03.0+dev / +beta1
2015-11-20 18:31 xleroy Note Added: 0014757
2015-11-20 18:31 xleroy Target Version 4.03.0+dev / +beta1 => later
2017-02-23 16:46 doligez Category OCaml windows => platform support (windows, etc)
2017-02-23 17:16 doligez Category platform support (windows, etc) => platform support (windows, cross-compilation, etc)
2017-06-18 09:52 nojebar Note Added: 0017914
2017-06-18 09:52 nojebar File Added: getenv_test.c
2017-09-25 15:19 frisch Note Added: 0018326
2017-09-25 15:19 frisch Status acknowledged => resolved
2017-09-25 15:19 frisch Fixed in Version => 4.06.0 +dev/beta1/beta2/rc1
2017-09-25 15:19 frisch Resolution open => fixed
2017-09-25 15:19 frisch Assigned To => frisch
2017-09-25 15:45 nojebar Note Added: 0018330
2017-09-25 15:47 frisch Assigned To frisch =>
2017-09-25 15:47 frisch Status resolved => confirmed
2017-09-25 15:47 frisch Fixed in Version 4.06.0 +dev/beta1/beta2/rc1 =>
2017-09-27 11:02 dra Note Added: 0018352
2018-04-05 22:26 gasche Note Added: 0018979
2018-04-05 23:16 nojebar Status confirmed => resolved
2018-04-05 23:16 nojebar Assigned To => nojebar
2018-04-05 23:17 nojebar Note Added: 0018980

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker