Skip to content

Fixed job object API for handling 0 cpu limit#549

Open
shrjadhav-msft wants to merge 3 commits intomasterfrom
shrjadhav/fix-job-object-zero-cpu-limit
Open

Fixed job object API for handling 0 cpu limit#549
shrjadhav-msft wants to merge 3 commits intomasterfrom
shrjadhav/fix-job-object-zero-cpu-limit

Conversation

@shrjadhav-msft
Copy link
Contributor

This PR fixes the regression in my last commit (reconfiguration of job object PR: #542)

While working with MrBot to reconfigure CPU/memory limits for already created job object, we come across a case where what if user set it to 0 during reconfiguration, the AI confidently guided me toward an implementation that called SetInformationJobObject with JobObjectCpuRateControlInformation using ControlFlags = 0 and CpuRate = 0 to "disable" the CPU rate control. It generated test PS1 scripts to validate that this works, and I trusted those scripts..

So, the previous commit allowed both percent_cpu and percent_physical_memory to be 0 (i.e., "disable both limits"), and also allowed reconfiguring from a non-zero value to 0. Neither of these actually works against the real Windows API. When set to 0, windows API return Invalid Args error.

This PR reverts back that change to disable the limits when value is 0, and again bring back the behavior where we accept limits when they are non-zero.

@shrjadhav-msft
Copy link
Contributor Author

/AzurePipelines run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@shrjadhav-msft shrjadhav-msft requested a review from darobs March 11, 2026 10:29
@shrjadhav-msft
Copy link
Contributor Author

/AzurePipelines run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

/*Codes_SRS_JOB_OBJECT_HELPER_88_041: [ During reconfiguration, if percent_cpu is 0 and the job_object_singleton_state.percent_cpu is non-zero, job_object_helper_set_job_limits_to_current_process shall fail and return NULL. ]*/
(percent_cpu == 0 && job_object_singleton_state.percent_cpu != 0) ||
/*Codes_SRS_JOB_OBJECT_HELPER_88_046: [ During reconfiguration, if percent_physical_memory is 0 and the job_object_singleton_state.percent_memory is non-zero, job_object_helper_set_job_limits_to_current_process shall fail and return NULL. ]*/
(percent_physical_memory == 0 && job_object_singleton_state.percent_memory != 0))
Copy link
Contributor

@jebrando jebrando Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe that percent_cpu & percent_physical_memory can not be zero due to the above check on line 323. check not needed here. #ByDesign

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually looking closer at this code, I see the that both can't be zero, but one can be zero. So this check is valid.

@jebrando
Copy link
Contributor

#include <stdlib.h>

Need an include for stdbool.h


Refers to: win32/src/job_object_helper.c:5 in a11fc45. [](commit_id = a11fc45, deletion_comment = False)

{
/*Codes_SRS_JOB_OBJECT_HELPER_88_032: [ If there are any failures, internal_job_object_helper_create shall fail and return a non-zero value. ]*/
failed = true;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably need to log a failure here to let someone know that the failure in internal_job_object_helper_set_cpu_limit came from this function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and the line 263

Copy link
Contributor

@jebrando jebrando left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants