Skip to content

Commit e42221d

Browse files
zx2c4dmitshur
authored andcommitted
[release-branch.go1.12] runtime: do not use PowerRegisterSuspendResumeNotification on systems with "program time" timer
Systems where PowerRegisterSuspendResumeNotification returns ERROR_ FILE_NOT_FOUND are also systems where nanotime() is on "program time" rather than "real time". The chain for this is: powrprof.dll!PowerRegisterSuspendResumeNotification -> umpdc.dll!PdcPortOpen -> ntdll.dll!ZwAlpcConnectPort("\\PdcPort") -> syscall -> ntoskrnl.exe!AlpcpConnectPort Opening \\.\PdcPort fails with STATUS_OBJECT_NAME_NOT_FOUND when pdc.sys hasn't been initialized. Pdc.sys also provides the various hooks for sleep resumption events, which means if it's not loaded, then our "real time" timer is actually on "program time". Finally STATUS_OBJECT_NAME_ NOT_FOUND is passed through RtlNtStatusToDosError, which returns ERROR_ FILE_NOT_FOUND. Therefore, in the case where the function returns ERROR_ FILE_NOT_FOUND, we don't mind, since the timer we're using will correspond fine with the lack of sleep resumption notifications. This applies, for example, to Docker users. Updates #35447 Updates #35482 Fixes #36377 Change-Id: I9e1ce5bbc54b9da55ff7a3918b5da28112647eee Reviewed-on: https://go-review.googlesource.com/c/go/+/208317 Reviewed-by: Jason A. Donenfeld <[email protected]> Reviewed-by: Austin Clements <[email protected]> Run-TryBot: Jason A. Donenfeld <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/213198
1 parent 93f0599 commit e42221d

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/runtime/os_windows.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,10 @@ func loadOptionalSyscalls() {
261261
}
262262

263263
func monitorSuspendResume() {
264-
const _DEVICE_NOTIFY_CALLBACK = 2
264+
const (
265+
_DEVICE_NOTIFY_CALLBACK = 2
266+
_ERROR_FILE_NOT_FOUND = 2
267+
)
265268
type _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS struct {
266269
callback uintptr
267270
context uintptr
@@ -287,10 +290,20 @@ func monitorSuspendResume() {
287290
callback: compileCallback(*efaceOf(&fn), true),
288291
}
289292
handle := uintptr(0)
290-
if stdcall3(powerRegisterSuspendResumeNotification, _DEVICE_NOTIFY_CALLBACK,
291-
uintptr(unsafe.Pointer(&params)),
292-
uintptr(unsafe.Pointer(&handle))) != 0 {
293-
throw("PowerRegisterSuspendResumeNotification failure")
293+
ret := stdcall3(powerRegisterSuspendResumeNotification, _DEVICE_NOTIFY_CALLBACK,
294+
uintptr(unsafe.Pointer(&params)), uintptr(unsafe.Pointer(&handle)))
295+
// This function doesn't use GetLastError(), so we use the return value directly.
296+
switch ret {
297+
case 0:
298+
return // Successful, nothing more to do.
299+
case _ERROR_FILE_NOT_FOUND:
300+
// Systems without access to the suspend/resume notifier
301+
// also have their clock on "program time", and therefore
302+
// don't want or need this anyway.
303+
return
304+
default:
305+
println("runtime: PowerRegisterSuspendResumeNotification failed with errno=", ret)
306+
throw("runtime: PowerRegisterSuspendResumeNotification failure")
294307
}
295308
}
296309

0 commit comments

Comments
 (0)