1

This is a follow-up to my previous question: Function keys stop working after a while in Mojave

The issue I had is that loginwindow kept freezing. In this Gist, the issue is explained in detail. The explanation can be found below.

macOS Mojave build >18A326g has a nasty issue.

Basically, loginwindow likes threads. It's so passionate about threads that
it will continue to collect threads endlessly. Now, the problem with such a
process collecting so many threads is that, as the owner of all processes
running under your account, if the process crashes, say goodbye to most of
macOS.

Well, this is exactly what happens. loginwindow freezes at 4096 threads and
causes most of macOS to become unstable or unusable. For example, most of the
menu items under the apple menu become useless, such as About This Mac or the
Sleep/Restart/Shut Down options. Some of the function/media keys stop working
as well, such as the brightness, keyboard backlight, and volume keys.

I also believe the log out option gets disabled too, but I haven't tested it.
However, if this is indeed the case, this means the only way to log out
reliably is using this script with the --kill option, which finds and kills
loginwindow for you.

loginwindow does indeed collect 4096 threads and then freeze. Every time. In fact, I built a notifier using JXA to warn me when corruption (threads / 4096) reaches over 90%.

That's beside the point, however. As far as I can tell, nobody else has the issue (either that or they never figured out why).

I've reported this issue to Apple 4 betas ago, but the issue's still here.

How do I figure out what's causing this issue? How do I prevent it?

  • I'd try a new admin account & see if it still happens. My current uptime is 6 days [never gets much past that these days with a new beta every week;) & loginwindow is showing just 2 threads; so my guess is it's not System per se, it's something else you have running. – Tetsujin Sep 11 '18 at 6:41
  • Huh... I'll try a new account. – Dev Sep 11 '18 at 7:59
  • @Tetsujin I can confirm it's system-wide, just logged in via root and the issue persists. (threads are already at 9 as soon as I log in!) – Dev Sep 11 '18 at 8:04
  • Sorry, still can't repro on b10. 2 threads only. It has to be something you have installed; maybe test safe boot. – Tetsujin Sep 13 '18 at 7:53
  • This sounds very similar to issues I've been having. It occurs for me after I've let the computer sit for a while, usually overnight. I've been trying lots of debugging, but still haven't figured out a root cause, although I did find that booting the computer into Safe Mode prevented the issue. I'll keep an eye on loginwindow and see if that's what causing my issue as well. – hisaac Sep 13 '18 at 12:55
1

I've noticed the exact same hanging loginwindow issue since upgrading to Mojave 10.14.5. As @Dev and some of the other responses pointed out, there are some common applications which seem to contribute to this. Little Snitch is the culprit in my case and have found the following solutions work best as a workaround. My system is also configured for automatic login.

The immediate solution to fix the hanging loginwindow is to ssh into the host which hangs and run this simple script (or one-liner) to stop loginwindow and then start it again using launchctl.

#!/bin/sh
sudo launchctl stop com.apple.loginwindow
sleep 2
sudo launchctl start com.apple.loginwindow

OR

sudo launchctl stop com.apple.loginwindow && sleep 2 && sudo launchctl start com.apple.loginwindow

The long-term workaround solution until Objective Development and/or Apple addresses this problematic behavior in their code is to instrument a launch daemon which executes a modified version of the above script at startup.

  1. Create script in /usr/local/bin named fixHang.sh

    sudo touch /usr/local/bin/fixHang.sh
    
  2. Edit the script and paste in the content below.

    sudo nano /usr/local/bin/fixHang.sh
    

    #!/bin/sh
    logFile="/Users/Michael/loginwindowdaemonscript.txt"
    echo "------------BOOT BEGIN $(date)----------"
    ustart=$(date -j -f "%a %b %d %T %Z %Y" "date" "+%s")
    timestamp=$(date)
    loginPIDChar=$(ps -ef | grep -v "grep" | grep "/usr/libexec/UserEventAgent (Aqua)" | awk $'{print $2}' | wc | awk $'{print $1}')
    loginPID=$(ps -ef | grep -v "grep" | grep "/usr/libexec/UserEventAgent (Aqua)" | awk $'{print $2}')
    echo "$(date) - START" >> "$logFile"
    echo "$(date) - loginwindow PID: $loginPIDWindow" >> "$logFile"
    echo "$(date) - UserEventAgentAqua PID: $loginPID" >> "$logFile"
    while [ "$loginPIDChar" == "0" ];
    do
            echo "$(date) - BEGIN LOOP" >> "$logFile"
            sleep 2
            echo "BEGIN CHECK" >> "$logFile"
            loginPIDChar=$(ps -ef | grep -v "grep" | grep "/usr/libexec/UserEventAgent (Aqua)" | awk $'{print $2}' | wc | awk $'{print $1}')
            loginWindowPID=$(pgrep loginwindow)
            echo "$loginWindowPID" >> "$logFile"
            echo "loginPIDChar = $loginPIDChar" >> "$logFile"
            if [ "$loginPIDChar" != 0 ];
            then
                    timestamp=$(date)
                    loginWindowPID=$(pgrep loginwindow)
                    echo "$timestamp - KILL PROCESS # $loginWindowPID" >> "$logFile"
                    sudo kill -9 "$loginWindowPID"
                    sleep 5
                    echo "$timestamp - BREAK LOOP" >> "$logFile"
                    break
            else
                    timestamp=$(date)
                    loginWindowPID=$(pgrep loginwindow)
                    echo "$timestamp - PROCESS NUMBER: $loginWindowPID" >> "$logFile"
            fi
            sleep 2
            echo "$(date) - END LOOP" >> "$logFile"
    done
    sleep 5
    newloginWindowPID=$(pgrep loginwindow)
    uend=$(date -j -f "%a %b %d %T %Z %Y" "date" "+%s")
    echo "$(date) - fixDuration: $((uend - ustart)) seconds" >> "$logFile"
    echo "$(date) - oldLoginWindowPID #: $loginWindowPID" >> "$logFile"
    echo "$(date) - newLoginWindowPID #: $newloginWindowPID" >> "$logFile"
    echo "------------BOOT COMPLETE $(date)----------" >> "$logFile"

Save out the file (Ctrl+O + [ENTER]) and exit nano (Ctrl+X + [ENTER])

  1. Make script executable and change ownership to root:wheel

    sudo chmod +x /usr/local/bin/fixHang.sh
    sudo chown root:wheel /usr/local/bin/fixHang.sh
    
  2. Create a launch daemon in /Library/LaunchDaemons which when loaded will execute the fixHang.sh script on startup

    sudo touch /Library/LaunchDaemons/com.startup.fixhang.plist
    sudo nano /Library/LaunchDaemons/com.startup.fixhang.plist
    
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>Label</key>
        <string>com.startup.fixhang</string>
        <key>LaunchOnlyOnce</key>
        <true/>
        <key>LimitLoadToSessionType</key>
        <array>
            <string>Aqua</string>
        </array>
        <key>ProgramArguments</key>
        <array>
            <string>sh</string>
            <string>-c</string>
            <string>/usr/local/bin/fixHang.sh</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
    </dict>
    </plist>
    

Save out the file (Ctrl+O + ENTER) and exit nano (Ctrl+X + ENTER)

  1. Change ownership of /Library/LaunchDaemons/com.startup.fixhang.plist to root:wheel

    sudo chown root:wheel /Library/LaunchDaemons/com.startup.fixhang.plist
    
  2. Load the launch daemon for execution at startup

    sudo launchctl load /Library/LaunchDaemons/com.startup.fixhang.plist
    
0

I know the StackExchange community tends to hate these kinds of answers, but I don't know exactly what fixed my issue.

loginwindow is now ending threads correctly. That is to say, at some point it had less threads than before, and it's hovering around 2-3.

I messed around with kexts and LaunchDaemons. I uninstalled certain things, like Karabiner Elements, and Camtasia's audio driver. I disabled certain startup items, like SIMBL. But I don't know exactly what fixed the issue.

If anyone else with this issue has any of this same software, I'd recommend trying to disable some in /Library/LaunchDaemons. (/Library/Extensions for kexts)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .