sandboxd icon indicating copy to clipboard operation
sandboxd copied to clipboard

Interrupting the loading ("sandboxing node ...") with Ctrl-C breaks the command

Open henrebotha opened this issue 1 year ago • 1 comments

If I hit Ctrl-C while a command, e.g. node, is loading, the command is broken for that shell, resulting in "command not found" errors. It's no longer possible for sandboxd to load the command; but the command is also not available in the path.

Is there some way to change the loading function to recover gracefully from such situations? Perhaps trapping the kill signal?

henrebotha avatar Mar 01 '24 15:03 henrebotha

Hmm, this appears to be something particular with (at least in my setup) nvm. I tried adding the following to sandboxd.

diff --git c/sandboxd i/sandboxd
index 553859c..ccdb2ce 100755
--- c/sandboxd
+++ i/sandboxd
@@ -33,2 +33,4 @@ function sandbox_delete_hooks(){
 function sandbox(){
+  trap "" SIGINT
+
   local cmd=$1
@@ -41,4 +43,8 @@ function sandbox(){
     (>&2 echo "sandbox '$cmd' not found.\nHave you defined sandbox_init_$cmd(){ ... } in your sandboxrc (${RC_FILE})?")
+
+    trap - SIGINT
     return 1
   fi
+
+  trap - SIGINT
 }

Effectively, this makes Ctrl-C (SIGINT) a no-op during sandbox(), and then removes that trap after sandbox() completes.

This actually works with a simple test case.

sandbox_init_foo(){
  for i in $(seq 1 5); do
    echo Fooing "$i"
    sleep 1
  done

  foo() {
    echo Bar
  }
}

However, it fails for my more complex nvm hook.

sandbox_init_nvm(){
  source "$NVM_DIR"/nvm.sh
  autoload -U add-zsh-hook
  load_nvmrc() {
    local nvmrc_path="$(nvm_find_nvmrc)"

    if [ -n "$nvmrc_path" ]; then
      local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")

      if [ "$nvmrc_node_version" = "N/A" ]; then
        nvm install
      elif [ "$nvmrc_node_version" != "$(nvm version)" ]; then
        nvm use
      fi
    elif [ -n "$(PWD=$OLDPWD nvm_find_nvmrc)" ] && [ "$(nvm version)" != "$(nvm version default)" ]; then
      echo "Reverting to nvm default version"
      nvm use default
    fi
  }
  add-zsh-hook chpwd load_nvmrc
  load_nvmrc

  export PATH="$(yarn global bin):$PATH"
}
sandbox_hook nvm node
sandbox_hook nvm nodemon
sandbox_hook nvm yarn

So I think there's something a bit more convoluted happening here.

henrebotha avatar Mar 04 '24 10:03 henrebotha