@@ -133,12 +133,19 @@ def run
133133 end
134134 end
135135
136- def set_env_vars ( env )
136+ def with_env_vars ( env )
137+ old_env = ENV . to_hash
138+
139+ # TODO move env-filtering
137140 # Delete all env vars which are unchanged from before spring started
138141 original_env . each { |k , v | ENV . delete k if ENV [ k ] == v }
139142
140143 # Load in the current env vars, except those which *were* changed when spring started
141144 env . each { |k , v | ENV [ k ] ||= v }
145+
146+ yield
147+ ensure
148+ ENV . replace ( old_env . to_hash )
142149 end
143150
144151 def serve ( client )
@@ -150,41 +157,42 @@ def serve(client)
150157
151158 client_args , client_env = JSON . load ( client . read ( client . gets . to_i ) ) . values_at ( "args" , "env" )
152159
153- preload unless preloaded?
154- command = Spring . command ( client_args . shift )
160+ with_env_vars ( client_env ) do
161+ preload unless preloaded?
162+ command = Spring . command ( client_args . shift )
155163
156- connect_database
157- setup command
164+ connect_database
165+ setup command
158166
159- if Rails . application . reloaders . any? ( &:updated? )
160- ActionDispatch ::Reloader . cleanup!
161- ActionDispatch ::Reloader . prepare!
162- end
167+ if Rails . application . reloaders . any? ( &:updated? )
168+ ActionDispatch ::Reloader . cleanup!
169+ ActionDispatch ::Reloader . prepare!
170+ end
163171
164- pid = fork {
165- IGNORE_SIGNALS . each { |sig | trap ( sig , "DEFAULT" ) }
166- trap ( "TERM" , "DEFAULT" )
172+ pid = fork {
173+ IGNORE_SIGNALS . each { |sig | trap ( sig , "DEFAULT" ) }
174+ trap ( "TERM" , "DEFAULT" )
167175
168- ARGV . replace ( client_args )
169- $0 = command . process_title
170- set_env_vars ( client_env )
176+ ARGV . replace ( client_args )
177+ $0 = command . process_title
171178
172- # requiring is faster, so if config.cache_classes was true in
173- # the environment's config file, then we can respect that from
174- # here on as we no longer need constant reloading.
175- if @original_cache_classes
176- ActiveSupport ::Dependencies . mechanism = :require
177- Rails . application . config . cache_classes = true
178- end
179+ # requiring is faster, so if config.cache_classes was true in
180+ # the environment's config file, then we can respect that from
181+ # here on as we no longer need constant reloading.
182+ if @original_cache_classes
183+ ActiveSupport ::Dependencies . mechanism = :require
184+ Rails . application . config . cache_classes = true
185+ end
179186
180- connect_database
181- srand
187+ connect_database
188+ srand
182189
183- invoke_after_fork_callbacks
184- shush_backtraces
190+ invoke_after_fork_callbacks
191+ shush_backtraces
185192
186- command . call
187- }
193+ command . call
194+ }
195+ end
188196
189197 disconnect_database
190198 reset_streams
0 commit comments