I find that the JobLab sample always exits early with the message "Process already in a job". I don't understand why this is but when the "IsProcessInJob" call is removed the sample runs just fine.
Opened this issue · 6 comments
I find that the JobLab sample always exits early with the message "Process already in a job". I don't understand why this is but when the "IsProcessInJob" call is removed the sample runs just fine
Hi MRahilly.
I noticed you closed this issue; did you want me to investigate this?
OK, no worries. I'll try to look at this today or tomorrow evening after work.
I'm just going to reopen this issue to keep track of it.
I get the same behaviour with the process starting in a job object, and this happens in both 32 and 64 bit builds, and when debugging or running the application manually. I can query some amount of the job object the process resides in using QueryInformationJobObject(): https://docs.microsoft.com/en-us/windows/win32/api/jobapi2/nf-jobapi2-queryinformationjobobject. You can also view the job ID of a process in Task Manager > Details and selecting Job object ID.
However, disabling the check at the beginning allows the sample to continue, and it (mostly) appears to be working fine. This is OK if you're running on Windows 8 or later, as the behaviour was changed to allow you to change the process' job even if it's already assigned to a job. See the documentation for AssignProcessToJobObject(): https://docs.microsoft.com/en-us/windows/win32/api/jobapi2/nf-jobapi2-assignprocesstojobobject for more information. Otherwise, the sample appears to work correctly: I can spawn processes and assign PIDs to jobs and terminate them. However, the sample does minimal error checking, so I can't tell if any of the other native calls are failing without digging deeper.
My guess is there was a change in how Windows handles jobs between when the book was written and now to make all processes part of some job. AssignProcessToJobObject() was definitely changed, but I'm guessing by default Windows more aggressively assigns jobs to processes.
I'll update the sample to add a note about the change in behaviour.