Skip to content

Commit 056a3d1

Browse files
committed
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. Fixes #35447 Fixes #35482 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]>
1 parent 499dc1c commit 056a3d1

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/runtime/os_windows.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,10 @@ func loadOptionalSyscalls() {
270270
}
271271

272272
func monitorSuspendResume() {
273-
const _DEVICE_NOTIFY_CALLBACK = 2
273+
const (
274+
_DEVICE_NOTIFY_CALLBACK = 2
275+
_ERROR_FILE_NOT_FOUND = 2
276+
)
274277
type _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS struct {
275278
callback uintptr
276279
context uintptr
@@ -296,10 +299,20 @@ func monitorSuspendResume() {
296299
callback: compileCallback(*efaceOf(&fn), true),
297300
}
298301
handle := uintptr(0)
299-
if stdcall3(powerRegisterSuspendResumeNotification, _DEVICE_NOTIFY_CALLBACK,
300-
uintptr(unsafe.Pointer(&params)),
301-
uintptr(unsafe.Pointer(&handle))) != 0 {
302-
throw("PowerRegisterSuspendResumeNotification failure")
302+
ret := stdcall3(powerRegisterSuspendResumeNotification, _DEVICE_NOTIFY_CALLBACK,
303+
uintptr(unsafe.Pointer(&params)), uintptr(unsafe.Pointer(&handle)))
304+
// This function doesn't use GetLastError(), so we use the return value directly.
305+
switch ret {
306+
case 0:
307+
return // Successful, nothing more to do.
308+
case _ERROR_FILE_NOT_FOUND:
309+
// Systems without access to the suspend/resume notifier
310+
// also have their clock on "program time", and therefore
311+
// don't want or need this anyway.
312+
return
313+
default:
314+
println("runtime: PowerRegisterSuspendResumeNotification failed with errno=", ret)
315+
throw("runtime: PowerRegisterSuspendResumeNotification failure")
303316
}
304317
}
305318

0 commit comments

Comments
 (0)