Skip to content

Possible SerialPort memory leak on Raspbian (all Unix?) #55146

Closed

Description

@dseeg

Description

When using SerialPort.Read(byte[] buffer, int offset, int count) and a TimeoutException occures, the reference for the byte[] buffer used is not released by the underlying SerialStream used by SerialPort.

Sample repo: https://github.com/dseeg/SerialPortMemoryLeak

Application runs though 20000 iterations of trying to read from a SerialPort and timing out each time.

Configuration

  • .NET Version: .NET 5.0.301
  • OS: Raspbian 9.13 stretch
  • Architecture: ARM

Regression?

Unknown

Data

dotnet-counters

Press p to pause, r to resume, q to quit.
    Status: Running

[System.Runtime]
    % Time in GC since last GC (%)                                 0
    Allocation Rate (B / 1 sec)                               33,260
    CPU Usage (%)                                                  1
    Exception Count (Count / 1 sec)                                0
    GC Fragmentation (%)                                           7.642
    GC Heap Size (MB)                                             24
    Gen 0 GC Count (Count / 1 sec)                                 1
    Gen 0 Size (B)                                             9,884
    Gen 1 GC Count (Count / 1 sec)                                 0
    Gen 1 Size (B)                                           485,444
    Gen 2 GC Count (Count / 1 sec)                                 0
    Gen 2 Size (B)                                        25,744,976
    IL Bytes Jitted (B)                                       75,380
    LOH Size (B)                                             131,120
    Monitor Lock Contention Count (Count / 1 sec)                  0
    Number of Active Timers                                        0
    Number of Assemblies Loaded                                   19
    Number of Methods Jitted                                     735
    POH (Pinned Object Heap) Size (B)                         20,016
    ThreadPool Completed Work Item Count (Count / 1 sec)           0
    ThreadPool Queue Length                                        0
    ThreadPool Thread Count                                        0
    Working Set (MB)                                              84

dumpheap -stat

...
6604d32c       11       262276 System.Collections.Concurrent.ConcurrentQueueSegment`1+Slot[[System.IO.Ports.SerialStream+SerialStreamIORequest, System.IO.Ports]][]
6a2c3248    20000       560000 System.IO.Ports.SerialStream+SerialStreamIORequest
6601239c    20000       800000 System.Threading.Tasks.Task`1[[System.Int32, System.Private.CoreLib]]
66014c18    20000       880000 System.Threading.Tasks.Task+ContingentProperties
66012008    20002       880088 System.Threading.CancellationTokenSource
011b9e40    31952      2024484      Free
6a2b700c    20025     20732431 System.Byte[]
Total 135477 objects

gcroot inspection

...
53bf10f4 6a2b700c     1036
53bf157c 6a2b700c     1036
53bf1a04 6a2b700c     1036
53bf1e8c 6a2b700c     1036
53bf2314 6a2b700c     1036
53bf27a8 6a2b700c     1036
53bf2c3c 6a2b700c     1036
53bf30c4 6a2b700c     1036
53bf354c 6a2b700c     1036
53bf39f8 6a2b700c     1036
53bf3eb4 6a2b700c     1036
53bf5708 6a2b700c     1036
53bf6a70 6a2b700c     1036
53bfa4a0 6a2b700c     1036
53bfce58 6a2b700c     1036
53c01688 6a2b700c     1036
53c08bcc 6a2b700c     1036

Statistics:
      MT    Count    TotalSize Class Name
6a2b700c    20025     20732431 System.Byte[]
Total 20025 objects

> gcroot 53bfa4a0
Found 0 unique roots (run 'gcroot -all' to see all roots).
> gcroot 53bf30c4
Thread 75d4:
    7E9016F0 6A215A8C SerialPortMemoryLeak.Program.Main(System.String[]) [C:\Users\Dillon\source\repos\SerialPortMemoryLeak\SerialPortMemoryLeak\Program.cs @ 55]
        r11-44: 7e90171c
            ->  62F08D38 System.IO.Ports.SerialPort
            ->  62F09288 System.IO.Ports.SerialStream
            ->  62F09310 System.Collections.Concurrent.ConcurrentQueue`1[[System.IO.Ports.SerialStream+SerialStreamIORequest, System.IO.Ports]]
            ->  53766868 System.Collections.Concurrent.ConcurrentQueueSegment`1[[System.IO.Ports.SerialStream+SerialStreamIORequest, System.IO.Ports]]
            ->  63F00020 System.Collections.Concurrent.ConcurrentQueueSegment`1+Slot[[System.IO.Ports.SerialStream+SerialStreamIORequest, System.IO.Ports]][]
            ->  53BF3508 System.IO.Ports.SerialStream+SerialStreamIORequest
            ->  53BF30C4 System.Byte[]

Found 1 unique roots (run 'gcroot -all' to see all roots).
> gcroot 53bf1e8c
Thread 75d4:
    7E9016F0 6A215A8C SerialPortMemoryLeak.Program.Main(System.String[]) [C:\Users\Dillon\source\repos\SerialPortMemoryLeak\SerialPortMemoryLeak\Program.cs @ 55]
        r11-44: 7e90171c
            ->  62F08D38 System.IO.Ports.SerialPort
            ->  62F09288 System.IO.Ports.SerialStream
            ->  62F09310 System.Collections.Concurrent.ConcurrentQueue`1[[System.IO.Ports.SerialStream+SerialStreamIORequest, System.IO.Ports]]
            ->  53766868 System.Collections.Concurrent.ConcurrentQueueSegment`1[[System.IO.Ports.SerialStream+SerialStreamIORequest, System.IO.Ports]]
            ->  63F00020 System.Collections.Concurrent.ConcurrentQueueSegment`1+Slot[[System.IO.Ports.SerialStream+SerialStreamIORequest, System.IO.Ports]][]
            ->  53BF22D0 System.IO.Ports.SerialStream+SerialStreamIORequest
            ->  53BF1E8C System.Byte[]

Found 1 unique roots (run 'gcroot -all' to see all roots).

Analysis

From the data I collected using dotnet-counters and dotnet-dump, and inspecting the code for SerialStream.Unix.cs, the byte[] buffer eventually makes its way into a SerialStreamIORequest and loaded into a queue. SerialStreamIORequests should be removed from that queue when they are marked completed. It seems that either this code isn't being reached or IsCompleted is not being set when the cancellation token fires.

Activity

  1. added
    tenet-performance
    on Jul 4, 2021
  2. ghost added
    untriaged
    on Jul 4, 2021
  3. removed
    untriaged
    on Jul 5, 2021
  4. adamsitnik commented on Jul 5, 2021

    @adamsitnik
    Member

    @dseeg thank you for a very detailed bug report. Would you be interested in sending a PR with a fix?

  5. added this to the 7.0.0 milestone on Jul 5, 2021
  6. dseeg commented on Jul 5, 2021

    @dseeg
    Author

    @adamsitnik Yes, I would be very interested, but fixing this will be a little bit of a learning experience for me so I'm not sure how long it will take. I've forked the repo and once its finished building (my PC is ancient by todays standards, so its taking a while) and I've had a chance to play with it, I should have a better idea.

  7. ghost added
    in-pr
    on Jul 6, 2021
  8. ghost removed
    in-pr
    on Jul 9, 2021
  9. ghost added
    in-pr
    on Jul 9, 2021
  10. freddyrios commented on Sep 26, 2021

    @freddyrios

    @adamsitnik I was also hit by a memory leak affecting all Unix that turned out to be fixed recently in .net 6: https://github.com/dotnet/runtime/pull/56866/files#diff-efd183ff76a828527dffa9f7370a0459922121146a211b15fd9a4863d4be70d5R1011-R1023

    I would have commented in that PR, but it seems to be closed and the issue here is also about leak.

    That unnanounced change there fixes a leak that happens due to the SerialStreamIORequest being registered to the cancellation token while also holding a reference to it. It was not a rare occurence, since it triggered by passing a cancelation token when using ReadAsync on the stream. Our dump analysis also showed plenty of SerialStreamIORequest and Task like in the current issue, except in our case it was accompanied CancellationTokenSource.CallbackNode instead of CancellationTokenSource.

    @dseeg the above could explain why SerialStreamIORequest was staying around. I suggest to give it a check against the latest where that PR is in place.

    SerialStreamIORequests should be removed from that queue when they are marked completed. It seems that either this code isn't being reached or IsCompleted is not being set when the cancellation token fires.

  11. dseeg commented on Oct 5, 2021

    @dseeg
    Author

    @freddyrios Just ran my sample application using 6.0.100-rc.1.21463.6 and I'm still seeing similar results as before:

    ...
    74ab3d84    20000       560000 System.IO.Ports.SerialStream+SerialStreamIORequest
    70610cdc    20002       560056 System.Threading.CancellationTokenSource
    706110ac    20000       800000 System.Threading.Tasks.Task`1[[System.Int32, System.Private.CoreLib]]
    70619338    20000       880000 System.Threading.Tasks.Task+ContingentProperties
    0227c4e8    27220      2306468      Free
    74aaec78    20011     20728083 System.Byte[]
    
  12. ghost removed
    in-pr
    on Oct 10, 2021
  13. added
    needs-further-triage
    on Oct 10, 2021
  14. npehrsson commented on Apr 11, 2022

    @npehrsson

    Any progress on this issue, is there still an issue in .NET 6.0.3?
    We discovered this bug as well running .NET Core 3.1, and need to know if it's solved in .NET 6 and if it is worth upgrading to solve this issue.

  15. danmoseley commented on Apr 11, 2022

    @danmoseley
    Contributor

    @npehrsson I don't believe so. We would accept a PR, but otherwise it may be some time (months) before we can give it attention.

  16. danmoseley commented on Apr 11, 2022

    @danmoseley
    Contributor

    If we had a fix in main, we could potentially discuss backporting to 6.0 as well depending on the risk.

  17. ghost added
    in-pr
    on Apr 14, 2022
  18. removed
    needs-further-triage
    on Aug 9, 2022
  19. ghost removed
    in-pr
    on Aug 10, 2022
  20. ghost locked as resolved and limited conversation to collaborators on Sep 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.IO.Portstenet-performance

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions