66

I've been facing an issue (frequently) with the recent major release of the iOS application development tool - Xcode 9-beta.

It's showing me the following error frequently while running/debugging app in Simulator (iOS 11).

Could not attach to pid : “2370”
Ensure <project title> is not already running, and <system username> has permission to debug it.

Here is a snapshot for the same issue:

error message - Could not attach to pid

What would be permanent solution of this issue, as it's disturbing frequently?

6
  • Can you file a bug on this and attach the output of sudo sysdiagnose -q and xcrun simctl diagnose?
    – russbishop
    Jun 28, 2017 at 6:50
  • @russbishop Reported a bug. Command 'xcrun simctl diagnose' not working. Error: Unrecognized subcommand: diagnose
    – Krunal
    Jun 28, 2017 at 9:45
  • you must have an older version of Xcode selected with xcode-select. Make sure Xcode 9 is selected.
    – russbishop
    Jul 3, 2017 at 5:01
  • I cleaned the derived data & cleaned the build folder. It worked for me.
    – iOSDev
    Oct 5, 2017 at 2:23
  • @russbishop i am also having in 9.4 when i am running test cases how will solve it please help Sep 26, 2018 at 9:52

22 Answers 22

46

If issue is on OS Mojave and you are trying, like me to run tests on older Xcode version (lower than 10.0), make sure that in your scheme, when you select Test, Debug executable is disabled

enter image description here

You won't be able to debug tests from this point

4
  • 6
    That works, but then you can't debug your tests anymore :( Oct 25, 2018 at 21:49
  • Unbelievable! Thank you! Dec 14, 2018 at 0:21
  • This fix also worked for me when I had an "attach failed" error running UI tests on Xcode 12.1 on Catalina.
    – Angela
    Nov 12, 2020 at 15:58
  • I have since turned Debug executable back on, and the error didn't come back, and I can debug my tests. So perhaps it only needs to be turned off and on again.
    – Angela
    Nov 13, 2020 at 0:02
23

Still not a permanent solution, but I had to quit and restart Xcode as the other solutions did not work for me.

1
  • damn.. was just a restart :S Feb 23, 2019 at 2:32
16

This worked for me:

Edit Scheme -> Info -> Executable -> Ask on launch

Credits to @nastya-gorban's answer here

Update

After spending a considerable time with examples on Apple bug report, they basically disregarded the issue as using manual certificates is not "expected".

Long story short, if you don't have a business account and hence multiple developers on the same account, you should be fine with using the automatic signing and should not see the issue.

If you do have a business account with multiple users (which I found it breaks automatic signing), this is their suggestion:

We suggest that you use automatic signing for your debug builds and manual signing for your distribution builds.

4
  • I have since posted the issue on the Bug reporter, but still interchanging info to figure the cause.
    – Efren
    Jul 24, 2018 at 4:28
  • 1
    According to apple: "You can’t debug something provisioned with an ad-hoc distribution profile. Distribution profiles don’t allow debugging...We assume you’re using manual signing. If you selected a distribution signing certificate, then you can’t choose a development provisioning profile. Development provisioning profiles don’t contain distribution signing certificates. So, if you want to debug, you’ll need to select a development signing certificate and a development provisioning profile. Incidentally, this is what automatic signing would have done for you without all the fuss."
    – Efren
    Jul 27, 2018 at 7:25
  • Latest and final reply: "Engineering has determined that this issue behaves as intended based on the following information: Yep, depending on which OS we're talking about, the rules are stronger for debugging a process. Ask On Launch is also potentially finding a different copy of the application, so, even on newer iOS versions there may be a way to get something running via "Ask on Launch" if it gets the distribution signed copy. We suggest that you use automatic signing for your debug builds and manual signing for your distribution builds."
    – Efren
    Sep 4, 2018 at 8:22
  • 11.3 Worked for me
    – Zeeshan
    Mar 26, 2020 at 17:21
11

Killing my simulator and then running it again from Xcode.

1
  • I tried all these solutions, but it solves error temporary. Can i have its permanent solution?
    – Krunal
    Oct 17, 2017 at 9:37
8

I had this issue too. There seems to be an issue with having two Xcode version installed at the same time. (9.4.1 and 10.0 Beta)

It works with the beta, but not with the stable version. Everything is set to the tools of the Xcode 9.4.1 stable version. I can only run my unit tests with the beta.

After removing the beta, it worked with the stable version.

1
  • 3
    This sounds like my issue. I installed Xcode 10 and updated to Mojave. Then I had to use Xcode 9.4 again and it started complaining like this. I will try uninstalling Xcode 10 and see if that helps.
    – jowie
    Sep 28, 2018 at 15:03
7

delete derived data and clean the project, wait until processing is complete, this may take some time. The idea is to give some processing time. Works fine after that

1
  • I tried that solution, but it solves error temporary. Can i have its permanent solution.
    – Krunal
    Oct 16, 2017 at 16:54
7

I have been dealing with this issue for days. I have been able to build but not launch on Simulator, and I get the same "pid:.." error message.

I am using: - Xcode v9.2 - Swift 3.2 - Building for iOS

The things that I tried that DID NOT WORK were:

restarting the computer; deleting content and settings (of Simulator, I do not have "reset"); uninstalling and reinstalling Xcode; changing "Deployment Target"; changing the device in the simulator's Hardware->Manage Device; deleting Derived Data, Cleaning and Building, or just waiting...forever.

What WORKED was as @Rajasekhar mentioned:

  • checked out the Keychain certificates.
  • deleted the exiting ones by right clicking (they'd passed expiration)
  • and unchecked "automatically manage signing" in Targets->General

After that it successfully launched in Simulator. I don't know if the issue will come back but hopefully this works.

2
  • 1
    Please don't add "thank you" as an answer. Once you have sufficient reputation, you will be able to vote up questions and answers that you found helpful. - From Review Feb 2, 2018 at 4:12
  • 1
    i included an extra step that i took as well as how to delete the certificate which was not mentioned above but was asked by another user
    – tameikal
    Feb 3, 2018 at 4:52
7

Even on Xcode 11.1/2/3

It is an Authorization issue with Simulator, When Simulator does not have the necessary access. It raises the issue.

Try following in Terminal

sudo DevToolsSecurity -enable

or

sudo /usr/sbin/DevToolsSecurity -enable

Details can be found here

5

I hate to add more noise to this, but for me, the answer is to, nonsensically, use sudo.

Run normally, Xcode 9.4.1 (9F2000) and Xcode 10.0 beta 4 (10L213o) both failed to attach to my app after multiple tries, giving the error quoted in the original post.

What worked was to run Xcode (9.4) with sudo,

sudo /Applications/Xcode.app/Contents/MacOS/Xcode

I don't see why sudo is necessary. The Cocoa app to which I am attaching is a Debug build that I just built in Xcode 9.4.1 and dragged into /Applications. It is not codesigned. Posix permissions on the .app, its Contents, its MacOS, and the executable are all octal 755. Owner is me. It works fine if I leave it in the Build folder, build and debug in the normal way.

The problem is apparently with lldb. I also tried using lldb (lldb-902.0.79.7) from the command line. I got the same result. It works only with sudo. Without sudo,

error: attach failed: unable to attach
1
  • This looked like a sound solution, so I held high hopes for it to work for me! Sadly it didn't. 😞 I do have two versions of Xcode installed, however. I will try removing one.
    – jowie
    Sep 28, 2018 at 15:04
5

I had same issue. Check screenshot.

Screenshot added

Below are few solutions that should work:

  1. Rerun project
  2. Clean (Shortcut: cmd + shift + K) and Rerun project
  3. Quit Xcode and Simulator. Open project and run again
  4. Reset content of Simulator (Select Simulator -> Goto Hardware tab -> Erase All Content and Settings…) and rerun project.

Screenshot added

Solution 4 worked for me.

0
4

This seems to be a temporary issue when you are trying to build too fast after a build has started. Try stopping and running the project again.

4
  • 1
    Yes, it is temporary. But facing often, with different PIDs> There isn't permanent solution to it? It works fine, "Stopping and running again". But not gone forever.
    – Krunal
    Oct 6, 2017 at 14:19
  • 1
    No, not yet. Seems like it's a bug. Try running only once and don't click several times on the button. Oct 6, 2017 at 14:21
  • 2
    Yes, I raised a ticket in Bug Reporter for the same. But Apple is unable to track a bug. I shared a complete system report generated using command xcrun simctl diagnose and forwarded to Apple.
    – Krunal
    Oct 7, 2017 at 2:42
  • Exact!, for me that is due to an excessive time waiting for emulator response
    – Josem
    Jun 26, 2018 at 12:44
4

(most likely solution) 1. Simulator-> Hardware-> Erase all contents and Settings

(less likely solution) 2. keychain-> upper right lock-> unlock and lock again (or the other way around)

3
  • Can you elaborate on: "keychain-> upper right lock-> unlock and lock again (or the other way around)". Keychain App? Jun 24, 2019 at 22:37
  • I think the UI of Keychain Access has changed after this answer was posted. Basically you just right click the lock and click unlock/lock so that it refresh the keychain.
    – Will Gwo
    Sep 10, 2019 at 22:39
  • 1
    This has solved my issue. Using Xcode 9.2 on High Sierra. Nov 14, 2019 at 7:00
4

I was seeing this in Xcode 10.2 and the cause for me was that lldb-rpc-server was crashing. I worked around it by ensuring either Address Sanitizer or Thread Sanitizer is enabled in the debug options. I also filed a bug report viewable on Open Radar.

4

I tried all the answers above. The only thing worked to me is changing the build number.

4

In my case (Xcode 10.1), this was the error in the console:

kernel  macOSTaskPolicy: (com.apple.debugserver) may not get the taskport of (bin) (pid: 10132): (bin) is hardened, (bin) doesn't have get-task-allow, (com.apple.debugserver) is a declared debugger

So the solution was to disable the Hardened Runtime, clean the build folder and run again.

3

This is the issue with the untrusted certificates in key-chain access, please remove such a type of certificates and re-build again.

5
  • How can I remove certificate, can you please eleborate in detail?
    – Krunal
    Oct 26, 2017 at 4:02
  • open key-chain access -> check for the certificates, there you can find certificates those not related with your protect. (typically what I'm trying to tell you is? 'some times you open unknown certificates into your key chain access, those are related with your project only but currently do don't have any membership on those teams') Oct 26, 2017 at 5:02
  • One more suggestion is Just kill the Xcode and Simulator, turn-Off your device and restart again,I believe this will fix the issue. Oct 28, 2017 at 10:36
  • We can achieve it by re-starting device & Xcode once. Dec 11, 2017 at 19:08
  • None of the solutions posted here are working at all for me. I've even restarted my computer several times. I can currently only run on a real device. Any updates?
    – n8tr
    Dec 21, 2017 at 23:04
2

This happens on my machine, when I set the 'new build system' Go to menu file=>workspace settings and set Build System to "Standard".

2

I realise this is not a problem with a single solution, from all the other answers. So, here's what worked for me:

1) Reboot the machine

2) The first run always works for me. The only thing that helps me avoid this error after this first run, is to stop the application from XCode, instead of just clicking the Run button to re-run the application.

Another thing, when I lock my computer the issue re-appears sometimes (probably when I forget to stop the application). So I have to reboot my machine again.

1

After doing some digging, this worked for me on Xcode 10.3.

sudo /usr/sbin/DevToolsSecurity -enable

1

In my case the only thing that worked was switching back from the "New Build System" to the "Legacy Build System" in the Workspace Settings. Bummer.

1

It's an issue with authorization. Try this in the Terminal:

sudo DevToolsSecurity -enable

1

First, close app completely via sim (not sleep - close app totally) If not working yet: reboot whole PC, ensure only 1 simulator is active upon reboot.

More than 1 sim can confuse it if it's bugging.

Not the answer you're looking for? Browse other questions tagged or ask your own question.