{"record":{"id":"79c9d9f033d10a5a","repo":"go-delve/delve","slug":"could-not-get-thread-count","errorCode":null,"errorMessage":"could not get thread count","messagePattern":"could not get thread count","errorType":"error_code","errorClass":"couldNotGetThreadCount","httpStatus":null,"severity":"error","filePath":"pkg/proc/native/proc_darwin.go","lineNumber":223,"sourceCode":"\tdbp.postExit()\n\treturn\n}\n\nfunc (dbp *nativeProcess) requestManualStop() (err error) {\n\tvar (\n\t\ttask          = C.mach_port_t(dbp.os.task)\n\t\tthread        = C.mach_port_t(dbp.memthread.os.threadAct)\n\t\texceptionPort = C.mach_port_t(dbp.os.exceptionPort)\n\t)\n\tdbp.os.halt = true\n\tkret := C.raise_exception(task, thread, exceptionPort, C.EXC_BREAKPOINT)\n\tif kret != C.KERN_SUCCESS {\n\t\treturn fmt.Errorf(\"could not raise mach exception\")\n\t}\n\treturn nil\n}\n\nvar couldNotGetThreadCount = errors.New(\"could not get thread count\")\nvar couldNotGetThreadList = errors.New(\"could not get thread list\")\n\nfunc (dbp *nativeProcess) updateThreadList() error {\n\treturn dbp.updateThreadListForTask(dbp.os.task)\n}\n\nfunc (dbp *nativeProcess) updateThreadListForTask(task C.task_t) error {\n\tvar (\n\t\terr   error\n\t\tkret  C.kern_return_t\n\t\tcount C.int\n\t\tlist  []uint32\n\t)\n\n\tfor {\n\t\tcount = C.thread_count(task)\n\t\tif count == -1 {\n\t\t\treturn couldNotGetThreadCount","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/native/proc_darwin.go#L205-L241","documentation":"This sentinel error is returned when the Mach `thread_count` call fails (returns -1) while Delve is enumerating the threads of a debugged task on macOS. It means Delve could not determine how many threads exist in the target process, so thread state cannot be synced. It is produced in updateThreadListForTask and surfaces through Launch and updateThreadList.","triggerScenarios":"Calling Launch on macOS (or any operation that triggers dbp.updateThreadList) when the underlying Mach task port is invalid or the task has exited, so C.thread_count(task) returns -1.","commonSituations":"Debugging a target that died or exited between launch and the thread-list refresh; attaching/launching under a hardened-runtime/signature configuration where Mach task ports are restricted; stale task port after process termination.","solutions":["Re-run the launch/attach; the failure is often transient due to a race with process exit.","Verify the target process is still alive before launching/attaching (e.g. `ps -p <pid>`).","On newer macOS, ensure delve is codesigned with the get-task-allow entitlement or run on a machine with debugging permitted (SIP/AMFI settings).","Retry after disabling hardened runtime restrictions: `codesign --remove-signature <binary>` for dev builds.","Update delve; Mach task handling is periodically fixed in newer versions."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// before launching/attaching, ensure the pid is alive\nif _, err := os.FindProcess(pid); err != nil {\n\treturn fmt.Errorf(\"target not running: %w\", err)\n}","typeGuard":"// sentinel check\nfunc isCouldNotGetThreadCount(err error) bool {\n\treturn errors.Is(err, errors.New(\"could not get thread count\")) || err != nil && err.Error() == \"could not get thread count\"\n}","tryCatchPattern":"if err := dlv.Launch(...); err != nil {\n\tif err.Error() == \"could not get thread count\" {\n\t\t// process likely exited; retry with a fresh launch\n\t\ttime.Sleep(100 * time.Millisecond)\n\t\treturn dlv.Launch(...)\n\t}\n\treturn err\n}","preventionTips":["Verify the target process stays alive during setup (no premature exits).","Ensure macOS debugging entitlements/signing are configured for delve.","Avoid attaching to processes in mid-exit; check process state first.","Keep delve updated; mach port handling improves over releases."],"tags":["macos","mach","debugging","process"],"backgroundTag":"mach-thread-enumeration-failed","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}