Mach ports problem

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

[quote]0 0x00007fff36d7ddb7 THE_SYSTEM_HAS_NO_PORTS_AVAILABLE + 4
1 0x00007fff36c7874c __CFRunLoopCreate + 555
2 0x00007fff36c78499 _CFRunLoopGet0 + 194[/quote]

But there also is a thread on printing

[quote]12 0x00007fff3455be71 -[NSPrintInfo(NSInternal) _printSessionForGetting] + 43
13 0x00007fff345619d8 -[NSPrintInfo(NSInternal) _createDefaultOrUnflattenPrintSettingsIfNecessary] + 188
14 0x00007fff345618f0 -[NSPrintInfo(NSInternal) _printSettingsForSetting] + 22
15 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 "" 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?

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…

Sigh… yes, this seems a bug in macOS.

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

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.

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

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.

@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.