cli: Running multiple `cf set-env` commands in parallel has inconsistent results

If you run multiple cf set-env commands at the same time, the results are inconsistent and never correct.

Here’s an example script to replicate this:

#!/bin/bash
export CF_TRACE=trace.log
counter=1
while [ $counter -le 4 ]
do
    envName="testEnv$counter"
    cf set-env $1 $envName $envName &
    counter=$((counter+1))
done
wait
echo All done

The expected result would be to have four environment variables set like “testEnv1” through “testEnv4”. What happens in practice is that you end up with one or two of the variables set.

Looking at the trace logs, you can see what is happening. The command first sends a GET request to pull the current list of environment variables and then sends a PUT request to update it with the new environment variable. Each separate cf set-env process runs the GET and sees the initial state of the environment variables. It then runs the PUT with a single new variable added. Unfortunately which ever one runs last wins and steps on the other changes.

The use case for this is when there are a large number of environment variables being set. Running multiple cf set-env commands in parallel can speed things up.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 17 (5 by maintainers)

Most upvoted comments

You could try cf curl -X PUT /v2/apps/<app-guid> -d '{"environment_json":{"ENV1":"VAL1", "ENV2": "VAL2"}}'. That overwrites everything and replaces it with what is in the JSON, so if you have existing env variables you’d have to include those as well as the new ones you want to set.

+1 I like the env1=val1 style too. A couple comments…

  • Currently env variable names can have spaces too. Would “some env”=“some val” work? or “some env=some val”?
  • Can we drop the comma after each pair? env1=val1 env2=val2 ...