Skip to content

Commit 49035e2

Browse files
authored
Append custom env vars instead of replace in Process (#171)
Append custom env vars instead of replace in Process (#168, #169) PR #162 refactored the default configuration code. This introduced a subtle bug where `env` became `[]string{}` instead of the default of `nil`. In golang, `exec.Cmd.Env == nil` means to use the "current process's environment". By setting it to `[]string{}` as a default the Process's environment was emptied out which caused an array of strange and difficult to troubleshoot behaviour. See issues #168 and #169 This commit changes the behaviour to append model configured environment variables to the default list rather than replace them.
1 parent 9963ae1 commit 49035e2

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

proxy/process.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,19 @@ func (p *Process) start() error {
189189
p.waitStarting.Add(1)
190190
defer p.waitStarting.Done()
191191
cmdContext, ctxCancelUpstream := context.WithCancel(context.Background())
192-
p.proxyLogger.Debugf("<%s> Executing start command: %s", p.ID, strings.Join(args, " "))
192+
193193
p.cmd = exec.CommandContext(cmdContext, args[0], args[1:]...)
194194
p.cmd.Stdout = p.processLogger
195195
p.cmd.Stderr = p.processLogger
196-
p.cmd.Env = p.config.Env
197-
196+
p.cmd.Env = append(p.cmd.Environ(), p.config.Env...)
198197
p.cmd.Cancel = p.cmdStopUpstreamProcess
199198
p.cmd.WaitDelay = p.gracefulStopTimeout
200199
p.cancelUpstream = ctxCancelUpstream
201200
p.cmdWaitChan = make(chan struct{})
202201

203202
p.failedStartCount++ // this will be reset to zero when the process has successfully started
203+
204+
p.proxyLogger.Debugf("<%s> Executing start command: %s, env: %s", p.ID, strings.Join(args, " "), strings.Join(p.config.Env, ", "))
204205
err = p.cmd.Start()
205206

206207
// Set process state to failed

proxy/process_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ func TestProcess_StopImmediately(t *testing.T) {
394394
// Test that SIGKILL is sent when gracefulStopTimeout is reached and properly terminates
395395
// the upstream command
396396
func TestProcess_ForceStopWithKill(t *testing.T) {
397+
if runtime.GOOS == "windows" {
398+
t.Skip("skipping SIGTERM test on Windows ")
399+
}
397400

398401
expectedMessage := "test_sigkill"
399402
binaryPath := getSimpleResponderPath()
@@ -405,7 +408,6 @@ func TestProcess_ForceStopWithKill(t *testing.T) {
405408
Cmd: fmt.Sprintf("%s --port %d --respond %s --silent --ignore-sig-term", binaryPath, port, expectedMessage),
406409
Proxy: fmt.Sprintf("http://127.0.0.1:%d", port),
407410
CheckEndpoint: "/health",
408-
CmdStop: "taskkill /f /t /pid ${PID}",
409411
}
410412

411413
process := NewProcess("stop_immediate", 2, config, debugLogger, debugLogger)
@@ -465,3 +467,27 @@ func TestProcess_StopCmd(t *testing.T) {
465467
process.StopImmediately()
466468
assert.Equal(t, process.CurrentState(), StateStopped)
467469
}
470+
471+
func TestProcess_EnvironmentSetCorrectly(t *testing.T) {
472+
expectedMessage := "test_env_not_emptied"
473+
config := getTestSimpleResponderConfig(expectedMessage)
474+
475+
// ensure that the the default config does not blank out the inherited environment
476+
configWEnv := config
477+
478+
// ensure the additiona variables are appended to the process' environment
479+
configWEnv.Env = append(configWEnv.Env, "TEST_ENV1=1", "TEST_ENV2=2")
480+
481+
process1 := NewProcess("env_test", 2, config, debugLogger, debugLogger)
482+
process2 := NewProcess("env_test", 2, configWEnv, debugLogger, debugLogger)
483+
484+
process1.start()
485+
defer process1.Stop()
486+
process2.start()
487+
defer process2.Stop()
488+
489+
assert.NotZero(t, len(process1.cmd.Environ()))
490+
assert.NotZero(t, len(process2.cmd.Environ()))
491+
assert.Equal(t, len(process1.cmd.Environ())+2, len(process2.cmd.Environ()), "process2 should have 2 more environment variables than process1")
492+
493+
}

0 commit comments

Comments
 (0)