[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Environment variables in dynamic modules
|
From: |
Spencer Baugh |
|
Subject: |
Re: Environment variables in dynamic modules |
|
Date: |
Thu, 11 Jan 2024 12:53:49 -0500 |
|
User-agent: |
Gnus/5.13 (Gnus v5.13) |
Eli Zaretskii <eliz@gnu.org> writes:
>> From: Spencer Baugh <sbaugh@janestreet.com>
>> Date: Thu, 11 Jan 2024 11:03:25 -0500
>>
>>
>> In Emacs, process-environment (read by Lisp getenv) is distinct from C
>> environment variables (read by C getenv).
>>
>> This means that a dynamic module which links against a library which
>> reads environment variables will not be affected by changes to
>> process-environment.
>>
>> For example, if a user calls (setenv "VAR" "value") or binds
>> process-environment to (cons (cons "VAR" "value" process-environment)),
>> a getenv("VAR") in the dynamic module library won't return "value".
>> Likewise, if a dynamic module spawns subprocesses, they will inherit the
>> environment that the Emacs process started with, not the current
>> environment in process-environment.
>>
>> This is usually unexpected, and causes difficult-to-track-down bugs,
>> especially for dynamic modules that spawn subprocesses or for large
>> dynamic modules with lots of functionality.
>>
>> There are a number of possible ways to solve it:
>>
>> A. Carefully track down every place that a library reads environment
>> variables or spawns subprocesses, and pass in the Emacs environment
>> instead.
>> (but this is intractable in modules which call other libraries)
>>
>> B. Advise Elisp setenv to also change the C environment
>> (but this doesn't work with let-bindings of process-environment)
>>
>> C. Set all variables in the C environment to match process-environment
>> every time we call into the dynamic module
>> (but this is slow and hurts performance)
>>
>> D. Use linker tricks to replace C getenv with a version which calls back
>> into Emacs.
>> (but this doesn't work on other threads, since we can only call into
>> Emacs from the main thread)
>>
>> None of these are particularly satisfying. I have implemented D, but
>> since my module uses multiple threads, it doesn't really solve the
>> problem for me.
>>
>> Any suggestions?
>
> The only correct solution is C,
Fair.
> and you should only do that in the module when the module calls
> something that really needs the environment.
Determining when that happens is the same task as A. It's not possible
to know in a large module which calls many libraries all the places
which might read environment variables. Some library might run a
subprocess in any function, which needs the environment.
So therefore, in a large dynamic module, C needs to be done every time
we call into the dynamic module.
> I don't know why you say C is slow; did you time it? You could, of
> course, set only some of the environment variables, those that matter,
> instead of setting all of them.
It is not too slow if it is done only when needed. It is too slow to do
on every call into the dynamic module.
On the other hand, maybe it could be optimized. On every call into the
dynamic module I could check whether process-environment is eq to the
last process-environment value, and if it's not, only then synchronize
the environment. Except setenv updates process-environment with setcdr,
so I can't just check eq. Maybe I could also advise setenv to catch
this case.