pyobjc
pyobjc copied to clipboard
Stopping event loop causes process termination in Sonoma
It appears that when fully loading a bundle (such as Foundation), a bouncing python app appears in the dock, and NSApp() now returns an Object instead of None. This makes it impossible to stop the event loop without causing the entire python process to be terminated (due to https://github.com/ronaldoussoren/pyobjc/blob/dee913c26b276112d061c3ca953275ec106344b1/pyobjc-framework-Cocoa/Lib/PyObjCTools/AppHelper.py#L170-L171 ).
Here's a minimal PoC:
from Foundation import NSTimer
from PyObjCTools.AppHelper import runConsoleEventLoop, stopEventLoop
from AppKit import NSApp
import Foundation
# Load any boundle
# This causes an app to appear in the dock on macOS >= 14.0
dir(Foundation)
# This works too:
# objc.loadBundle(
# 'Foundation',
# globals(),
# bundle_identifier='com.apple.Foundation',
# )
# Confirm there's an App Running
print(NSApp())
def stopper(self):
# Here this terminates the app - and thus the whole python process -instead of terminating the event loop
stopEventLoop()
NSTimer.scheduledTimerWithTimeInterval_repeats_block_(0.3, False, stopper)
runConsoleEventLoop()
print("This should be printed, but it's not")
Output in Sonoma:
$ python3 -u broken.py
<NSApplication: 0x145fe2590>
2024-02-11 21:11:21.024 Python[33482:418681] ApplePersistence=NO
$ sw_vers
ProductName: macOS
ProductVersion: 14.2.1
BuildVersion: 23C71
Output in Ventura:
$ python3 -u broken.py
None
This should be printed, but it's not
$ sw_vers
ProductName: macOS
ProductVersion: 13.6.3
BuildVersion: 22G436
We found this out while experimenting with the VisionKit on Ventura. Another snippet that presents the same problem is: https://github.com/ronaldoussoren/pyobjc/issues/592#issuecomment-1937794024