Can not instantiate JobObject

Sep 4, 2012 at 3:27 AM

I want to use JobObjectWrapper in my application to limit process memory. I'm trying to instatiate a JobObject with the following code:

        JobObject job = new JobObject();

but I'm getting the following exception:

Unhandled Exception: JobManagement.JobException: The management process is runni ng inside a Job, it will not be able to assign child processes to a new Job.

I'm also getting such exception when I try to run examples in the solution from codeplex(f.e. EndOfProcessMemory).

What am I doing wrong?

Sep 4, 2012 at 11:47 AM

Are you running your code with the Visual Studio Debugger attached? If so, your executables gets started as a job by the debugger, which your child process needs to be able to breakaway from before you can assign it to your job.

You can circumvent this problem by either starting your code without a debugger attached, or by passing CREATE_BREAKAWAY_FROM_JOB in the flags of  CreateProcess.

Sep 5, 2012 at 4:51 AM

Thanks for the reply! I tried both.

1) "starting your code without a debugger attached".

I run this code

JobObject jo = new JobObject ();

 by pressing "Start without debugging", but still get the exception.

2) "passing CREATE_BREAKAWAY_FROM_JOB in the flags of  CreateProcess".

I'm not quite sure what to do in my situation. I try to explain what I want to do.

using (JobObject jo = new JobObject())
jo.Events.OnEndOfJobTime += new    jobEventHandler<EndOfJobTimeEventArgs>(Events_OnEndOfProcessTime);
jo.Limits.PerJobUserTimeLimit = new TimeSpan(0, 0, 0, 0, 1);
System.Diagnostics.ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo(filePath);
si.CreateNoWindow = true;
si.RedirectStandardInput = true;
si.UseShellExecute = false;
System.Diagnostics.Process p = jo.CreateProcessMayBreakAway(si);

What process should I start with the CREATE_BREAKAWAY_FROM_JOB in the flag.

Thank you in advance for your help.

Jan 9, 2013 at 8:21 PM

I can reproduce this readily on Windows 8 64 bit. 
I see the issue whenever UAC is enabled and I launch my exe which requires Elevated Privilages to run. So having manifest with the line: "<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />" is key.   From what I see ,calling "CreateProcessMayBreakAway"  (setting  CREATE_BREAKAWAY_FROM_JOB, however that is supposed to be done) will not solve the issue.

This is because when the Exception occurs in the  JobObject() constructor ( via CreateJob()):

JobObject() : _name(System::String::Empty) {CreateJob();}

 CreateJob calls as it first line "ProbeForRunningInJob()". So before you get chance to set any "Breakaway" flag, you get the exception from "ProbeForRunningInJob()":

	//Throw exception if the host process is in a Job itself#using <System.dll>
	void JobObject::ProbeForRunningInJob()
		BOOL bInAnyJob;

		::IsProcessInJob(::GetCurrentProcess(), NULL, &bInAnyJob);

		if (bInAnyJob)
			throw gcnew JobException(L"The management process is running inside a Job, it will not be able to assign child processes to a new Job.");

So,  after a user clicks the UAC request prompt to allow execution of the program,  Windows must then create a Job for my process to run (elevated).

By the time my code actually runs, it is already in a Job due to UAC. So then I get the error.

Now of course, I could comment out that call to "ProbeForRunningInJob()", but that doesn't seem like the correct path to pursue.
Can anyone tell us what should we do in this case? Will there be an update to this code?

I really like this code,  otherwise :)


Jan 9, 2013 at 9:16 PM

And to be clear, this is only if launching the exe from a "less privileged" account context. Then being presented with the UAC prompt.
If you open an Administrator command prompt, and call the exe from with this already elevated command prompt, you don't get the exception.