# Mach ports problem

**URL:** <https://forum.xojo.com/t/mach-ports-problem/46957>\
**Category:** macOS\
**Created:** [January 10, 2019, 6:47am UTC](https://forum.xojo.com/t/mach-ports-problem/46957 "2019-01-10T06:47:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [January 10, 2019, 6:47am UTC](https://forum.xojo.com/t/mach-ports-problem/46957/1 "2019-01-10T06:47:41Z")

</div>

Got a crash from a user “system has no mach ports available”. Checking the crash log I see some information about ports

[quote]0 com.apple.CoreFoundation 0x00007fff36d7ddb7 **THE\_SYSTEM\_HAS\_NO\_PORTS\_AVAILABLE** + 4  
1 com.apple.CoreFoundation 0x00007fff36c7874c \_\_CFRunLoopCreate + 555  
2 com.apple.CoreFoundation 0x00007fff36c78499 \_CFRunLoopGet0 + 194[/quote]

But there also is a thread on printing

[quote]12 com.apple.AppKit 0x00007fff3455be71 -[NSPrintInfo(NSInternal) \_printSessionForGetting] + 43  
13 com.apple.AppKit 0x00007fff345619d8 -[NSPrintInfo(NSInternal) \_createDefaultOrUnflattenPrintSettingsIfNecessary] + 188  
14 com.apple.AppKit 0x00007fff345618f0 -[NSPrintInfo(NSInternal) \_printSettingsForSetting] + 22  
15 com.apple.AppKit 0x00007fff3492ffcb -[NSConcretePrintOperation runOperation] + 514  
16 MBS\_MacControls\_HTMLViewerPrint\_Plugin\_19729.dylib 0x0000000109e1e45b PrintToPDFFile(REALobjectStruct\*, REALfolderItemStruct\*, double, double, double, double) + 1323  
17 com.mothsoftware.mailarchiverx 0x0000000101fe34af HTMLViewerHTMLViewerPrintExtensionMBS.$PrintToPDFFileMBS%b%oof8f8f8f8 + 111[/quote]

So I think that printing is somehow messing up the mach ports. After some emails exchanged with Christian and further testing it seems that executing AppleScripts with Mail as target is opening mach ports by the dozen. Those are closed very slowly as I can see in the Activity Viewer. It’s not even my app. Simply executing a script in the ScriptViewer

`property Mailbox0 : "iCloud"
property Mailbox1 : "Newsletters"
property currentMessageStr : "1"
with timeout of 10000 seconds
	set currentMessage to currentMessageStr as integer
	set AllHeaders to {}
	tell application id "com.apple.mail"
		try
			repeat with messageNo from currentMessage to currentMessage + 9
				set theMessage to (message messageNo of mailbox (Mailbox1) of account (Mailbox0))
				if not deleted status of theMessage then
					set theRes to (all headers of theMessage)
					if theRes = "" then
						copy ("empty") to end of AllHeaders
					else
						copy theRes to end of AllHeaders
					end if
				else
					copy ("deleted") to end of AllHeaders
				end if
			end repeat
			return AllHeaders
		on error errMsg number errNr
			if errNr = -1719 then
				return AllHeaders
			end if
		end try
	end tell
end timeout`

opens like 20 - 30 mach ports per single execution. My app can do this hundreds or thousands of times.

I’m not an expert in low level macOS stuff. From Goggling I think that mach ports are used for low level communication. But I haven’t the foggiest what those things are.

Questions: can I change the number of available mach ports? Can I force/encourage Mail to close the mach ports faster?

---

<div class="post-metadata">

**Author:** ![Sam\_Rowlands](https://forum.xojo.com/user_avatar/forum.xojo.com/sam_rowlands/32/265_2.png) [@Sam\_Rowlands](https://forum.xojo.com/u/Sam_Rowlands)\
**Post date:** [January 10, 2019, 8:38am UTC](https://forum.xojo.com/t/mach-ports-problem/46957/2 "2019-01-10T08:38:15Z")

</div>

File a bug report with Apple and include as much detail as possible. It sounds to me like a bug in Mail and/or AppleScript.

Is it related to a particular version of the macOS (like say 10.14 or 10.13.6)?

p.s. My guess is unless you have a reference to each and every port, you cannot manually close them, and as you didn’t directly create them…

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [January 10, 2019, 11:21am UTC](https://forum.xojo.com/t/mach-ports-problem/46957/3 "2019-01-10T11:21:26Z")

</div>

Sigh… yes, this seems a bug in macOS.

I can see the bug here with High Sierra and the user has Mojave.

---

<div class="post-metadata">

**Author:** ![Tim\_Jones](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_jones/32/8627_2.png) [@Tim\_Jones](https://forum.xojo.com/u/Tim_Jones)\
**Post date:** [January 10, 2019, 4:24pm UTC](https://forum.xojo.com/t/mach-ports-problem/46957/4 "2019-01-10T16:24:15Z")

</div>

You will discover that this is as designed (like with tcp/udp ports). They are held for a period after they are “closed” to prevent malicious kernel level attacks and to insure that all packages/buffers have been sent. When the port is released, it goes into the TIME\_WAIT state to prevent duplicate packets that were delayed en route to the first connection from being delivered to the second connection.

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [January 10, 2019, 5:10pm UTC](https://forum.xojo.com/t/mach-ports-problem/46957/5 "2019-01-10T17:10:34Z")

</div>

@Time Jones: and how can you then run out of ports?

---

<div class="post-metadata">

**Author:** ![Tim\_Jones](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_jones/32/8627_2.png) [@Tim\_Jones](https://forum.xojo.com/u/Tim_Jones)\
**Post date:** [January 10, 2019, 5:24pm UTC](https://forum.xojo.com/t/mach-ports-problem/46957/6 "2019-01-10T17:24:14Z")

</div>

Until the TIME\_WAIT state is cleared, the released port is not removed from the ports list. Because Mach Ports set up kernel level IPC, I don’t know why you would need so many of them when mail operations should be occurring in user space, not kernel space.

What if you were to stop using AppleScript and instead include a mail handler directly within your app? The Xojo mail example is pretty thorough as a starting point.

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [January 10, 2019, 5:35pm UTC](https://forum.xojo.com/t/mach-ports-problem/46957/7 "2019-01-10T17:35:46Z")

</div>

@Tim Jones: my app archives emails either with AppleScript from Mail/Outlook or with IMAP from any IMAP account. The app handles GBs of data. The AppleScripts are created dynamically and can be executed 10k times or more.

The Xojo email example is for sending mails. Very very simple mails.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 7:31pm UTC](https://forum.xojo.com/t/mach-ports-problem/46957/8 "2020-10-29T19:31:19Z")

</div>


