Skip to content

Incorrect value returned by Environment.ProcessorCount when running on GCP Cloud Run resulting in memory problems. #99046

Closed

Description

@mnebes

Description

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.

See the related issue in the google-cloud-dotnet repo: googleapis/google-cloud-dotnet#11404

Reproduction Steps

  1. Follow the quickstart steps described in the official Cloud Run docs: https://cloud.google.com/run/docs/quickstarts/build-and-deploy/deploy-dotnet-service
  2. Add the following line in Program.cs:
app.Logger.Log(LogLevel.Information, "Processor count: {ProcessorCount}", Environment.ProcessorCount);
  1. Deploy to Cloud Run.
  2. Observe the logs.

Expected behavior

Environment.ProcessorCount should return 1 on a single CPU container and in turn set the Garbage Collector to Workstation mode.

Actual behavior

Environment.ProcessorCount returns 2 on a single-CPU container leading to misconfigured GC.

Regression?

We started experiencing this issue upon migration from .NET 7 to .NET 8.

Known Workarounds

Set DOTNET_PROCESSOR_COUNT to 1 or force Workstation GC explicitly in .csproj/.fsproj.

Configuration

Using .NET8 running in the official Asp NET Core 8.0 docker image. Architecture is x64. Image is run by Google Cloud Platform Cloud Run.

Other information

No response

Activity

  1. ghost added
    untriaged
    on Feb 28, 2024
  2. mangod9 commented on Feb 28, 2024

    @mangod9
    Member

    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?

  3. removed
    untriaged
    on Feb 28, 2024
  4. added this to the 9.0.0 milestone on Feb 28, 2024
  5. EgorBo commented on Feb 28, 2024

    @EgorBo
    Member

    Can you print what lscpu command outputs?

  6. AustinWise commented on Feb 29, 2024

    @AustinWise
    Contributor

    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:

    https://github.com/dotnet/runtime/blob/6ebba8631a2617779162042786c015559dbbde0a/src/coreclr/pal/src/misc/cgroup.cpp#L97C2-L103C10

    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:

    AustinWise/TestCpuCount@2b36610

  7. martincostello commented on Feb 29, 2024

    @martincostello
    Member

    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.

  8. mnebes commented on Feb 29, 2024

    @mnebes
    Author

    Thanks everyone for having a look!

    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.

    Can you print what lscpu command outputs? @EgorBo

    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.

  9. mnebes commented on Feb 29, 2024

    @mnebes
    Author

    @EgorBo I modified my repro code to spawn a shell process and run the lscpu command. Here's the output:

    Architecture:        x86_64
    CPU op-mode(s):      32-bit, 64-bit
    Address sizes:       46 bits physical, 48 bits virtual
    Byte Order:          Little Endian
    CPU(s):              2
    On-line CPU(s) list: 0,1
    Vendor ID:           AuthenticAMD
    Model name:          unknown
    CPU family:          175
    Model:               1
    Thread(s) per core:  0
    Core(s) per socket:  0
    Socket(s):           0
    Stepping:            unknown
    BogoMIPS:            3249.34
    Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a rdseed adx smap clwb sha_ni xsaveopt xsavec xgetbv1 xsaves umip pku vaes vpclmulqdq rdpid
    Virtualization:      AMD-V
    

    it does look like the container thinks it has 2 CPUs, but I'm really not an expert in how to interpret this output 🙂

  10. tannergooding commented on Feb 29, 2024

    @tannergooding
    Member

    Might this be hyperthreading? You are allocated one physical core, which maps to 2 logical cores.

  11. EgorBo commented on Feb 29, 2024

    @EgorBo
    Member

    Might this be hyperthreading? You are allocated one physical core, which maps to 2 logical cores.

    in case of HP, lscpu usually shows Thread(s) per core: 2

  12. mangod9 commented on Feb 29, 2024

    @mangod9
    Member

    @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.

  13. danmoseley commented on Feb 29, 2024

    @danmoseley
    Contributor

    Is this normal output in any circumstance?
    Thread(s) per core: 0

  14. mnebes commented on Mar 1, 2024

    @mnebes
    Author

    @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.

    @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_COUNT one right now as it was the easiest to deploy via terraform and things look fine atm.

  15. AustinWise commented on Mar 11, 2024

    @AustinWise
    Contributor

    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.

  16. mangod9 commented on Mar 11, 2024

    @mangod9
    Member

    Thanks @AustinWise for your fix, it is now merged.

  17. mnebes commented on Mar 12, 2024

    @mnebes
    Author

    Thanks @AustinWise and everyone involved!

    @mangod9 will this fix be coming to some future release of .NET 8 or only in 9?

  18. AustinWise commented on Mar 12, 2024

    @AustinWise
    Contributor

    It will be in .NET 9. I have opened a PR to propose backporting the fix to .NET 8: #99563

  19. mangod9 commented on Mar 13, 2024

    @mangod9
    Member

    we would need a good case to back port. Assume the workaround is easy enough?

  20. mnebes commented on Mar 13, 2024

    @mnebes
    Author

    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.

  21. glb-cblin commented on Mar 13, 2024

    @glb-cblin

    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...)

  22. locked and limited conversation to collaborators on Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions