bugfix: no infinite recursion in setup-env.sh on Cray
On Cray platforms, we rely heavily on the module system to figure out what targets, compilers, etc. are available. This unfortunately means that we shell out to the `module` command as part of platform initialization. Because we run subcommands in a shell, we can get infinite recursion if `setup-env.sh` and friends are in some init script like `.bashrc`. This fixes the infinite loop by adding guards around `setup-env.sh`, `setup-env.csh`, and `setup-env.fish`, to prevent recursive initializations of Spack. This is safe because Spack never shells out to itself, so we do not need it to be initialized in subshells. - [x] add recursion guard around `setup-env.sh` - [x] add recursion guard around `setup-env.csh` - [x] add recursion guard around `setup-env.fish`
This commit is contained in:
		| @@ -12,6 +12,13 @@ | |||||||
| #    setenv SPACK_ROOT /path/to/spack | #    setenv SPACK_ROOT /path/to/spack | ||||||
| #    source $SPACK_ROOT/share/spack/setup-env.csh | #    source $SPACK_ROOT/share/spack/setup-env.csh | ||||||
| # | # | ||||||
|  |  | ||||||
|  | # prevent infinite recursion when spack shells out (e.g., on cray for modules) | ||||||
|  | if ($?_sp_initializing) then | ||||||
|  |     exit 0 | ||||||
|  | endif | ||||||
|  | setenv _sp_initializing true | ||||||
|  |  | ||||||
| if ($?SPACK_ROOT) then | if ($?SPACK_ROOT) then | ||||||
|     set _spack_source_file = $SPACK_ROOT/share/spack/setup-env.csh |     set _spack_source_file = $SPACK_ROOT/share/spack/setup-env.csh | ||||||
|     set _spack_share_dir   = $SPACK_ROOT/share/spack |     set _spack_share_dir   = $SPACK_ROOT/share/spack | ||||||
| @@ -38,3 +45,6 @@ else | |||||||
|     echo "ERROR: Sourcing spack setup-env.csh requires setting SPACK_ROOT to " |     echo "ERROR: Sourcing spack setup-env.csh requires setting SPACK_ROOT to " | ||||||
|     echo "       the root of your spack installation." |     echo "       the root of your spack installation." | ||||||
| endif | endif | ||||||
|  |  | ||||||
|  | # done: unset sentinel variable as we're no longer initializing | ||||||
|  | unsetenv _sp_initializing | ||||||
|   | |||||||
| @@ -36,6 +36,12 @@ | |||||||
| # to come up with a user-friendly naming scheme for spack dotfiles. | # to come up with a user-friendly naming scheme for spack dotfiles. | ||||||
| ################################################################################# | ################################################################################# | ||||||
|  |  | ||||||
|  | # prevent infinite recursion when spack shells out (e.g., on cray for modules) | ||||||
|  | if test -n "$_sp_initializing" | ||||||
|  |     exit 0 | ||||||
|  | end | ||||||
|  | set -x _sp_initializing true | ||||||
|  |  | ||||||
|  |  | ||||||
| # | # | ||||||
| # Test for STDERR-NOCARET feature: if this is off, fish will redirect stderr to | # Test for STDERR-NOCARET feature: if this is off, fish will redirect stderr to | ||||||
| @@ -721,3 +727,6 @@ sp_multi_pathadd MODULEPATH $_sp_tcl_roots | |||||||
| # [3]: When the test in the if statement fails, the `status` flag is set to 1. | # [3]: When the test in the if statement fails, the `status` flag is set to 1. | ||||||
| #      `true` here manuallt resets the value of `status` to 0. Since `set` | #      `true` here manuallt resets the value of `status` to 0. Since `set` | ||||||
| #      passes `status` along, we thus avoid the function returning 1 by mistake. | #      passes `status` along, we thus avoid the function returning 1 by mistake. | ||||||
|  |  | ||||||
|  | # done: unset sentinel variable as we're no longer initializing | ||||||
|  | set -e _sp_initializing | ||||||
|   | |||||||
| @@ -39,6 +39,12 @@ | |||||||
| # spack module files. | # spack module files. | ||||||
| ######################################################################## | ######################################################################## | ||||||
|  |  | ||||||
|  | # prevent infinite recursion when spack shells out (e.g., on cray for modules) | ||||||
|  | if [ -n "${_sp_initializing:-}" ]; then | ||||||
|  |     exit 0 | ||||||
|  | fi | ||||||
|  | export _sp_initializing=true | ||||||
|  |  | ||||||
| spack() { | spack() { | ||||||
|     # Store LD_LIBRARY_PATH variables from spack shell function |     # Store LD_LIBRARY_PATH variables from spack shell function | ||||||
|     # This is necessary because MacOS System Integrity Protection clears |     # This is necessary because MacOS System Integrity Protection clears | ||||||
| @@ -357,3 +363,7 @@ _sp_multi_pathadd MODULEPATH "$_sp_tcl_roots" | |||||||
| if [ "$_sp_shell" = bash ]; then | if [ "$_sp_shell" = bash ]; then | ||||||
|     source $_sp_share_dir/spack-completion.bash |     source $_sp_share_dir/spack-completion.bash | ||||||
| fi | fi | ||||||
|  |  | ||||||
|  | # done: unset sentinel variable as we're no longer initializing | ||||||
|  | unset _sp_initializing | ||||||
|  | export _sp_initializing | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Todd Gamblin
					Todd Gamblin