You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect value returned by Environment.ProcessorCount when running on GCP Cloud Run resulting in memory problems. #99046
We noticed a strange behavior of our containers deployed to Google Cloud Platform's Cloud Run since upgrading to .NET 8. We were alerted that the containers are being terminated because they used too much memory. We usually allocate 1 CPU and 512MB/1GB Memory to all our containers and it was always enough. After ruling out memory leaks in our code we started logging GC stats and noticed that Environment.ProcessorCount returned 2 even though the container only had 1 CPU available.
That in turn seems to have caused the GC to use Server Mode, whereas according to the documentation, a single-CPU machine should always use Workstation GC.
We managed to crrect this behavior by setting the DOTNET_PROCESSOR_COUNT environment variable.
I'm not sure if anything changed to the way the processor count is being reported in .NET 8, but to my knowledg this behavior was different in prior versions.
I'm not the OP but I was curious and looked into the issue.
The first thing I check was if the Cloud Run execution environment makes a difference. When running in second generation execution environment, Environment.ProcessorCount returns 1 when the number of CPUs is set to 1 in Cloud Run. So that is a workaround.
The reason that Environment.ProcessorCount is wrong in the first generation execution environment is it fails to detect the version of cgroups. Specifically in this switch statement:
The value of f_type when running on generation 1 is SYSFS_MAGIC ( 0x62656572 ). If we assume that means "gcroups version 1", the CPU limit is correctly calculated. Here is my fix from my test program:
Thanks for reporting this issue. Are you noticing this only when deploying to GCP, or do you notice the same behavior on local container environments? @mangod9
Locally (M2 Pro Mac with Colima/QEMU set up to have 1 CPU available) the correct value of 1 is returned.
That's going to be a bit tricky as I don't know any straightforward way to ssh into a running container on Cloud Run. I'll see if I can figure something out.
Thanks @AustinWise for having a deeper look! I didn't think to check the 2nd gen environment as we rarely use those.
@mnebes might be worthwhile following up with GCP about how their infra is configured and whether this is expected. As a workaround you can always force your app to use WKS GC if that works best in your scenario.
@mnebes might be worthwhile following up with GCP about how their infra is configured and whether this is expected. As a workaround you can always force your app to use WKS GC if that works best in your scenario.
I filed a bug in the internal Google issue tracker for this issue and it is in the process of being triaged.
That said, I also think .NET could be improved to detect cgroup CPU limits in more cases. I have open #99508 to fix the problem. I tested this patch with .NET 8 branch and confirmed that it fixed the CPU count when run in a Google Cloud Run generation 1 environment.
True, the workarounds are straightforward, so for us it's fine. I'm more wondering if that isn't a surprise situation that other teams that are using .NET on Cloud Run will be hit by as well.
However, finding that the workaround is needed is very hard (i.e you have OOM errors, so finding that you need to change the CPU count is not the most easy to find and to understand...)
Activity
mangod9 commentedon Feb 28, 2024
Thanks for reporting this issue. Are you noticing this only when deploying to GCP, or do you notice the same behavior on local container environments?
EgorBo commentedon Feb 28, 2024
Can you print what
lscpucommand outputs?AustinWise commentedon Feb 29, 2024
I'm not the OP but I was curious and looked into the issue.
The first thing I check was if the Cloud Run execution environment makes a difference. When running in second generation execution environment,
Environment.ProcessorCountreturns 1 when the number of CPUs is set to 1 in Cloud Run. So that is a workaround.The reason that
Environment.ProcessorCountis wrong in the first generation execution environment is it fails to detect the version of cgroups. Specifically in this switch statement:https://github.com/dotnet/runtime/blob/6ebba8631a2617779162042786c015559dbbde0a/src/coreclr/pal/src/misc/cgroup.cpp#L97C2-L103C10
The value of
f_typewhen running on generation 1 isSYSFS_MAGIC( 0x62656572 ). If we assume that means "gcroups version 1", the CPU limit is correctly calculated. Here is my fix from my test program:AustinWise/TestCpuCount@2b36610
martincostello commentedon Feb 29, 2024
Looks like that and a few other magic values that aren't in the CLR code are in this doc: https://www.kernel.org/doc/Documentation/ABI/testing/ima_policy
Whether they all should be, I don't know.
mnebes commentedon Feb 29, 2024
Thanks everyone for having a look!
Locally (M2 Pro Mac with Colima/QEMU set up to have 1 CPU available) the correct value of
1is returned.That's going to be a bit tricky as I don't know any straightforward way to ssh into a running container on Cloud Run. I'll see if I can figure something out.
Thanks @AustinWise for having a deeper look! I didn't think to check the 2nd gen environment as we rarely use those.
mnebes commentedon Feb 29, 2024
@EgorBo I modified my repro code to spawn a shell process and run the
lscpucommand. Here's the output:it does look like the container thinks it has 2 CPUs, but I'm really not an expert in how to interpret this output 🙂
tannergooding commentedon Feb 29, 2024
Might this be hyperthreading? You are allocated one physical core, which maps to 2 logical cores.
EgorBo commentedon Feb 29, 2024
in case of HP,
lscpuusually showsThread(s) per core: 2mangod9 commentedon Feb 29, 2024
@mnebes might be worthwhile following up with GCP about how their infra is configured and whether this is expected. As a workaround you can always force your app to use WKS GC if that works best in your scenario.
danmoseley commentedon Feb 29, 2024
Is this normal output in any circumstance?
Thread(s) per core: 0mnebes commentedon Mar 1, 2024
@mangod9 see googleapis/google-cloud-dotnet#11404 (comment)
Even if Cloud Run reports these things in an unusual way it still wouldn't explain why we didn't have this issue before .NET 8, though.
As far as workarounds go, we're using the
DOTNET_PROCESSOR_COUNTone right now as it was the easiest to deploy via terraform and things look fine atm.AustinWise commentedon Mar 11, 2024
I filed a bug in the internal Google issue tracker for this issue and it is in the process of being triaged.
That said, I also think .NET could be improved to detect cgroup CPU limits in more cases. I have open #99508 to fix the problem. I tested this patch with .NET 8 branch and confirmed that it fixed the CPU count when run in a Google Cloud Run generation 1 environment.
mangod9 commentedon Mar 11, 2024
Thanks @AustinWise for your fix, it is now merged.
mnebes commentedon Mar 12, 2024
Thanks @AustinWise and everyone involved!
@mangod9 will this fix be coming to some future release of .NET 8 or only in 9?
AustinWise commentedon Mar 12, 2024
It will be in .NET 9. I have opened a PR to propose backporting the fix to .NET 8: #99563
mangod9 commentedon Mar 13, 2024
we would need a good case to back port. Assume the workaround is easy enough?
mnebes commentedon Mar 13, 2024
True, the workarounds are straightforward, so for us it's fine. I'm more wondering if that isn't a surprise situation that other teams that are using .NET on Cloud Run will be hit by as well.
glb-cblin commentedon Mar 13, 2024
I confirm 👍
Yes, the workaround is easy to put in place
However, finding that the workaround is needed is very hard (i.e you have OOM errors, so finding that you need to change the CPU count is not the most easy to find and to understand...)