Skip to content

Environment variables changed by rails cannot be reloaded #653

Open
@mildred

Description

@mildred

The following piece of code handle environment variable reloads in Spring

# Delete all env vars which are unchanged from before spring started
original_env.each { |k, v| ENV.delete k if ENV[k] == v }
# Load in the current env vars, except those which *were* changed when spring started
env.each { |k, v| ENV[k] ||= v }

What it does is:

  • iterate over environment variables from when spring was started and remove those that rails did not change
  • iterate over client environment variables and if those are not present in the environment already, replace them

My problem is that I have a Rails application where there is an environment variable that triggers if the app is in master mode or slave mode. I use ENV! to manage environment variable, and if a variable is not set, ENV! sets a default value. The following sequence of events cause an issue:

  • spring server starts without the MASTER environment variable
  • app sets MASTER=false which is its default value
  • some time later...
  • I start the app again with MASTER=true on the command-line
  • Spring server receives a client request with MASTER=true
  • the process above is followed:
    • it considers MASTER to be unset when the spring server started but it has now MASTER=false. It received a client request with MASTER=true
    • it does not removes MASTER from the environment because it sees the variable changed (fron no value to false)
    • it does not replace MASTER=false with MASTER=true because it already has a value.
  • the app is started in slave mode despite having specified MASTER=true on the command-line.

I don't know the rationale behind this code, but in some cases it is completely wrong.

I believe this might be a reason behing the #420 issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions