Appium server not killing running iOS simulator if an app is running

When I start a test - without a simulator running - I see these lines in the log:

debug: Starting iOS 7.* simulator log capture
debug: Killing the simulator process
debug: Killing any other simulator daemons

I infer from those that if I do have a simulator running when I start a test, that the Appium server will kill them. However, if I have the simulator running and an app running as well - it doesn’t. What I get instead is:

error: Failed to start an Appium session, err was: Error: Requested a new session but one was in progress
debug: Error: Requested a new session but one was in progress
at Appium.start (/usr/local/share/npm/lib/node_modules/appium/lib/appium.js:110:15)
at Object.exports.createSession [as handle] (/usr/local/share/npm/lib/node_modules/appium/lib/server/controller.js:159:16)
at next_layer (/usr/local/share/npm/lib/node_modules/appium/node_modules/express/lib/router/route.js:103:13)
at Route.dispatch (/usr/local/share/npm/lib/node_modules/appium/node_modules/express/lib/router/route.js:107:5)
at /usr/local/share/npm/lib/node_modules/appium/node_modules/express/lib/router/index.js:213:24
at Function.proto.process_params (/usr/local/share/npm/lib/node_modules/appium/node_modules/express/lib/router/index.js:286:12)
at next (/usr/local/share/npm/lib/node_modules/appium/node_modules/express/lib/router/index.js:207:19)
at next (/usr/local/share/npm/lib/node_modules/appium/node_modules/express/lib/router/index.js:182:38)
at next (/usr/local/share/npm/lib/node_modules/appium/node_modules/express/lib/router/index.js:182:38)
at next (/usr/local/share/npm/lib/node_modules/appium/node_modules/express/lib/router/index.js:182:38)
debug: Responding to client with error: {“status”:33,“value”:{“message”:“A new session could not be created. (Original error: Requested a new session but one was in progress)”,“origValue”:“Requested a new session but one was in progress”},“sessionId”:“0adc73fb-39cf-48f3-b460-1cc2077401d4”}

You could enable the --session-override flag.

Thx. I did that. And indeed the running simulator does indeed get clobbered.

But, when I run the test a second time (without restarting the server), after the simulator gets launched and my app opens, the test fails with org.openqa.selenium.remote.SessionNotFoundException. The only way around this is to restart the server.

Log:
info: --> POST /wd/hub/session {“desiredCapabilities”:{“newCommandTimeout”:900,“platformVersion”:“7.1”,“app”:"/Users/jblaze/Library/Developer/Xcode/DerivedData/Soda-dcldacmuyckglggreroxonotfoyr/Build/Products/Release-iphonesimulator/SodaDev.app",“platformName”:“iOS”,“deviceName”:“iPhone Simulator”,“browserName”:"",“locale”:“en_US”,“language”:“en”}}
debug: Appium request initiated at /wd/hub/session
debug: Request received with params: {“desiredCapabilities”:{“newCommandTimeout”:900,“platformVersion”:“7.1”,“app”:"/Users/jblaze/Library/Developer/Xcode/DerivedData/Soda-dcldacmuyckglggreroxonotfoyr/Build/Products/Release-iphonesimulator/SodaDev.app",“platformName”:“iOS”,“deviceName”:“iPhone Simulator”,“browserName”:"",“locale”:“en_US”,“language”:“en”}}
debug: Using local app from desired caps: /Users/jblaze/Library/Developer/Xcode/DerivedData/Soda-dcldacmuyckglggreroxonotfoyr/Build/Products/Release-iphonesimulator/SodaDev.app
debug: Creating new appium session 82f615d1-db10-4ff7-8dd9-b096a63bf6b9
debug: Removing any remaining instruments sockets
debug: Cleaned up instruments socket /tmp/instruments_sock
debug: Cleaning up any tracedirs
debug: Cleaned up /usr/local/share/npm/lib/node_modules/appium/instrumentscli0.trace
debug: Setting Xcode folder
debug: Setting Xcode version
debug: Setting iOS SDK Version
debug: iOS SDK Version set to 7.1
debug: Detecting automation tracetemplate
debug: Not auto-detecting udid, running on sim
debug: No Localizable.strings for language ‘en’, getting default strings
warn: Could not parse plist file at /Users/jblaze/Library/Developer/Xcode/DerivedData/Soda-dcldacmuyckglggreroxonotfoyr/Build/Products/Release-iphonesimulator/SodaDev.app/en.lproj/Localizable.strings
debug: Setting locale with command /usr/local/share/npm/lib/node_modules/appium/build/ios-sim-locale -sdk 6.1 -language en -locale en_US
debug: Setting locale with command /usr/local/share/npm/lib/node_modules/appium/build/ios-sim-locale -sdk 7.0 -language en -locale en_US
debug: Setting locale with command /usr/local/share/npm/lib/node_modules/appium/build/ios-sim-locale -sdk 7.1 -language en -locale en_US
debug: Setting locale with command /usr/local/share/npm/lib/node_modules/appium/build/ios-sim-locale -sdk 7.1-64 -language en -locale en_US
debug: Locale was set
debug: Creating instruments
debug: Preparing uiauto bootstrap
debug: Dynamic bootstrap dir: /Users/jblaze/Library/Application Support/appium/bootstrap
debug: Dynamic env: {“nodePath”:"/usr/local/Cellar/node/0.10.18/bin/node",“commandProxyClientPath”:"/usr/local/share/npm/lib/node_modules/appium/node_modules/appium-uiauto/bin/command-proxy-client.js"}
debug: Dynamic bootstrap code: // This file is automatically generated. Do not manually modify!

#import “/usr/local/share/npm/lib/node_modules/appium/node_modules/appium-uiauto/uiauto/bootstrap.js”;

bootstrap({
nodePath: “/usr/local/Cellar/node/0.10.18/bin/node”,
commandProxyClientPath: “/usr/local/share/npm/lib/node_modules/appium/node_modules/appium-uiauto/bin/command-proxy-client.js”,
});
debug: Dynamic bootstrap path: /Users/jblaze/Library/Application Support/appium/bootstrap/bootstrap-84502a623cd1caa2.js
debug: Reusing dynamic bootstrap: /Users/jblaze/Library/Application Support/appium/bootstrap/bootstrap-84502a623cd1caa2.js
debug: No iOS / app preferences to set
debug: Starting iOS 7.* simulator log capture
debug: Killing the simulator process
debug: Killing any other simulator daemons
debug: [INST] 2014-09-19 18:30:38 +0000 Fail: The target application appears to have died

debug: Checking whether instruments supports our device string
debug: Getting list of devices instruments supports
debug: Instruments is at: /Applications/Xcode.app/Contents/Developer/usr/bin/instruments
debug: [INST] Instruments Trace Complete (Duration : 118.188560s; Output : /usr/local/share/npm/lib/node_modules/appium/instrumentscli0.trace)

debug: [INSTSERVER] Instruments exited with code 0
debug: Cleaning up after instruments exit
debug: Instruments exited unexpectedly
debug: Destroying instruments client socket.
debug: Closing socket server.
debug: Instruments socket server was closed
debug: Deleted tracedir we heard about from instruments (/usr/local/share/npm/lib/node_modules/appium/instrumentscli0.trace)
debug: Stopping iOS log capture
debug: Killing the simulator process
debug: Killing any other simulator daemons
debug: Cleaning app state.
debug: Deleted /Users/jblaze/Library/Application Support/iPhone Simulator/7.1-64/Applications
debug: Deleted /Users/jblaze/Library/Application Support/iPhone Simulator/7.1-64/Library/TCC
debug: Deleted /Users/jblaze/Library/Application Support/iPhone Simulator/7.1-64/Library/Caches/locationd
debug: Deleted /Users/jblaze/Library/Application Support/iPhone Simulator/7.1-64/Media
debug: Deleted /Users/jblaze/Library/Application Support/iPhone Simulator/7.1-64/Library/Keychains
debug: Cleaning up appium session
debug: Cleaning app state.
debug: No folders found to remove
debug: No device id or app, not installing to real device.
debug: Starting command proxy.
debug: Instruments socket server started at /tmp/instruments_sock
debug: Starting instruments
debug: Instruments is at: /Applications/Xcode.app/Contents/Developer/usr/bin/instruments
info: Launching instruments
debug: Instruments device was changed from:" iPhone - Simulator - iOS 7.1 " to:" iPhone Retina (4-inch 64-bit) - Simulator - iOS 7.1"
debug: Attempting to run app on iPhone Retina (4-inch 64-bit) - Simulator - iOS 7.1
debug: Spawning instruments with command: /Applications/Xcode.app/Contents/Developer/usr/bin/instruments -t /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate -w iPhone Retina (4-inch 64-bit) - Simulator - iOS 7.1 /Users/jblaze/Library/Developer/Xcode/DerivedData/Soda-dcldacmuyckglggreroxonotfoyr/Build/Products/Release-iphonesimulator/SodaDev.app -e UIASCRIPT /Users/jblaze/Library/Application Support/appium/bootstrap/bootstrap-84502a623cd1caa2.js -e UIARESULTSPATH /tmp/appium-instruments
debug: And extra without-delay env: {“DYLD_INSERT_LIBRARIES”:"/usr/local/share/npm/lib/node_modules/appium/node_modules/appium-instruments/thirdparty/iwd/InstrumentsShim.dylib",“LIB_PATH”:"/usr/local/share/npm/lib/node_modules/appium/node_modules/appium-instruments/thirdparty/iwd"}
debug: And launch timeouts (in ms): {“global”:90000}
debug: [INST STDERR] 2014-09-19 11:30:45.645 ScriptAgent[46861:3107] CLTilesManagerClient: initialize, sSharedTilesManagerClient

debug: [INST STDERR] 2014-09-19 11:30:45.645 ScriptAgent[46861:3107] CLTilesManagerClient: init
2014-09-19 11:30:45.646 ScriptAgent[46861:3107] CLTilesManagerClient: reconnecting, 0x10d55b5b0

info: Instruments is ready to receive commands
debug: Instruments launched. Starting poll loop for new commands.
debug: Setting bootstrap config keys/values
debug: Pushing command to appium work queue: “target = $.target();\nau = $;\n$.isVerbose = true;\nautoAcceptAlerts = false;\n”
debug: Sending command to instruments: target = $.target();
au = $;
$.isVerbose = true;
autoAcceptAlerts = false;

debug: Socket data received (2 bytes)
debug: Socket data being routed.
debug: Sending command to instruments: target = $.target();
au = $;
$.isVerbose = true;
autoAcceptAlerts = false;

debug: [INST] 2014-09-19 18:30:46 +0000 Debug: evaluation finished

debug: [INST] 2014-09-19 18:30:46 +0000 Debug: Result is not protocol compliant, wrapping

debug: [INST] 2014-09-19 18:30:46 +0000 Debug: Running system command #1: /usr/local/Cellar/node/0.10.18/bin/node /usr/local/share/npm/lib/node_modules/appium/node_modules/appium-uiauto/bin/command-proxy-client.js /tmp/instruments_sock 2,{“status”:0,“value”:false}…

debug: Socket data received (28 bytes)
debug: Socket data being routed.
debug: Got result from instruments: {“status”:0,“value”:false}
debug: Pushing command to appium work queue: “au.bundleId()”
debug: Sending command to instruments: au.bundleId()
debug: Sending command to instruments: au.bundleId()
debug: [INST] 2014-09-19 18:30:46 +0000 Debug: Got new command 1 from instruments: au.bundleId()

debug: [INST] 2014-09-19 18:30:46 +0000 Debug: evaluating au.bundleId()

debug: [INST] 2014-09-19 18:30:46 +0000 Debug: evaluation finished

debug: [INST] 2014-09-19 18:30:46 +0000 Debug: Result is not protocol compliant, wrapping

debug: [INST] 2014-09-19 18:30:46 +0000 Debug: Running system command #2: /usr/local/Cellar/node/0.10.18/bin/node /usr/local/share/npm/lib/node_modules/appium/node_modules/appium-uiauto/bin/command-proxy-client.js /tmp/instruments_sock 2,{“status”:0,“value”:“com.kabam.SodaDev”}…

debug: Socket data received (42 bytes)
debug: Socket data being routed.
debug: Got result from instruments: {“status”:0,“value”:“com.kabam.SodaDev”}
debug: Bundle ID for open app is com.kabam.SodaDev
debug: Setting initial orientation to PORTRAIT
debug: Pushing command to appium work queue: “au.setScreenOrientation(‘PORTRAIT’)”
debug: Sending command to instruments: au.setScreenOrientation(‘PORTRAIT’)
debug: Sending command to instruments: au.setScreenOrientation(‘PORTRAIT’)
debug: [INST] 2014-09-19 18:30:46 +0000 Debug: Got new command 2 from instruments: au.setScreenOrientation(‘PORTRAIT’)

debug: [INST] 2014-09-19 18:30:46 +0000 Debug: evaluating au.setScreenOrientation(‘PORTRAIT’)

debug: [INST] 2014-09-19 18:30:46 +0000 Debug: target.setDeviceOrientation(“1”)

debug: [INST] 2014-09-19 18:30:46 +0000 Debug: evaluation finished

debug: [INST] 2014-09-19 18:30:46 +0000 Debug: Running system command #3: /usr/local/Cellar/node/0.10.18/bin/node /usr/local/share/npm/lib/node_modules/appium/node_modules/appium-uiauto/bin/command-proxy-client.js /tmp/instruments_sock 2,{“status”:0,“value”:“PORTRAIT”}…

debug: Socket data received (33 bytes)
debug: Socket data being routed.
debug: Got result from instruments: {“status”:0,“value”:“PORTRAIT”}
debug: Device launched! Ready for commands
debug: Setting command timeout to the default of 60 secs
debug: Appium session started with sessionId null
info: <-- POST /wd/hub/session 303 8433.176 ms - 9
info: --> GET /wd/hub/session/null {}
debug: Appium request initiated at /wd/hub/session/null
debug: Request received with params: {}
info: <-- GET /wd/hub/session/null 404 1.045 ms - 40

Yeah, that’s unfortunate. To fix problems like this I ended up writing a custom test runner. It ends all the node processes and starts them up before each test. The code is specific to my setup however you might be able to get some ideas. It’s open source.

I’m experiencing this exact issue. Session-override is not sufficient because the subsequent session is null. Restarting the appium server in testware is an option as bootstraponline suggests, but it’s really not ideal.

Perhaps the sessionId needs to be reset? I noticed that the second time there’s this debug message:

debug: Appium session started with sessionId null

If you file an issue on github with full logs, the devs may be able to fix it.

I confirmed that running client.appClose() does allow the client recover, but this isn’t always possible in situations where tests end abnormally, which is what sessionOverride is supposed to cover.

Filed as:

Thanks. I’ve marked it as a bug.

Please tell me how to enable -session-override.
Thank you in advance.

See the server args doc on github which explains.