This is apparently semi-documented behavior, if comments from Dianne Hackborn on a G+ post constitute "documented": https://plus.google.com/+AndroidDevelopers/posts/94jCkmG4jff but foreground services associated with a notification icon should be allowed to keep a wake lock during doze mode.
It seems this is not valid when you have a top-activity in addition to the foreground service.
I have create a minimal implementation which demonstrates this: https://github.com/petrnalevka/dozetest/blob/master/src/com/urbandroid/doze/DozeService.java
The problem can be reproduced on
Android M, MRA58K Nexus 5 and MRA58N Nexus 6
My foreground service has an associated notification and I hold a partial wake lock. Unfortunately Doze mode takes over and my wake lock is broken. I was only able to prevent this by an opt-out from battery optimization or leaving the top activity.
I believe this is a bug in Android as there is no reason why foreground services should keep the wake lock but not if they have an activity on top.
I'm putting this issue on SO even I have already reported this on Androdi issue tracker here https://code.google.com/p/android/issues/detail?id=193802 in order to find workarounds as this is a crutial feature in order to not need the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS which apparently only mean REQUEST_REMOVAL_FROM_PLAYSTORE_BY_GOOGLE at the moment.
Here is how I was able to reproduce the issue using adb thanks to hints from +Dianne Hackborn:
It seems that when I keep an Activity in the foreground while having also the foreground service running my app is recognized by the Power manager as:
PARTIAL_WAKE_LOCK 'Doze lock' DISABLED (uid=10112, pid=24649, ws=null)
Proc # 0: fore F/A/TS trm: 0 24649:com.urbandroid.doze/u0a112 (top-activity)
If I press home and leave the activity I get into the state:
PARTIAL_WAKE_LOCK 'Doze lock' (uid=10112, pid=24649, ws=null)
Proc # 4: prcp F/S/SF trm: 0 24649:com.urbandroid.doze/u0a112 (fg-service)