Skip to content

Maintain environment variable case insensitivity for Windows after worker specialization #600

@hossam-nasr

Description

@hossam-nasr

This issue was prompted by this CRI: a customer was facing frequent crashes on some Windows workers with an error that a particular environment variable is not set. After investigation, it turned out that the environment variable in question was being set in app settings and referenced from process.env using different casing, and this issue was only occurring when the worker had undergone specialization (after being sent a functionEnvironmentReloadRequest).

Normally, in Windows, setting/getting environment variables is expected to be case-insensitive, and process.env in node treats environment variables case insensitively on Windows. Example:

PS C:\Users\hossamnasr> $Env:test="hello"
PS C:\Users\hossamnasr> $Env:test
hello
PS C:\Users\hossamnasr> $Env:TeSt
hello
PS C:\Users\hossamnasr> node
Welcome to Node.js v16.13.0.
Type ".help" for more information.
> process.env['test']
'hello'
> process.env['TeSt']
'hello'

However, that behavior is not maintained after worker specialization. This is because in the functionEnvironmentReloadHandler here, we are overriding the process.env object with a new object we get from the Host:

process.env = Object.assign({}, msg.environmentVariables);

So now, environment variables behave case sensitively:

> process.env = Object.assign({}, {'test': 'hello'})
{ test: 'hello' }
> process.env['test']
'hello'
> process.env['TeSt']
undefined

Instead, we should be setting each environment variable we get from the Host one by one, to ensure we maintain case insensitivity and not override the entire process.env object:

> process.env['test'] = 'hello'
'hello'
> process.env['test']
'hello'
> process.env['TeSt']
'hello'

Repro steps

  1. Create a function app with any trigger on Windows
  2. Set some environment variable foo in app settings
  3. Reference this environment variable in your function using different casing (e.g. fOo):
const timerTrigger: AzureFunction = async function (context: Context, myTimer: any): Promise<void> {
    if (process.env['fOo'] === undefined) {
        throw new Error("Env variable undefined :(")
    }
};

Expected behavior

App works normally without crashes.

Actual behavior

Occasionally, the app will crash with error "Env variable undefined :(" if a worker is specialized

Known workarounds

Treat app settings case sensitively for Windows

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions