I’m trying to run a service on my Mac Mini M1. But it has unexplained random delays from few seconds to 20 minutes many times during the day. It works perfectly on linux.
Sleep appears to be very broken on MacOSX. The machine doesn’t run anything else, only a few development tools are installed, screen and keyboard are not connected. I made a minimal reproduction:
#include <iostream>
#include <thread>
#include <chrono>
int main()
{
for (;;)
{
std::this_thread::sleep_for(std::chrono::seconds(10));
const auto now = std::chrono::system_clock::now();
std::cout << "Wall clock: " << now << std::endl;
}
}
Example of 15 minute sleep instead of 10 seconds (this happens many times):
Wall clock: 2024-07-02 05:34:08.577015
Wall clock: 2024-07-02 05:34:18.705682
Wall clock: 2024-07-02 05:34:28.853954
Wall clock: 2024-07-02 05:50:42.539091
Wall clock: 2024-07-02 05:50:52.582264
% cat localhost.sleep_test.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>localhost.sleep_test</string>
<key>Program</key>
<string>/usr/local/bin/sleep_test.sh</string>
<key>RunAtLoad</key>
<true/>
<key>StandardOutPath</key>
<string>/Users/user/sleep_test-output.txt</string>
<key>StandardErrorPath</key>
<string>/Users/user/sleep_test-errors.txt</string>
</dict>
</plist>
% launchctl load localhost.sleep_test.plist
% launchctl start localhost.sleep_test
% uname -a
Darwin MacMini 23.5.0 Darwin Kernel Version 23.5.0: Wed May 1 20:16:51 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T8103 arm64
% clang --version
Apple clang version 15.0.0 (clang-1500.3.9.4)
Target: arm64-apple-darwin23.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin