BigSurBug: helper apps won't launch reliably from shell

I have a main app which launches one or more copies of helper apps stored in the .app/Contents/Helpers/ folder. The apps are launched with the following code:

Window1.helpers() as Shell  

 Window1.LaunchHelpers
 const numberHelperApps = 8
  for i as integer = 1 to numberHelperApps
    dim cmd as string = "env FLAGS=-foobar" + str(i)  + " open -gn " +  appFile.shellPath
    dim sh as new shell 
    sh.mode = 1 
    sh.Execute cmd
    helpers.append sh // keep a reference so the shell never goes out of scope and gets destroyed
 next

This has worked well for years across multiple macOS versions. Suddently, in Big Sur, it’s inconsistent - sometimes I get 8 helpers launched, sometimes 7, 6, 4 or even just one.

However, if I run the equivalent command in Terminal:

for i in {1..8}; do echo $i; open -gn MyApp.app/Contents/Helpers/MyHelper.app ; done

I always get 8 apps launched.

Any ideas why?

If you run a test script from the shell that logs to a file do you always get 8 runs?

On further investigation

  • my actual code is using the & operator in the shell commend, e.g.
    open -gn MyApp.app/Contents/Helpers/MyHelper.app &
  • and I’m also using mode 1 (asynchronous) shells.

So my theory is that perhaps in Big Sur, the command open -gn if run quickly enough, is not detecting the very first launch of the app (the “-n” option means “open a new instance even if it’s running”. The “&” in the shell command means “run this command in the background, don’t wait for it to finish”

Setting the Shell to mode 0 (synchronous) and removing the background “&” from the command seems to fix the problem, at some cost in slower launch time for the main app.

Dont you need a “;” after the env and before the open?

You might think so, but actually the “env” command takes parameters followed by the next command, so
env foo=bar echo "hello"
is valid BASH

I know it’s valid, but i suspect it might have something to do with how mac allows the “open” command to be used.

Possible, but whatever it is worked fine for over 10 years until Big Sur…

Add that to the growing list.