Skip to content

Suspecting a memory leak related to QUIC connections #8243

Closed as not planned
Closed as not planned

Description

@leipert

Does your log mention database corruption?

If your Syncthing log reports panics because of database corruption it is
most likely a fault with your system's storage or memory. Affected log
entries will contain lines starting with panic: leveldb. You will need to
delete the index database to clear this, by running syncthing -reset-database.

No

Include required information

Please be sure to include at least:

  • which version of Syncthing and what operating system you are using

    • syncthing v1.19.1 "Fermium Flea" (go1.17.6 darwin-arm64) [email protected] 2022-02-21 13:57:21 UTC [noupgrade] (via homebrew)
    • macOS Monterey 12.3, MacBook Pro (2021), 32GB RAM
  • browser and version, if applicable: 'N/A'

After running for a few days syncthing takes up almost 1 GB of RAM, which is unexpected for a machine with just 1 shared folder with: 269 files, 22 folders and ~628 MiB total size. The contents of the folder don't change that often, and a few days ago the RAM usage was way lower. syncthing hasn't been interacted with in these last two days. syncthing is running for about a week now.

March 28th March 30th
Screenshot 2022-03-28 at 22 55 43 Screenshot 2022-03-30 at 15 21 06
394 MB RAM 932 MB RAM

Activity

  1. added
    bug
    needs-triage
    on Mar 30, 2022
  2. leipert commented on Mar 30, 2022

    @leipert
    Author

    I know that this bug report is not super helpful. I am not a go developer, and have no idea how to debug. It is also a little tricky, because memory usage seems to ramp over time. Could be that due to the 32GB RAM, garbage collection doesn't kick in yet?

    How can I help debugging this?

  3. calmh commented on Mar 30, 2022

    @calmh
    Member

    I run Syncthing on M1. Currently with just over a day's uptime, but it's at about 56 MiB resident, syncing 154 GiB of data.

    Screenshot 2022-03-30 at 17 13 03

  4. AudriusButkevicius commented on Mar 30, 2022

    @AudriusButkevicius
    Member

    Feels like this should live on the forum?

  5. calmh commented on Mar 30, 2022

    @calmh
    Member

    Let's see what the profile says

  6. leipert commented on Mar 30, 2022

    @leipert
    Author

    @calmh Here are the profiles:
    syncthing-profiles.zip

    Sorry if this isn't the right place to report this, @AudriusButkevicius

  7. leipert commented on Mar 30, 2022

    @leipert
    Author

    Had a look with pprof. Seems like a lot of memory is related to quic. Wonder if quic-go/quic-go#3322 is somehow related 🤔

  8. AudriusButkevicius commented on Mar 30, 2022

    @AudriusButkevicius
    Member

    I don't think we churn through streams, we have one per connection. I'm on the phone so can't check the profile to give any more meaningful insights.

  9. imsodin commented on Mar 30, 2022

    @imsodin
    Member

    It's a buffer pool for packets. So might be valid usage due to a lot of churn (seems overly excessive though), or quic-go failing to hand buffers back to the pool in some places. Can you get such profiles at different times to see if it continuously increases (which would align with the overall memory usage you see, but that might also have other causes).

  10. charleshan commented on Mar 30, 2022

    @charleshan

    I've had this issue as well. After a while, the memory usage grows 10x. Also, there's a constant 20-25% CPU usage. After restarting, it goes down to ~0.6%. Nothing is being synced and I don't see anything unusual in the logs.

    ScreenCapture 2022-03-30 at 14 33 56

    ScreenCapture 2022-03-30 at 14 31 16
    ScreenCapture 2022-03-30 at 14 27 54

  11. leipert commented on Mar 30, 2022

    @leipert
    Author

    @imsodin Just created a profile, about 4h later, saw an increase of 100MB since then:
    syncthing-heap-darwin-arm64-v1.19.1-003026.pprof.zip

  12. imsodin commented on Mar 31, 2022

    @imsodin
    Member

    Accompanied by a 30MB increase in that particalur pool in the profile.

    Can you als grap goroutine traces please, to check if there's an unexpected amount of open streams or something - would be surprising but better be sure http://localhost:8384/debug/pprof/goroutine

    The linked quic-go issue talks about not closing connections if I understand correctly. In our code it should be guaranteed that close is called, unless something is hung (which should then show in the traces from above).

  13. leipert commented on Mar 31, 2022

    @leipert
    Author
  14. 16 remaining items

  15. imsodin commented on Apr 11, 2022

    @imsodin
    Member

    The problem is all these operations are blocking, there's no way to pass a context/deadline to them. So even if the hang was detected, the goroutines would stick around.

    Anyway a relevant problem in the current context is quic.Connection.CloseWithError blocking: It waits for sends to finish before returning, thus we never close the actual connection here. Maybe having a deadline there to unblock in quic would be appropriate (@marten-seemann ?). And we definitely can do that when closing the quic conn on our side: Wait 10s for the session to close, and if it doesn't go ahead closing everything else cutting our losses (leaving that on goroutine pending, but not accumulating further ones like it's happening here). Ugly, but less ugly than status quo.

  16. calmh commented on Apr 12, 2022

    @calmh
    Member

    Does WriteMsgUDP not respect write deadlines? It seems one should be set, to be able to fail with a timeout at some point. (Maybe internally in the quic package, maybe from our side if it's exposed.)

    We had a similar issue way back with TLS/TCP connections because Close() does a write, and if we're closing because the connection is dead that will block forever. Setting a write deadline before close works.

  17. imsodin commented on Apr 12, 2022

    @imsodin
    Member

    That's actually it: Due to all the wrapping in quic conns, the deadline we set on the tls conn before closing is not applied to the underlying udp conn. And it's not as simple as just setting that deadline, as all quic connections from the listener share the underlying udp connection - we don't want to set a (very short) deadline for all quic connections when closing one.
    One option would be to set a long timeout on the listener udp-conn (something in line with the ping timeout in the protocol). However there's no possibility to set a timeout, just deadlines - so we'd have to wrap the udp-conn to override all write methods to extend the deadline on every successful write. Very ugly.

  18. calmh commented on Apr 13, 2022

    @calmh
    Member

    Yeah... I'm not aware of any reasonable circumstances where an UDP write should block for an appreciable amount of time, really, so to me this feels like something that should happen in the QUIC package prior to each write. If the UDP write is still blocked after a minute or something, it feels safe to say that something bad has happened and that connection is dead...

  19. bt90 commented on Apr 16, 2022

    @bt90
    Contributor

    Due to all the wrapping in quic conns

    I remember vaguely that it was discussed at some point to abandon the current wrapping/pfilter approach and reimplement it as a handler for non-QUIC packets passed to quic-go.

  20. imsodin commented on Apr 16, 2022

    @imsodin
    Member

    Looking at the quic server code it would be doable to add a write timeout via net.PacketConn.SetWriteDeadline in the sendQueue, possibly configurable through the *Config parameter. @marten-seemann Does that approach seem sensible and if yes, would you welcome such a contribution?

    I remember vaguely that it was discussed at some point to abandon the current wrapping/pfilter approach and reimplement it as a handler for non-QUIC packets passed to quic-go.

    I don't remember these or know what it's abouth. However my "wrapping comment" wasn't correct, it plays a role butit isn't really the fundamental issue here. That's the difference between tcp and quic, where in the former we have one tcp conn per "syncthing connection", while for the latter it's a single udp conn with multiplexed "syncthing/quic connections" on top.

  21. marten-seemann commented on Apr 16, 2022

    @marten-seemann

    Looking at the quic server code it would be doable to add a write timeout via net.PacketConn.SetWriteDeadline in the sendQueue, possibly configurable through the *Config parameter. @marten-seemann Does that approach seem sensible and if yes, would you welcome such a contribution?

    Not sure I understand how that's supposed to work. It's still the same underlying net.PacketConn, so the deadline would apply to all connections. I'd be very worried about the performance impact of setting a write deadline for every packet sent.

    I'd like to raise the question again why we're not raising this problem with the Go team. This is a Go bug after all.

  22. imsodin commented on Apr 16, 2022

    @imsodin
    Member

    Not sure I understand how that's supposed to work. It's still the same underlying net.PacketConn, so the deadline would apply to all connections.

    The deadline would indeed apply to all connections from one quic server/Listener with it's underlying connection. That seems like a desirable property, as all those connections are affected if the underlying connection is broken/timing out.

    I'd be very worried about the performance impact of setting a write deadline for every packet sent.

    That can be done more smartly than setting it on eery write, e.g. by setting a deadline and only extending it some time before it expires.

    I'd like to raise the question again why we're not raising this problem with the Go team. This is a Go bug after all.

    I don't understand this: Why is a blocking/hanging udp conn clearly a go bug (as in not an issue with the underlying system outside of go's control)?

  23. bt90 commented on May 23, 2022

    @bt90
    Contributor

    @marten-seemann is this issue related to our problem here?

    quic-go/quic-go#3409

  24. marten-seemann commented on May 23, 2022

    @marten-seemann

    Probably not. quic-go/quic-go#3409 would cause a connection to be dropped, but shouldn't result in a memory leak (unless your code doesn't properly deal with closed connections).

  25. calmh commented on Mar 10, 2023

    @calmh
    Member

    I'm going to consider this either solved, or need of refreshed troubleshooting if not, as there have been a lot of water under the bridge (new quic-go releases, new Go releases).

  26. leipert commented on Mar 11, 2023

    @leipert
    Author

    Makes sense to me, haven't noticed the problem lately!

  27. added
    frozen-due-to-age
    on Mar 9, 2024
  28. locked and limited conversation to collaborators on Mar 9, 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

    Labels

    bugfrozen-due-to-ageneeds-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions