[SOLVED] Appium for iOS Simulator fails on xcrun in first non-interactive run (or something)

EDIT: After some further debugging, it turns out that I’ve been mistaking correlation for causation: Adding a longer sleep before launching Appium makes the issue disappear. In this case, four minutes is the sweet spot. Still cannot fathom why :joy:

Platform: iOS
Appium version: 1.4.16 installed via NPM
Simulator version: Xcode 6.1.1 bundled 8.1 (12B411) iOS Simulator — iPhone 4s
Test language: wd-sync on Node.js, doing browsertesting against a number of browsers
OS: Mac OS X 10.9.5

I’m having this puzzling issue. I’m running Appium for Android and iOS and a number of other platforms connecting to a Selenium grid on a Mac Mini Server for browser testing. Everything is launched at logon by adding a BASH script, launch.command, to Login Items in the system preferences. Works great, except for one strange little detail.

launch.command makes sure that Genymotion as well as a number of Windows VirtualBoxes are started if not running, and also runs a number of screen sessions in which the Selenium grid, a Selenium node, a Selenium stand-alone instance (for desktop Safari), and two Appium instances run. The command used to launch the malfunctioning Appium instance is,

screen -dmS ios /bin/bash screen -S ios -p 0 -X stuff "while true; do appium --nodeconfig '$SCRIPTPATH/appium/ios-node.json' --port 47000; done 2>&1 3>&1 | tee log/ios.log "

When this is launched at startup, any incoming test will fail with an error like,

info: --> POST /wd/hub/session {"desiredCapabilities":{"platformVersion":"8.1","name":"authentication/authentication (mobile)/001-..-lib-clear cookies.js","browserName":"safari","javascriptEnabled":true,"platformName":"iOS","deviceName":"iPhone 4s","version":"","platform":"mac"}}
info: Client User-Agent string: Apache-HttpClient/4.5.1 (Java/1.8.0_71)
info: [debug] The following desired capabilities were provided, but not recognized by appium. They will be passed on to any other services running on this server. : name, javascriptEnabled, version, platform
info: [debug] Configuring Safari session
info: [debug] We're on iOS8+ so not copying mobile safari app
info: [debug] Creating new appium session 1a5a80f8-3344-4a67-9958-ee6156322136
info: [debug] Removing any remaining instruments sockets
info: [debug] Cleaned up instruments socket /tmp/instruments_sock
info: [debug] Setting Xcode version
warn: Support for Xcode 6.1.1 has been deprecated and will be removed in a future version. Please upgrade to version 6.3 or higher (or version 6.0.1 for iOS 8.0)
info: [debug] Xcode version set to 6.1.1
info: [debug] Setting iOS SDK Version
error: Could not determine iOS SDK version
info: [debug] Cleaning up appium session
error: Failed to start an Appium session, err was: Error: Command failed: /bin/sh -c xcrun --sdk iphonesimulator --show-sdk-version

info: [debug] Error: Command failed: /bin/sh -c xcrun --sdk iphonesimulator --show-sdk-version

    at ChildProcess.exithandler (child_process.js:213:12)
    at emitTwo (events.js:100:13)
    at ChildProcess.emit (events.js:185:7)
    at maybeClose (internal/child_process.js:821:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)
info: [debug] Responding to client with error: {"status":33,"value":{"message":"A new session could not be created. (Original error: Command failed: /bin/sh -c xcrun --sdk iphonesimulator --show-sdk-version\n)","cause":{"killed":true,"code":null,"signal":"SIGTERM","cmd":"/bin/sh -c xcrun --sdk iphonesimulator --show-sdk-version"},"isOperational":true,"killed":true,"code":null,"signal":"SIGTERM","cmd":"/bin/sh -c xcrun --sdk iphonesimulator --show-sdk-version","origValue":"Command failed: /bin/sh -c xcrun --sdk iphonesimulator --show-sdk-version\n"},"sessionId":null}
info: <-- POST /wd/hub/session 500 9199.122 ms - 525

But, if I run launch.command manually from a BASH shell via SSH or locally, the xcrun command runs successfully.

I was thinking it must be some environment variable missing, perhaps in PATH, but sourcing my .bashrc and dumping out env just before starting up Appium reveals an environment that matches exactly that which I have in a regular TTY or SSH shell. Also, xcrun is in PATH according to env.

Here’s the kicker: If I start another screen alongside the Appium one which sleeps for 60 seconds and then pkills Appium so that it restarts, it’ll no longer fail on xcrun trying to get the SDK version. Injecting a sleep before launching Appium has no effect, though. In other words, it doesn’t appear to be related to when Appium starts, but rather it seems that it needs to run once and fail, and then be restarted.

I realize this is a supremely strange issue, and now that I’ve found a (hacky) work-around I’m not as such looking for an answer, but I’m curious if anyone has any idea what could possibly cause this.

Daniel :smile: