xcodebuild doesn't deallocate pseudo terminals

Number:rdar://29393034 Date Originated:26.11.2016
Status:Duplicate/33190443 Resolved:No
Product:Developer Tools Product Version:Version 8.1 (8B62)
Classification: Reproducible:Always
While executing UIAutomation tests from command line using xcodebuild. The tool allocations enormous amount of pseudo terminals, around one terminal per test case. If the test target has more then 100 tests (in my case 186) the application can reach the maximum number of allowed pseudo terminals (by default 127) and it cannot execute next tests with following error message:

> Assertion Failure: <unknown>:0: UI Testing Failure - Failure attempting to launch <XCUIApplicationImpl: 0x610000030cc0 com.user.test at /Users/user/Library/Developer/Xcode/DerivedData/test-bdhmpkpvtcuzrscnsfkcsgohstze/Build/Products/Debug-iphonesimulator/test.app>: Error Domain=IDEPseudoTerminalDomain Code=1 "(null)"

This issue doesn't occurs if the test were started from Xcode. In case of Xcode the number of allocated pseudo terminals is no higher than 9. It can be checked by executing command:

> sudo lsof /dev/ptmx

As a workaround the maximum number of allowed pseudo terminals can be changed to 999 (this is the maximum number allowed by the system) by using command:

> sudo sysctl -w kern.tty.ptmx_max=999

But if the test target will have more than 1000 tests the issue will come back.

Steps to Reproduce:
1. Using terminal go to folder where the attached project was extracted
2. Execute UI tests using:
xcodebuild -project test.xcodeproj -scheme test -destination "platform=iOS Simulator,name=iPhone 7" build test
3. Tests should fail before `testExample128`

Expected Results:
All test should be executed and finished

Actual Results:
When the maximum number of pseudo terminals is reached the tests are no longer executed and starting failing with "Assertion Failure" error

masOS Sierra 10.12.1 (16B2555)
Xcode Version 8.1 (8B62)

I've attached logs from xcodebuild command and compressed project with tests.


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!