3 ms·
Bazel-watcher tries to accomplish this on Linux by using process group IDs. It works, if imperfectly sometimes. I ported this to Windows[1] using Job Objects. A
by jchw 7y ago
Bazel-watcher tries to accomplish this on Linux by using process group IDs. It works, if imperfectly sometimes. I ported this to Windows[1] using Job Objects. As usual, the Windows API was hell, and I made use of undocumented syscalls in order to make it work (though that part is partly Go’s fault: when you start a process, it immediately drops the thread handle on the floor. If you start a process suspended, that means it’s impossible to resume using documented APIs.)
Thankfully Raymond Chen wrote an article[2] about Job Objects which helped me figure out the last bits. I genuinely am not sure I could have gotten it right without that article. There’s so many subtle ways to fail!
[1] https://github.com/bazelbuild/bazel-watcher/pull/144/files https://github.com/bazelbuild/bazel-watcher/pull/144/files
[2] https://devblogs.microsoft.com/oldnewthing/20130405-00/?p=4743 https://devblogs.microsoft.com/oldnewthing/20130405-00/?p=47...
- neerajsi 7y agoI'm confused by this post, but I'm an ex-dev on the NT kernel. ResumeThread is a well documented API. As is TerminateJobObject. The one thing I find a bit baroque is the recently added way to make sure a process is in a job on creation, which I believe is part is the ProcThreadAttributes mechanism.
- jchw 7y agoYes, ResumeThread is. NtResumeProcess is what I had to use, because Go immediately closes the thread handle. I’m sure it’s safe to rely on NtResumeProcess. I have used it since XP without issue. But I definitely wish there was a better way to go back from a process to a thread. The best I could find is using Toolhelp32 to iterate all the threads on the system, which I believe is just wrapping NtQuerySystemInformation. Would’ve worked but definitely wasn’t fast.