-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
runtime: Windows service lifecycle events behave incorrectly when called within a golang environment #40167
Comments
@zx2c4 Do you have any ideas? Although unrelated, I remember that you had some insight regarding 31685 @alexbrainman I remember that you also had insight into the runtime |
Thank you for creating this issue. Is your issue dup of #40074 ? Alex |
Thanks for the response. It's not really a dupe since my repro doesn't use x/sys/windows/svc but makes direct syscalls. I would assume that the fix to this would solve both tho! Johan |
cc @aclements |
I've found a workaround. It seems that the runtime hooks SetConsoleCtrl in windows which leads to the termination of the program when the CTRL_SHUTDOWN_EVENT is received. The runtime does this here: By overriding the console ctrl handler and the CTRL_SHUTDOWN_EVENT event, you can disable the golang handler for this particular event. Here is a modifed example that does that:
|
Change https://golang.org/cl/242378 mentions this issue: |
I just posted a patch to maybe fix this, but I haven't tested. @yohan1234 - can you test and let me know whether or not this works? |
@zx2c4 - I've tried that because I thought that the docs were incorrect on msdn. Unfortunately, it only affects "Ctrl+C input" and not CTRL_SHUTDOWN_EVENT. Here's a some modified code that tests that:
|
I would have preferred that too, but perhaps that boat has already sailed, since we'd break API if we removed it. However maybe there's a way to only call |
Nevermind, it's always used, as
Is your code hitting that |
I was thinking about adjusting runtime itself not to call SetConsoleCtrlHandler, if the process is running as service. What do you think? Alex |
This broke when we added CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, CTRL_SHUTDOWN_EVENT to Go SetConsoleCtrlHandler handler. But maybe we should not be calling SetConsoleCtrlHandler from the service at all? Alex |
That's what I initially tried doing. But this will break other things still where the signal handling is actually useful (crashes). I have another solution I'm working on that avoids bailing when in a service. I'm writing the code now to detect if we're running as a service. Tricky business. |
Are you looking for golang.org/x/sys/windows/svc.IsAnInteractiveSession ? Alex |
Nope, not what we want, I don't think. The solution I'm writing is uglier, unfortunately. We'll see if it works. Halfway done now. |
Change https://golang.org/cl/242280 mentions this issue: |
@yohan1234 Alright, try that latest CL. I haven't tested it for your case yet, but maybe it's correct. |
Thanks a lot for the help.
I believe that it is hitting the exit(2). I don't understand why the exit(2) exists though. That's not the proper way of using that function. If the handler does not handle the signal it should return 0. Even if you wish the program to terminate, it's preferred to exit gracefully via the entry point. Windows takes care of killing of the process automatically if required. This happens after a grace period of 10 seconds after the function returns. This is dependent upon the event. To test your patch, do I need to rebuild go or can I just pull in a local runtime package with that patch somehow? Sorry I'm not sure what the steps are. Also, if anyone is using gdi/user32 with hwnds, I think that the program still crashes needlessly rather than unwinding via hwnds. You would have to write a similar snippet to ignore events in case hwnds have been created. Just something to consider for the future. I would say that lazily loading it when the signals package is pulled in would be a nice solution. Pulling in the signals package would impose some kind of lifecycle management anyways. |
You can patch your local install in .../go/src/runtime/..., and simply build your app. The runtime and stdlib get linked into the executable. Create a file with the patch in your installed .../go, and run EDIT: you can download the patch file from the CL link above. |
You have to rebuild, but I can build you a .zip or .tar or something if you tell me what GOOS/GOARCH you want. But you're probably better off doing this yourself. Clone the repo and run ./make.bash or make.bat.
I'm not sure I grok what you mean here. I maintain a large gdi/user32 codebase and haven't encountered problems. What weird behavior do you suspect I should be seeing? |
@zx2c4 the CL immediately above is in .../go/src/runtime/. That doesn't require a rebuild of Go. @yohan1234, see my instructions above. |
True. |
Thanks for the info @networkimprov The patch wasn't compatible with 1.14.5 or 1.14.4 so I rebuilt golang with the patch applied from the golang repo using make.bat. Unfortunately it did not work. Either isWindowsService() segfaults, returns the wrong answer or the program crashes somewhere else. I rebuilt the main.go program in my original post which logs the service event if it works.
As stated in the docs: If a console application loads the gdi32.dll or user32.dll library, the HandlerRoutine function that you specify when you call SetConsoleCtrlHandler does not get called for the CTRL_LOGOFF_EVENT and CTRL_SHUTDOWN_EVENT events. The operating system recognizes processes that load gdi32.dll or user32.dll as Windows applications rather than console applications. If you depend on the signals package to process SIGTERM via SetConsoleCtrlHandler, that's just gonna stop working if you load gdi32/user32 If you don't depend on those events, you wouldn't have any problems. Just saying that it's probably not possible to walk this line and implement SIGTERM on windows via the signals package. It's just going to function properly if you write console programs. |
I am not convinced your example program does not have bugs. For example, you should only call SetServiceStatus from your main service thread. You Go program is not doing that. Perhaps there are other issues with your program. Alex |
I'm not opposed to the patch; I didn't say I was. I'm opposed to dropping code into the runtime on the eve of a release. What is the hurry? How does attacking me make this an urgent fix? (And please stop being abusive when you hear disagreement; this is not LKML.) A previous late-cycle Windows runtime patch produced #35447, and others. The above is a policy matter, not a technical one. Re the latter, I offered some insight on the reason for |
And re tests, I meant that the CL currently has none. It would be nice to know that Win-service apps work in subsequent releases. Finding false positives obviously requires trials in a wide variety of Windows configurations, which we can't get for 1.15. |
Thanks a lot guys for helping me out. These kinds of issues are really tough to resolve. @networkimprov @zx2c4 All windows services run in session 0 since Vista. Golang requires Windows 7.
I'm probably misunderstanding you here but it's trivial to start a process in session 0. We do this all the time. In fact, it's encouraged by Microsoft because it's a sandbox with lots of wings clipped. Just run it from a service running in session 0. We run a bunch of powershell stuff from there. It is much harder to start a process from session 0 in another session though. We do this too though, it just requires a bit of extra yanking of the Windows chain. The lifecycle of a Windows program is hard to generalize. All golang programs ship with the console ctrl handler set. This is unexpected when writing services or user32/gdi32 programs. It raises a lot of ambiguity in how those programs react to lifecycle events. A service written in golang crashes upon shutdown, but not if user32 has been touched. This is not something that's obviously understood when interacting with the api. @zx2c4 patches solve this issue which is great. If things were written from scratch though, I would argue to either a) remove the console ctrl handler entirely in the runtime or b) inject a hidden window into all golang programs that use the signals package to map WM_ENDSESSION/WM_CLOSE to whatever they need to do as these events are more predictable and remove ambiguity from having two paths for handling ctrl-c events. |
I think the CL (242280) is safe, but I don't know how important it is to land at this late stage of the release cycle. I gave the CL a +1 and I'll let Alex decide whether to put it in for 1.15. Thanks. |
Right, so the point is: when you do that, and the parent is a legitimate service, isWindowsService is telling the truth. And when you inject a foreign process without a service parent, but title it services.exe, and then start a child from there, what then can you say? Not much more than: you've just gone through great lengths to do something ridiculous that no real setup does, and the status of that process is mostly irrelevant. IOW, the false positive potential seems nil in cases that matter.
Not sure about hidden window injection; that seems a bit heavy, and also different. The console ctrl handler works by connecting as a CSR client, and passing its callback in to that machinery, and it's something the runtime always does even for the default handler (which just calls RtlExitUserProcess). But what might make sense instead could be just not installing it if the signals package isn't touched, or maybe only installing it in the case of the linker setting the console flag on the PE. Or some combination thereof. On the other hand, simply returning zero (the 1.16 CL) seems like the simplest fix. So after the 1.15 fix lands and 1.16 opens, maybe we can start to play with that a bit. |
Right. The only other non Windows processes/services that I know of that run in session 0 are user mode drivers.
That seems reasonable to me. |
Thank you for checking, @ianlancetaylor
I think CL 242280 is important enough so it is available to Go 1.14 and 1.15 users, but is not important enough so it delays imminent Go 1.15 release. So, I propose, we merge CL 242280 on release-branch.go1.14 and release-branch.go1.15 (once Go 1.15 is released) branches. And submit CL 243597 on master once Go 1.15 is released. @gopherbot, please backport to Go 1.14 and Go 1.15. This is serious problem where Windows service does not receives stop / shutdown event. @zx2c4 would you, please, copy CL 242280 onto release-branch.go1.14, so we can have it available as soon as possible. Or I can do it myself, if you like. Thank you. Alex |
Backport issue(s) opened: #40411 (for 1.14), #40412 (for 1.15). Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://golang.org/wiki/MinorReleases. |
Will do. |
Change https://golang.org/cl/244957 mentions this issue: |
Change https://golang.org/cl/244958 mentions this issue: |
Change https://golang.org/cl/244959 mentions this issue: |
Sorry for the churn. The two backports are https://golang.org/cl/244958 and https://golang.org/cl/244959. |
Does this issue affect Go 1.13 in addition to 1.14, or was it introduced in 1.14? We need to know this because the minor release policy requires an issue is fixed in all affected supported releases (see #34536). |
Go 1.13 is good. We introduced this bug in Go 1.14. See #40074 (comment) for details. Alex |
@alexbrainman I see that the fix for this issue intended for Go 1.16 (CL 243597) involves removing the
It says it does that, but it doesn't seem to explain why. I'm not opposed to this approach, just looking to understand this better. This is a long thread and from reading it, my brief understanding so far is that the Go 1.16 fix is better but may need additional work/testing, while the Go 1.15+1.14 backports are larger code changes but deemed safer/smaller difference in behavior. Or is it something else? Thank you. Edit: Comments #40167 (comment) and #40167 (comment) confirm my understanding is accurate. |
That is exactly correct. Already submitted fix on current master has not many lines of code, but is riskier - it removes exit(2) and assumes that Windows will do the right thing on its own. And it affects all Go programs. This fix is more "logically correct", but is new, and needs more time to be tested / verified. Backports to go1.14 and go1.15 have more lines of code, but they only affects Windows service programs (limited set of all Windows programs - these programs are specifically designed to be started when Windows boots, and they have to comply with a specific protocol, because of that). And the change just removes code that was not intended to run in Windows service #40074 (comment) I hope it explains. Alex |
Thanks very much. The backport issues have been approved, so it's a matter of getting the cherry pick CLs +2ed, run trybots, and then a release manager can submit them. We are targeting September 1st or so for the next minor releases. |
The service handler needs to handle CTRL+C-like events -- including those sent by the service manager itself -- using the default Windows implementation if no signal handler from Go is already listening to those events. Ordinarily, the signal handler would call exit(2), but we actually need to allow this to be passed onward to the service handler. So, we detect if we're in a service and skip calling exit(2) in that case, just like we do for shared libraries. Updates #40167. Updates #40074. Fixes #40412. Change-Id: Ia77871737a80e1e94f85b02d26af1fd2f646af96 Reviewed-on: https://go-review.googlesource.com/c/go/+/244959 Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
The service handler needs to handle CTRL+C-like events -- including those sent by the service manager itself -- using the default Windows implementation if no signal handler from Go is already listening to those events. Ordinarily, the signal handler would call exit(2), but we actually need to allow this to be passed onward to the service handler. So, we detect if we're in a service and skip calling exit(2) in that case, just like we do for shared libraries. Updates #40167. Updates #40074. Fixes #40411. Change-Id: Ia77871737a80e1e94f85b02d26af1fd2f646af96 Reviewed-on: https://go-review.googlesource.com/c/go/+/244958 Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Upon further narrowing of a minimal example that reproduces 40157, it seems that the golang runtime is preventing emitting of SERVICE_CONTROL_SHUTDOWN events.
I've written a program in c as well as in golang that logs this event. The order of syscalls are identical.
Source code
main.go
main.c
Building
golang:
go build
C:
cl main.c /link Advapi32.lib
.Running
sc create svctest binpath= _path_
in an administrator cmd.exe, path should be an absolute path pointing to either the c executable or the golang executable.sc start svctest
to start the serviceshutdown /s
. This will shut down the computer. Do not shutdown using the start-menu.Cleanup
Run
sc delete svctest
to remove the service entry in WindowsWhat did you expect to see?
Both executables log SERVICE_CONTROL_STOP and SERVICE_CONTROL_SHUTDOWN to
C:\Windows\Temp\svctest.log
. The above steps should log a SERVICE_CONTROL_SHUTDOWN during shutdown of the computer.What did you see instead?
Only the C program appropriately logs SERVICE_CONTROL_SHUTDOWN. You can test that both programs log SERVICE_CONTROL_STOP by right clicking on the service in the Task Manager and selecting Restart.
The text was updated successfully, but these errors were encountered: