I have submitted the question to bugreports and Apple has confirmed the following:
"Apps need to use power assertions while engaged on activities that should not be interrupted and we are working to evangelize this.
For you an interim solution would be to use "pmset noidle" in Terminal."
this is not what I want because pmset noidle will just prevent the mac from sleeping but will not resolve the issue. And the evangelization stuff may be quite long before any single programmer decides it's useful to update their software.
So I have studied a workaround which is working quite well, at least for me - you may want to try this out and let me know.
First of all, I have created a bash script that checks CPU usage and lowers a counter each time the mac is in idle state (i.e. Idle CPU reaches at least 93%). When the counter reaches 0 (which means, after 10 minutes of inactivity), the mac goes to sleep with the pmset sleepnow command.
#!/bin/sh
# forcesleep.sh
#
#
# Created by Luca on 16/10/12.
#
minuti=$(cat /Users/luca/.minutes)
valore=$(top -F -R -o cpu -l 3 | grep "CPU usage" | tail -n 1 | awk -F"," {'print $3'} | cut -d"." -f 1)
if [ $valore -gt 93 ]
then
minuti=$((minuti-2))
echo $minuti >/Users/luca/.minutes
else
minuti=10
echo $minuti >/Users/luca/.minutes
fi
if [ $minuti -eq 0 ]
then
echo 10 >>/Users/luca/.minutes
pmset sleepnow
fi
I have then created a LaunchAgent .plist which is called every two minutes:
<?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.luca.forcesleep</string>
<key>ProgramArguments</key>
<array>
<string>/Users/luca/scripts/idlesleep.sh</string>
</array>
<key>Nice</key>
<integer>1</integer>
<key>StartInterval</key>
<integer>120</integer>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/tmp/sleeperror.txt</string>
<key>StandardOutPath</key>
<string>/tmp/sleepout.txt</string>
</dict>
</plist>
The plist runs every 120 seconds (2 minutes) and executes the bash script I explained before which is located in my home folder, /Users/luca/scripts/idlesleep.sh.
Change "luca" with your home folder name!
You have to save the plist under $HOME/Library/LaunchAgents
My .plist is named com.luca.forcesleep.plist - the name is in the <string>..</string>
Once everything is setup, just run from terminal the following command:
launchctl load $HOME/Library/LaunchAgents/com.luca.forcesleep.plist (or the name you have given to the plist file) and that's it.
You may want to play around with value 93 which is the threshold between idle state and active state and may be different from mac to mac.