-
Couldn't load subscription status.
- Fork 44
Description
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']
undefinedInstead, 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
- Create a function app with any trigger on Windows
- Set some environment variable
fooin app settings - 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